2

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

对于我的家庭作业,我正在尝试编写一个程序来查找 10000 以下的所有四个完美数字,但是当我运行它时我的代码不起作用,我不知道为什么(它只运行了一两秒钟,并且然后在不打印任何内容后说“构建成功”)。我将它包括在下面,以及一些解释我的思考过程的评论。有人可以帮助我并告诉我它有什么问题吗?

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

4 回答 4

3

您在第一个循环之前初始化factorSum为,但在尝试每个新的. 这些因素不断加起来,永远不等于要检查的数字。将其重置为循环的开头。0for0n0n for

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

于 2015-04-10T18:22:59.400 回答
1

您的程序有一些问题:

  1. 您需要在遍历因素后重置factorSum0
  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.");
      }
    }
  }
}
于 2015-04-10T18:27:41.233 回答
1

我想您已经这样做了,但无论如何,您代码中的主要问题是“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);
        }
    }
}
于 2016-02-02T23:58:52.270 回答
0

你应该保持这样

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;
}
于 2015-04-10T18:27:58.603 回答