TheMegalomaniac
TheMegalomaniac

Reputation: 161

Finding the total number combinations for an integer using three numbers

For a given integer, n, I need to print all the lists of length 3 which sum to n. The members of the list must be non-negative integers. After printing all these lists, it must then print the number of lists that were found.

For example, if n=2:

Here is the program I did for lists of length 2 rather than lists of length 3:

#include <stdio.h>
int main (void)
{
    int total;
    int c1=0;
    int c2=0;
    int c3=0;
    int count;

    printf("Welcome friends and mainly enemies to the thingy. Only postive intergers!!!\n That's right just enter a number here:");
    scanf ("%d",&total);
    printf ("\n\n\nWhy pick %d? Here is the list if combinations anyway,",total);

    for (count=0;count<=total;count++)
    {
        printf ("\n");
        for (c1=count;c1==count;c1--)
        {
            printf("%d ",c1);

        }
        for (c2=total-count;c2<=total-count;c2++)
        {
            printf("%d",c2);

        }

    }
    printf ("\n\nThere are %d number combinations that total %d",count,total);

}

The goal is to extend this from lists of length two to lists of length 3.

Additional Info: I can only use one other variable c3.

Upvotes: 0

Views: 2024

Answers (4)

koushik
koushik

Reputation: 1

#include <stdio.h>

void main()
{
    unsigned int num, p1, p2, p3, count=0;

    printf("Enter a positive number : ");

    scanf("%d", &num);

    for (p1=0; p1<=num; p1++)
    {
        for (p2=0; p2<=num; p2++)
        {
            for (p3=0; p3<=num; p3++)
            {
                if (p1 + p2 + p3 == num)
                {
                    count++;
                    printf("%d + %d + %d = %d\n", p1, p2, p3, num);
                }
            }
        }
    }
    printf("\nThe total combinations for your number %d = %d", num, count);
}

Upvotes: 0

Joel Cornett
Joel Cornett

Reputation: 24788

Hope this helps:

int c3 = 0;
for (int c1 = 0; c1 <= total; c1++) {       
    for (int c2 = total - c1; c2 >= 0; c2--){
        printf("%d %d %d \n", c1, c2, total - c1 - c2);
        c3++;
    }

}

printf("there are %d ways to sum to %d", c3, total);

Upvotes: 3

Boris Strandjev
Boris Strandjev

Reputation: 46943

If you are interested only in the number the answer is combination with repetition of total + 1 elements 2nd class: Let me explain this: let us consider total number of 1s. They have total + 1 gaps inbetween counting the gap before the first 1 and after the last one. You are trying to choose 3 numbers that sum up to total: we will do that by placing to separators in the gaps I just explained. The first number will be the sum of ones before the first separators, the second - the number of ones between the separators, the third - the remaining ones. Note that because we allow for the numbers to be zero we allow the separators to be placed in the same gap and also to be before the first 1 and after the last 1. This is exactly equivalent to "combination with repetition of total + 1 elements 2nd class" as I already said and is classical combinatorial problem. You can read about those kind of combinations here but basically the answer will be ((total + 1) * (total + 2)) / 2. Btw I would suggest to retag this problem with at least algorithm tag.

EDIT: Following the request of further explanation, I will just illustrate all my thoughts with example (actually on the same example you gave in the question): we need to split 2 in three groups. 2 leads us to the number of ones we have to write: _1_1_. Here I have written the so-called-by-me gaps with '_'. Now, I will denote the two separators with '|'. Again the separators are used to determine what amount of 1s are used for the first, second and third number in the sum partition. On the left I write the combination using my notation and on the right the corresponding combination in your notation.

||_1_1_ -> 0, 0, 2
|_1|_1_ -> 0, 1, 1
|_1_1|_ -> 0, 2, 0
_1||_1_ -> 1, 0, 1
_1|_1|_ -> 1, 1, 0
_1_1||_ -> 2, 0, 0

Now, hopefully you understand better what are separators and how they are used to determine what are the three numbers used in a combination. If you increase total the logic stays the same, but you will need to write more if you need to illustrate the case.

Probably now you also understand why the gaps are total + 1 and how each of the two separators can happen to be in each of these two gaps. all this leads us to the promised combination with repetition of total + 1 elements of 2nd class.

Upvotes: 1

dnsmkl
dnsmkl

Reputation: 792

As I see it, this is problem for mathematics not programing

In case all three integers are distinct and can be used more then once:

3 possibilities for 1st digit
3 possibilities for 2nd digit
3 possibilities for 3rd digit
--
27 = 3*3*3

Upvotes: -1

Related Questions