1. Member Join Date
Nov 2011
Posts
4
Rep Power
0

## Perfect number assignment

So I have an assignment to create a program with 3 methods that test to see if the number entered is a perfect number. What I have is close to being done but I'm having a problem with printing my factors in a descending order. When I try to decrement my counter, i , it creates an infinite loop. Any suggestions on how to correct that? Also another thing is that when you test numbers 1-9, 9 shows up with 1 & 3 as factors, not sure why. I'm guessing it has to do in the testPerfect method, but where?
Here is my testPerfect and printPerfect methods, if you need to see the main I can post it. The main prints everything except the factors.
Java Code:
```   public static boolean testPerfect(int number2)
{

int sum = 0;
boolean testing = false;

for (int i = 1; i < number2/2; i++ )
{
if ( number2 % i == 0)
sum = sum + i;

if (sum == number2/2)
testing = true;

}  return testing;

} // end testPerfect

public static void printFactors(int print)
{
for (int i = 1; i <= print /2; i++)
{
if (print % i == 0)
{
System.out.print(i + " ");
}
}
}```
Last edited by MusicMan; 11-23-2011 at 07:24 PM.  Reply With Quote

2. ## Re: Perfect number assignment

When I try to decrement my counter, i , it creates an infinite loop.
Where is the code with this problem?
Do you need to use two variables, one to print out, one to control the loop?  Reply With Quote

3. Member Join Date
Nov 2011
Posts
4
Rep Power
0

## Re: Perfect number assignment

it's in the printFactors method.
Java Code:
```    public static void printFactors(int print)
{

for (int i = 1; i <= print /2; i++)
{
if (print % i == 0)
{
System.out.print(i + " ");

}

}
}```  Reply With Quote

4. ## Re: Perfect number assignment

Can you explain what the problem is with the code you just posted?  Reply With Quote

5. Member Join Date
Nov 2011
Posts
4
Rep Power
0

## Re: Perfect number assignment

Certainly. It is printing out the factors in increasing order. I need them to be in decreasing order.  Reply With Quote

6. ## Re: Perfect number assignment

Then start at the other end and work down.  Reply With Quote

7. Member Join Date
Nov 2011
Posts
4
Rep Power
0

## Re: Perfect number assignment

I've changed the print statement to:
Java Code:
`System.out.printf("%d", --i);`
But that results in an infinite loop.  Reply With Quote

8. ## Re: Perfect number assignment

What stops the loop from executing? If you change the value of the loop control variable then you can effect how if and when the loop will stop.
Take a piece of paper and write down the starting value and under it the values you want to have printed out.
What value will i start with and how will i change as you go through the loop?
What will be the ending value of i?
How do you get from the starting value to the ending value of it?

Answering those questions will tell you how to code the if statement.
Go back and look at the definitions of the 3 clauses in the if statement.  Reply With Quote

9. ## Re: Perfect number assignment Originally Posted by MusicMan Certainly. It is printing out the factors in increasing order. I need them to be in decreasing order.
Think a bit: if a number f is that smallest factor of a number n, then n%f == 0 and n == f*p where p is (consequently) the largest factor.

kind regards,

Jos  Reply With Quote

10. Senior Member Join Date
Jan 2012
Posts
210
Rep Power
10

## Re: Perfect number assignment Originally Posted by MusicMan Also another thing is that when you test numbers 1-9, 9 shows up with 1 & 3 as factors, not sure why. I'm guessing it has to do in the testPerfect method, but where?
Try this to find smallest factors of integer:
Java Code:
```        int number = 9;

for (int i = 2; number != 1; i++) {
while (number % i == 0) {
System.out.println(i);
number /= i;
}
}```  Reply With Quote

loop problem, perfect number 