sjpdlcn
sjpdlcn

Reputation: 5

Program that displays every perfect number

My code runs but for one of the tests two outputs are printed when I only need one. I am unsure of how to avoid this.

This is the task:

Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the sum of all the numbers that divide evenly into it. For example, 6 is perfect because 1, 2, and 3 divide evenly into it and their sum is 6; however, 12 is not a perfect number because 1, 2, 3, 4, and 6 divide evenly into it, and their sum is greater than 12.

The provided template lays out the initial for loop to check each number beginning from 2 and going up to 1,000. In this for loop, the perfect() method is called, where you will submit your piece of code to test if each number follows the conditions described above.

Set result to true if it meets those conditions and use sum to add up the numbers divisible by int n in the method's parameter.

public class Perfect{
public static void main (String args[]){
  final int MAX = 1000;
  for(int i = 2; i <= MAX; i++)
  if(perfect(i) == true)
  System.out.println("The number " + i + " is perfect");
}

 public static boolean perfect(int n){
  int sum = 1;
  int i;
  boolean result = false;
    for (i = 2; i < n / 2; i++) {
        if (n % i == 0) {
            sum += i;
        }
    }
  if (sum == i) {
  return true;
  }
  else {
  return false;
  }
 }
}

My output: The number 496 is perfect The number 729 is perfect

Expected output: The number 496 is perfect

It only expects the first line printed...

Upvotes: 0

Views: 2758

Answers (4)

ICodeForCaffeine
ICodeForCaffeine

Reputation: 195

First, I don't know if you have used the correct formula. But, you should know that the first perfect number are 6, 28, 496 and 8128. 729 is not a perfect number.

Hope it helped.

    public static void main(String[] args) {
        int i, j, s;
        System.out.println("Perfect numbers 1 to 1000: ");
        for(i=1;i<=1000;i++){    
            s=0;
            for(j=1;j<i;j++){ 
                 if(i%j==0){
                    s=s+j;  
                 }
            }
          if(i==s){           //if i == s is perfect
                 System.out.println(i);
              }
        }
    }
}

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109567

You checked sum == i instead of sum == n.

As 729 = 3^6 : 3, 243, 9, 81, 27.

public static boolean perfect(int n) {
    int sum = 1;
    for (int i = 2; i <= n / 2 && sum <= n; i++) {
        if (n % i == 0) {
            sum += i;
        }
    }
    return sum == n;
}

Upvotes: 0

Guy
Guy

Reputation: 50864

You need to compare sum to the original number n, not to i. And you need to add 1 to the loop condition or it will miss the last divider in even numbers

public static boolean perfect(int n){
    int sum = 1;
    for (int i = 2; i < (n / 2) + 1; i++) {
        if (n % i == 0) {
            sum += i;
        }
    }
    return sum == n;
}

Upvotes: 3

Sachini Wickramaratne
Sachini Wickramaratne

Reputation: 599

According to the question, you have to print all perfect numbers. I have created a small snippet, try it and see.

    public void printPerfect() {

        for(int i=2;i<1000;i++) {
            List<Integer> l =factors(i);
            int sum =0;
            for (Integer factor : l) {
                sum+=factor;
            }
            if(sum==i) {
                System.out.println("perfect-- " +i);
            }
        }


    }

List<Integer> factors(int number) {
        List<Integer> l = new ArrayList<Integer>();
        for(int i = 1; i <= number; ++i) {
            if (number % i == 0) {
                if(i!=number)
                    l.add(i);
            }
        }
        return l;
    }

Upvotes: 0

Related Questions