Results 1 to 10 of 10
  1. #1
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default 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 07:21 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #3
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    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. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes, but are they *close* to the perfect ones?

  5. #5
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    very close actually, the three are 6, 28, and 496. Got it...didn't subtract 1 in the print statement as well....doh

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #8
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    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 07:35 AM.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #10
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    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...:(.

Similar Threads

  1. Guide to hacking a website with 1000 way
    By thanhquoc in forum Networking
    Replies: 1
    Last Post: 07-29-2010, 07:08 AM
  2. Phone number Program ..
    By Sary in forum New To Java
    Replies: 9
    Last Post: 03-17-2010, 07:15 PM
  3. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM
  4. approx. 1000 instances of class?
    By artemff in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-01-2010, 07:57 PM
  5. Efficient Perfect Number
    By Lite3864 in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 01:07 AM

Posting Permissions

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