# All possible way to divide a number?

• 09-24-2010, 01:13 AM
bobocheez
All possible way to divide a number?
Say you have 100.

How can you find all combinations of 4 numbers that will add up to 100?

I'm guessing using 4 loops, but I can't wrap my head around how to set it up.
• 09-24-2010, 01:23 AM
crikey
Well, you could use a random number generator, but it may take up to 100,000 calculations just to find what you need. But in the end, you will have to do for loops. Try studying them; It is not too hard.
• 09-24-2010, 01:25 AM
bobocheez
I going in the right direction?

Code:

```int counter = 0;                 for (int draw1 = 0; draw1 <15; draw1++){                         for (int draw2 = 0; draw2<15; draw2++){                                 for (int draw3 = 0; draw3<15; draw3++) {                                         for (int draw4 = 0; draw4<15; draw4++){                                                 if (draw1*draw1 +draw2*draw2 + draw3*draw3 + draw4*draw4 == 200){                                                         System.out.println(draw1+" "+draw2+" "+draw3+" "+draw4);                                                         counter++;                                                 }                                         }                                 }                         }                 }```
• 09-24-2010, 01:39 AM
bobocheez
Ok here's the final code.
Does it look right?

Code:

```int count = 1;                 for (int d1 = 1; d1 <=200; d1++){                         for (int d2 = 1; d2<=200; d2++){                                 for (int d3 = 1; d3<=200; d3++) {                                         for (int d4 = 1; d4<=200; d4++){                                                 if (d1*d1 + d2*d2 + d3*d3 + d4*d4 == 200){                                                         System.out.println(d1+" "+d2+" "+d3+" "+d4+" "+count);                                                         count++;                                                 }                                         }                                 }                         }                 }```
• 09-24-2010, 03:24 AM
Norm
Quote:

if (d1*d1 + d2*d2 + d3*d3 + d4*d4 == 200){
What does this test for? It looks different from the original problem description.