Q: Finding and printing perfect numbers under 10000 (Liang, Intro to Java, Exercise 5.33) 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.");
                }
            }
        }
    }
}


A: 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.

A: You have a few problems with your program:


*

*You need to reset factorSum to 0 after looping through the factors

*You should check your factorSum == n AFTER adding all the factors, not inside the loop.

*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.");
      }
    }
  }
}


A: 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);
        }
    }
}


A: 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;
}

