Results 1 to 5 of 5
  1. #1
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default 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.

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    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.

  3. #3
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    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++;
    						}
    					}
    				}
    			}
    		}

  4. #4
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    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++;
    						}
    					}
    				}
    			}
    		}

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

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

Similar Threads

  1. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM
  3. Divide and Conquer@Array...im becoming crazy!
    By wyldstyle in forum New To Java
    Replies: 0
    Last Post: 08-23-2009, 10:20 PM
  4. Replies: 2
    Last Post: 08-04-2009, 01:35 AM
  5. How to divide code in classes?
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 06:36 PM

Posting Permissions

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