1.11M Members

Help highschool 1rst 4 perfect numbers

 
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++;
}
} 
}
 
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 six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: