2

A perfect number is a number that's equal to the sum of all its positive divisors, excluding itself.

For my homework, I'm trying to write a program to find all four perfect numbers under 10000, but my code doesn't work when I run it and I'm not sure why (it just runs for a second or two, and then says "build successful" after not printing anything). I included it below, along with some comments that explain my thought process. Can someone help me out and tell me what's wrong with it?

public class HomeworkTwo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        //Variable declaration

        int n;
        int possibleFactor;
        int factorSum=0;


        /**For each n, the program looks through all positive integers less than n  
           and tries to find factors of n. Once found, it adds them
           together and checks if the sum equals n. Then it repeats till n=9999. **/

        for (n=2; n<10000; n++) {
            for (possibleFactor = 1; possibleFactor < n; possibleFactor++) {
                if (n % possibleFactor == 0) {
                    factorSum = possibleFactor + factorSum;
                }

                //Is the number perfect? Printing
                if (factorSum == n) {
                    System.out.println(""+ n +" is a perfect number.");
                }
            }
        }
    }
}
pikachu
  • 63
  • 5
Asker
  • 1,299
  • 2
  • 14
  • 31

4 Answers4

3

You initialize factorSum to 0 before the first for loop, but you don't reset it to 0 when trying each new n. The factors keep adding up and are never equal tot he number to check. Reset it to 0 at the beginning of the n for loop.

Also, you may want to move the test and print of the number being a perfect number after the inner for loop, but before the end of the outer for loop, or else it may print more than necessary.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

You have a few problems with your program:

  1. You need to reset factorSum to 0 after looping through the factors
  2. You should check your factorSum == n AFTER adding all the factors, not inside the loop.
  3. You only need to check up to n/2; for example 10 will never be divisible by 7.

Here's the resulting program (with slightly better formatting):

public class HomeworkTwo {

  /**
   * @param args
   *          the command line arguments
   */
  public static void main(String[] args) {

    // Variable declaration

    int n;
    int possibleFactor;
    int factorSum = 0;

    /**
     * For each n, the program looks through all positive integers less than n
     * and tries to find factors of n. Once found, it adds them together and
     * checks if the sum equals n. Then it repeats till n=9999.
     **/

    for (n = 2; n < 10000; n++) {
      factorSum = 0;
      for (possibleFactor = 1; possibleFactor <= n / 2; possibleFactor++) {
        if (n % possibleFactor == 0) {
          factorSum = possibleFactor + factorSum;
        }
      }
      // Is the number perfect? Printing
      if (factorSum == n) {
        System.out.println("" + n + " is a perfect number.");
      }
    }
  }
}
durron597
  • 31,968
  • 17
  • 99
  • 158
  • Thanks, this was a good answer. Would thumbs up, but don't have enough rep yet so this comment will be a temporary substitute – Asker Apr 10 '15 at 18:35
1

I suppose you already did it, but anyway, the main problem in your code is the "factorSum" variable. After checking each number, you should set it to 0 again. I addition, I used printf instead println, but it is the same:

public static void main(String[] args) {
    int number = 0;
    int factor = 0;
    int factorSum = 0;

    for(number = 2; number < 10000; number++) { //for each number we will check the possible factors.
        factorSum = 0;

        for(factor = 1; factor < number; factor++)
            if((number % factor) == 0) { //if it is a factor, we add his value to factorSum.
                factorSum = factorSum + factor;
            }

        if(factorSum == number) {
            System.out.printf("The number: %d is a perfect number.\n", number);
        }
    }
}
0

You should keep it like this

for (n=2; n<10000; n++) {
    for (possibleFactor = 1; possibleFactor < n; possibleFactor++) {
        if (n % possibleFactor == 0) {
            factorSum = possibleFactor + factorSum;
        }
    }

    //Is the number perfect? Printing
    if (factorSum == n) {
        System.out.println(""+ n +" is a perfect number.");
    }
    factorSum = 0;
}
pikachu
  • 63
  • 5