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.
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 + " ");
}
}
}
Re: Perfect number assignment
Quote:
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?
Re: Perfect number assignment
it's in the printFactors method.
Code:
public static void printFactors(int print)
{
for (int i = 1; i <= print /2; i++)
{
if (print % i == 0)
{
System.out.print(i + " ");
}
}
}
Re: Perfect number assignment
Can you explain what the problem is with the code you just posted?
Re: Perfect number assignment
Certainly. It is printing out the factors in increasing order. I need them to be in decreasing order.
Re: Perfect number assignment
Then start at the other end and work down.
Re: Perfect number assignment
I've changed the print statement to:
Code:
System.out.printf("%d", --i);
But that results in an infinite loop.
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.
Say you start with 14, what will be in the list.
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.
Re: Perfect number assignment
Quote:
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
Re: Perfect number assignment
Quote:
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:
Code:
int number = 9;
for (int i = 2; number != 1; i++) {
while (number % i == 0) {
System.out.println(i);
number /= i;
}
}