soccersquirt82

Aug 31, 2008, 03:32 PM

Part of my homework is to make a program in Java that will take all the divisors of n and add them up. (e.g. n = 10, answer = 1+2+5 = 8) The part in italics is what (I think) keeps it from looping. The answer to all numbers is one because it doesn't loop. What can the italicized line be or am I way off?

static int slow (int n){

int divisor = 1; // number to divide n by and increases by one each time until above n

int answer = 0; // number that is being returned

//loop condition

while (divisor < n) {

if((n % divisor) == 0) {

return divisor; }

answer = answer + divisor;

divisor = divisor + 1;

}

return answer;

}

static int slow (int n){

int divisor = 1; // number to divide n by and increases by one each time until above n

int answer = 0; // number that is being returned

//loop condition

while (divisor < n) {

if((n % divisor) == 0) {

return divisor; }

answer = answer + divisor;

divisor = divisor + 1;

}

return answer;

}