We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,661 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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++;
}
} 
}
2
Contributors
1
Reply
8 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
kiaski
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0588 seconds using 2.74MB