Results 1 to 10 of 10
  1. #1
    MusicMan is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default 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 06:24 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default 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?

  3. #3
    MusicMan is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default 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 + " ");
    
                    }
    
            }
         }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Perfect number assignment

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

  5. #5
    MusicMan is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Perfect number assignment

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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Perfect number assignment

    Then start at the other end and work down.

  7. #7
    MusicMan is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: Perfect number assignment

    Quote Originally Posted by MusicMan View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Perfect number assignment

    Quote Originally Posted by MusicMan View Post
    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;
                }   
            }

Similar Threads

  1. Perfect number 1-1000 program help
    By ImWithStupid in forum New To Java
    Replies: 9
    Last Post: 04-28-2011, 08:22 AM
  2. Replies: 9
    Last Post: 03-06-2011, 03:00 AM
  3. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 07:44 PM
  4. Efficient Perfect Number
    By Lite3864 in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 01:07 AM
  5. Way to Java Perfect
    By Javaisinmyblood in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 11:28 PM

Tags for this Thread

Posting Permissions

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