1,105,214 Community Members

Help highschool 1rst 4 perfect numbers

Member Avatar
kiaski
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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++;
}
} 
}
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: