查找并打印 10000 以下的完美数字(Liang,Java 简介,练习 5.33)

Finding and printing perfect numbers under 10000 (Liang, Intro to Java, Exercise 5.33)

完美数是等于所有正除数之和的数,不包括它自己。

对于我的家庭作业,我正在尝试编写一个程序来查找 10000 以下的所有四个完美数字,但是当我 运行 它时我的代码不起作用而且我不确定为什么(它只是 运行s 一两秒钟,然后在不打印任何内容后说 "build successful")。我将其包含在下面,以及一些解释我的思考过程的评论。有人可以帮我解决问题吗?

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

您在第一个 for 循环之前将 factorSum 初始化为 0,但是在尝试每个新的 n 时您没有将其重置为 0 .这些因素不断累加,永远不会等于要检查的数量。在 n for 循环开始时将其重置为 0

此外,您可能希望在内部 for 循环之后但在外部 for 循环结束之前移动测试和打印数字是一个完美的数字,否则它可能会打印超出需要的内容。

您的程序有一些问题:

  1. 您需要在遍历因数后将 factorSum 重置为 0
  2. 您应该在添加所有因素之后检查您的 factorSum == n,而不是在循环内。
  3. 您最多只需要检查n/2;例如 10 永远不能被 7 整除。

这是生成的程序(格式稍好):

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

你应该保持这样

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

我想您已经这样做了,但无论如何,您代码中的主要问题是 "factorSum" 变量。检查每个数字后,您应该再次将其设置为 0。另外,我用printf代替了println,但是是一样的:

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