Question

I attempted this program in java for a bounded constant input for upto 1000 integers. Not getting the expected output :(

package perfect.numbers;
import java.util.ArrayList;
public class PerfectNumbers {

    public static void main(String[] args) {
        long num=1000; int sum = 0;
        ArrayList<Integer> perfectList = new ArrayList<>();
        ArrayList<Integer> factorList = new ArrayList<>();
        for(int i=1; i<num; i++)
        {
           for(int j=1; j<i/2; j++)
           {         
                if(i%j==0)
                {
                    factorList.add(j);
                }

                for(int h=0; h<factorList.size(); h++)
                    sum=sum+factorList.get(h);

                if(sum==i)
                perfectList.add(i);
                sum=0;
            }
        }

        System.out.println("The Perfect numbers upto 1000 are : ");

        for(Integer item : perfectList)
        {
            System.out.print(item + "/t");
        }
    }
}
Was it helpful?

Solution

Your code has multiple problems:

  1. factorList not cleared
  2. for instance 6 is a perfect number = 1+2+3 but the inter for loop stops at 2. (j<3)
  3. Also I have the third inner for I moved out of the second inner for loop.

Here is the correct version:

import java.util.ArrayList;

public class PerfectNumbers {

    public static void main(String[] args) {
        long num = 1000;
        int sum = 0;
        ArrayList<Integer> perfectList = new ArrayList<>();
        ArrayList<Integer> factorList = new ArrayList<>();
        for (int i = 6; i < num; i++) {
            factorList.clear();
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {
                    factorList.add(j);
                }
            }
            sum = 0;
            for (int h = 0; h < factorList.size(); h++) {
                sum = sum + factorList.get(h);
            }
            if (sum == i) {
                perfectList.add(i);
            }

        }
        System.out.println("The Perfect numbers upto 1000 are : ");
        for (Integer item : perfectList) {
            System.out.print(item + "/t");
        }
    }
}

OTHER TIPS

  1. Move the sum calculation code from second loop to the first loop.
  2. Clear the factorList after sum calculation.

I think your Inner for loop should be like this :

for(int j=1; j<=i/2; j++)
{         
       if(i%j==0)
       {
                factorList.add(j);
       }
}
for(int h=0; h<factorList.size(); h++)
      sum=sum+factorList.get(h);

if(sum==i)
perfectList.add(i);
sum=0;

Here I closed the inner forloop before performing the sum. In your case what happened, every time when a new digit which is satisfy the condition i%j==0, sum is calculated and if the sum is not equal to i then sum is reset to 0. That cause the problem.

And I changed inner for loop to j<=i/2. Explaination : take i = 6. And We know the factor of 6 those are : 1,2,3. Now if j<i/2 is inner for loop then it will neglect the number 3 and exit from that instance so we never get 3 in factorlist. Hope this will answer all your questions.

found answer through google, Updated little as per your requirement

are you expecting below output

The Perfect numbers upto 1000 are : 0 6 28 496

public class PerfectNumbers {   
    public static boolean  isPerfectNumber(int number){        
        int temp = 0;
        for(int i=1;i<=number/2;i++){
            if(number%i == 0){
                temp += i;
            }
        }
        if(temp == number){           
            return true;
        } else {            
            return false;
        }
    }

 public static void main(String[] args) {
    System.out.println("The Perfect numbers upto 1000 are :");
     for (int i=0;i<= 1000; i++){
         if(PerfectNumbers.isPerfectNumber(i)){
            System.out.println(i);
         }
     }              
 } 
}

There's an obvious problem in your inner loop (i.e. the for loop with j as index variable): your end test is j<i/2. This will terminate the loop so that j will never reach i/2, but every even integer i has i/2 among its proper divisors. You need to change the end test to j <= i/2.

You would probably also want to change the end test in the outer loop to i <= num. Otherwise you will only check the integers up to num - 1.

You also need to move the sum calculation and check from the inner into the outer loop, because you want to do those only once for every potential perfect number, not for every potential proper divisor.

/*
@ Author 12CSE54
@ Date 28.10.14
*/

import java.util.*; 

public class perfect {
    Scanner s = new Scanner(System.in);

    public void check() {
        System.out.println("Enter the number\n");
        int p = sc.nextInt();
        int temp = 0;

        for(int i = 1; i <= p / 2; ++i) {
            if (p % i == 0)
                temp += i;
        }

        if (temp == p)
            System.out.println("It is a perfect number");
        else 
            System.out.println("It is not a perfect number");
    }

    public static void main(String args[]) {
        perfect p1 = new perfect();
        p1.check();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top