# Thread: All possible way to divide a number?

1. Member Join Date
Aug 2010
Posts
28
Rep Power
0

## 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.  Reply With Quote

2. Senior Member Join Date
Jul 2010
Posts
125
Rep Power
0

## 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.  Reply With Quote

3. Member Join Date
Aug 2010
Posts
28
Rep Power
0

## I going in the right direction?

Java 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++;
}
}
}
}
}```  Reply With Quote

4. Member Join Date
Aug 2010
Posts
28
Rep Power
0

## Ok here's the final code.
Does it look right?

PHP 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++;
}
}
}
}
}```  Reply With Quote

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•