【问题标题】:Finding and printing perfect numbers under 10000 (Liang, Intro to Java, Exercise 5.33)查找和打印 10000 以下的完美数(Liang,Java 简介,练习 5.33)
【发布时间】:2015-06-16 13:41:56
【问题描述】:

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

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

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

【问题讨论】:

  • 我认为你需要在外循环完成每次迭代后创建factorSum =0

标签: java netbeans perfect-numbers


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你的程序有一些问题:

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

    【讨论】:

    • 谢谢,这是一个很好的答案。会竖起大拇指,但还没有足够的代表,所以这个评论将是一个临时的替代品
    【解决方案3】:

    我想您已经这样做了,但无论如何,您代码中的主要问题是“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);
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      你应该保持这样

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 1970-01-01
        • 2021-01-20
        相关资源
        最近更新 更多