954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help highschool 1rst 4 perfect numbers

A number is called a proper divisor of N if M < N and M divides N. A positive integer is called perfect if it is the sum of its positive proper divisors. For example, the positive proper divisors of 28 are 1, 2, 4, 7, and 14 and 1+2+4+7+14=28. Therefore, 28 is perfect. Write a program to display the first 4 perfect integers.

I got this but the loop won't stop running.

public class PerfectNumbers
{
    static int FIRSTPERFECT;
    static int SECONDPERFECT;
    static int THIRDPERFECT;
    static int FOURTHPERFECT;
        
 
    public static void main(String[] args)
    {
        findPerfect();
        System.out.println(FIRSTPERFECT);
        System.out.println(SECONDPERFECT);
        System.out.println(THIRDPERFECT);
        System.out.println(FOURTHPERFECT);
    }
    public static void findPerfect()
    {
        int count = 0;
        int sum = 0;
        int n = 1;
        while(count <= 4)
        {
            int divisor = 1;
            n = 1;
            sum = 0;
            while (divisor < n)
            {
                if(n % divisor == 0)
                {
                    sum += divisor;
                }
                divisor++;
            }
       
           if(sum == n)
           {
      
                if(count == 1)
                {
                    FIRSTPERFECT = n;
                    count++;
                    n++;
                }
                else if(count == 2)
                {
                    SECONDPERFECT= n;
                    count++;
                    n++;
                }
                else if(count == 3)
                {
                    THIRDPERFECT = n;
                    count++;
                    n++;
                }
                else if(count == 4)
                {
                    FOURTHPERFECT = n;
                    count++;
                    n++;
                }
      
           }
           else
                n++;
}
} 
}
kiaski
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Look at lines 24 and 25, then consider what will happen with those values in your loop condition on line 27.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: