# Perfect number 1-1000 program help

• 04-28-2011, 08:01 AM
ImWithStupid
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):

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?
• 04-28-2011, 08:08 AM
pbrockway2
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?
• 04-28-2011, 08:18 AM
ImWithStupid
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 =(.
• 04-28-2011, 08:21 AM
pbrockway2
Yes, but are they *close* to the perfect ones?
• 04-28-2011, 08:25 AM
ImWithStupid
very close actually, the three are 6, 28, and 496. Got it...didn't subtract 1 in the print statement as well....doh
• 04-28-2011, 08:28 AM
pbrockway2
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.
• 04-28-2011, 08:29 AM
pbrockway2
Quote:

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.)
• 04-28-2011, 08:32 AM
ImWithStupid
Thank you for the help hint and the tips, here is the final working code:

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.
• 04-28-2011, 08:43 AM
pbrockway2
Quote:

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.

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

Code:

`for(number = 1; number <= LIMIT + 1; number++)`

because the number isn't reported until the *next* time around the loop.
• 04-28-2011, 09:22 AM
ImWithStupid
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...:(.