This content has been marked as final.
Show 5 replies

1. Re: How can I find divisors & add them by using if and while statements?
jschellSomeoneStoleMyAlias Jan 7, 2005 2:58 AM (in response to 807596)I have tried this using alittle more 'advanced' java
And exactly what part are you having trouble with?
like arrays and collections, but I dont quite get
them yet. What I need to do is find perfect numbers
by checking divisors of numbers and adding them up.
If the divisors of a number sum up to that number,
, then its perfect. I need to test a few different
numbers so how would you guys write it out.
I have tried a few different ways but nothing has
worked so far. 
2. Re: How can I find divisors & add them by using if and while statements?
807596 Jan 7, 2005 3:10 AM (in response to jschellSomeoneStoleMyAlias)Well everytime I rewrite the code it compiles, but will never find the perfect numbers. Usually it gets caught up on a loop. I can tell by adding a few println( ) around. So then I play with it and try a few different ways. Then eventually just rewrite it. And the same happens.
Here is one of my codes to give you an idea of what I am trying to do, anyone know how I can fix it so it does what I want?
import java.io.*; public class Testing { public static void main (String[] args) throws IOException { int testnumber = 4; //the testing number int testdivisor = 1; //the test divisor int divisors; //the amount of divisors int divisor1 = 0; //the value of 1st divisor int divisor2 = 0; //2nd divisor int divisor3 = 0; //3rd divisor int divisor4 = 0; //4th divisor int hits2 = 0; int hits = 0; //# Of hits the last if statement has, //once it reaches 4 it ends program while (hits < 4) { testdivisor=1; System.out.println("1st while"); while (testdivisor<=testnumber) { testdivisor++; System.out.println("2nd while"); while (testdivisor % testnumber !=0) { testdivisor++; System.out.println("3rd while"); } if (divisor1==0) { divisor1=testdivisor; } if (divisor2==0) { divisor2=testdivisor; } if (divisor3==0) { divisor3=testdivisor; } if (divisor3==0) { divisor4=testdivisor; } } if ((divisor1+divisor2+divisor3+divisor4)==testnumber) { System.out.println(testnumber); hits++; divisor1=0; divisor2=0; divisor3=0; divisor4=0; System.out.println("final"); } else if ((divisor1+divisor2+divisor3+divisor4)!=testnumber) { testnumber++; divisor1=0; divisor2=0; divisor3=0; divisor4=0; System.out.println("WRONG"); } } } }

3. Re: How can I find divisors & add them by using if and while statements?
807596 Jan 7, 2005 3:37 AM (in response to 807596)Oi.
There must be an array tutorial out there somewhere.
GO, QUICK, FIND IT BEFORE YOU WRITE CODE LIKE THAT AGAIN!
For the love of programming everywhere, Godspeed.
~Cheers 
4. Re: How can I find divisors & add them by using if and while statements?
807596 Jan 7, 2005 5:38 AM (in response to 807596)try this:
just look at every number and see if it is a divisor of n and if it is, add it to the total. If you want to do something more and see how it arrives at its answersint total = 0; for(int i=1; i<n; i++){ if(n%i == 0) total+=i; } System.out.println("n = "+n+" sum of divisors = "+total);
just put a print statement in the place where you detect divisors.
int total = 0; for(int i=1; i<n; i++){ if(n%i == 0){ total+=i; System.out.println("divisor ="+i+" subtotal="+total); } } System.out.println("n = "+n+" sum of divisors = "+total);

5. Re: How can I find divisors & add them by using if and while statements?
807596 Jan 7, 2005 4:21 PM (in response to 807596)Also, stop posting this question over and over.