Counting combinations in c or in python

Posted by Dennis on Stack Overflow See other posts from Stack Overflow or by Dennis
Published on 2011-01-02T14:27:45Z Indexed on 2011/01/02 23:53 UTC
Read the original article Hit count: 227

Filed under:
|

Hello I looked a bit on this topic here but I found nothing that could help me.

I need a program in Python or in C that will give me all possible combinations of a and b that will meet the requirement n=2*a+b, for n from 0 to 10. a, b and n are integers. For example if n=0 both a and b must be 0. For n=1 a must be zero and b must be 1, for n=2 a can be 1 and b=0, or a=0 and b=2, etc.

I'm not that good with programming. I made this:

#include <stdio.h>

int main(void){
    int a,b,n;
    for(n = 0; n <= 10; n++){
        for(a = 0; a <= 10; a++){
            for(b = 0; b <= 10; b++)
                if(n == 2*a + b)
                    printf("(%d, %d), ", (a,b));
        }
        printf("\n");       
    }
}

But it keeps getting strange results like this:

(0, -1079628000), 
(1, -1079628000), 
(2, -1079628000), (0, -1079628000), 
(3, -1079628000), (1, -1079628000), 
(4, -1079628000), (2, -1079628000), (0, -1079628000), 
(5, -1079628000), (3, -1079628000), (1, -1079628000), 
(6, -1079628000), (4, -1079628000), (2, -1079628000), (0, -1079628000), 
(7, -1079628000), (5, -1079628000), (3, -1079628000), (1, -1079628000), 
(8, -1079628000), (6, -1079628000), (4, -1079628000), (2, -1079628000), (0, -1079628000), 
(9, -1079628000), (7, -1079628000), (5, -1079628000), (3, -1079628000), (1, -1079628000), 
(10, -1079628000), (8, -1079628000), (6, -1079628000), (4, -1079628000), (2, -1079628000), (0, -1079628000), 

ideone

Any idea what is wrong?

Also if I could do this for Python it would be even cooler. :D

© Stack Overflow or respective owner

Related posts about python

Related posts about c