# Thread: Perfect number 1-1000 program help

1. Member
Join Date
Nov 2010
Posts
10
Rep Power
0

## Perfect number 1-1000 program help

I am doing an exercise in my book that asks to make a program that finds the perfect numbers from 1-1000. The way I wrote the program makes sense to me but I am not getting the results I am supposed to be getting. Here is what I have (a perfect number is the sum of numbers that divide evenly into it equaling the number example 6 is 1, 2, 3 which add up to 6):

Java Code:
```//finds the perfect numbers between 1-1000
public class Perfect
{
public static void main(String[] args)
{
int number, total = 0;
final int LIMIT = 1000;
for(number = 1; number <= LIMIT; number++)
{
if(total == (number - 1))// optimized so it isn't checked in inner loop, have to subtract 1 to account for increment
System.out.println(number + " is a perfect number");
total = 0;//reset total
for(int divider = 1; divider < number; divider++)
{
if((number % divider) == 0)
total += divider;
}
}
}
}```
Can anyone tell me what I am doing wrong?
Last edited by ImWithStupid; 04-28-2011 at 08:21 AM.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
15
First, you are missing a } at the end. Related to this, when you post use the "code" tags. The idea is you put [code] at the start of the code and [/code] at the end. That way the code is readable with indents (and missing braces) clearly visible.

Next, so what *is* the program's behaviour at runtime? Ie what does it print? And could this be related to the optimisation in some way?

3. Member
Join Date
Nov 2010
Posts
10
Rep Power
0
Sorry about the bad format, didn't know about the [code] tags and the missing } was because I copy and pasted wrong (missed the bottom one). The output is 1, 7, 29, 497 which aren't perfect numbers =(.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
15
Yes, but are they *close* to the perfect ones?

5. Member
Join Date
Nov 2010
Posts
10
Rep Power
0
very close actually, the three are 6, 28, and 496. Got it...didn't subtract 1 in the print statement as well....doh

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
15
And - by the way - you wouldn't want to check for perfectness inside the inner loop (because total hasn't been fully calculated).

The most natural (and least error prone) place to check and print if perfect is just after the inner loop.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
15
Got it...didn't subtract 1 in the print statement as well....doh

Well done. You've still got an erroneous zero to get rid of but that's no big deal. (and can be avoided as in my previous post.)

8. Member
Join Date
Nov 2010
Posts
10
Rep Power
0
Thank you for the help hint and the tips, here is the final working code:

Java Code:
```//finds the perfect numbers between 1-1000
public class Perfect
{
public static void main(String[] args)
{
int number, total = 0;
final int LIMIT = 1000;
for(number = 1; number <= LIMIT; number++)
{
if((number != 1) && (total == (number - 1)))//1 is subtracted from number to account for increment before comparison
System.out.println((number - 1) + " is a perfect number");
total = 0;//reset total
for(int divider = 1; divider < number; divider++)
{
if((number % divider) == 0)
total += divider;
}
}
}
}```
got rid of the 0 by checking for 1, but that might be a bit redundant. It serves its purpose for this program though.
Last edited by ImWithStupid; 04-28-2011 at 08:35 AM.

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
15
Thank you for the help hint and the tips, here is the final working code

You're welcome. Personally I would put the check and print just after the inner for loop has finished. Just for clarity as much as anything.

Java Code:
```//finds the perfect numbers between 1-1000
public class Perfect
{
public static void main(String[] args)
{
int number, total = 0;
final int LIMIT = 1000;
for(number = 1; number <= LIMIT; number++)
{
total = 0;//reset total
for(int divider = 1; divider < number; divider++)
{
if((number % divider) == 0)
total += divider;
}
[color=blue]if(total == number)
System.out.println(number + " is a perfect number");[/color]
}
}
}```

There's also a subtle problem that emerges when LIMIT itself is perfect. The way you have written it the outer for loop should really be

Java Code:
`for(number = 1; number <= LIMIT + 1; number++)`

because the number isn't reported until the *next* time around the loop.

10. Member
Join Date
Nov 2010
Posts
10
Rep Power
0
haha wow, thanks for pointing that out, I'm still pretty new to programming and completely overlooked putting the check after the inner loop. That completely didn't cross my mind that you can put more statements after the inner loop before the outer loop does its check and increment. Thanks for showing me that, it should help prevent a lot of confusion down the road and make my code a lot less complicated. For now loops are my enemy, a little more practice and I think I will get the hang of them, I know I'll need them for initializing arrays...:(.

#### Posting Permissions

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