【问题标题】:Find the sum of the digits in the number 100! (My while loop would not stop)求数字 100 中的数字之和! (我的while循环不会停止)
【发布时间】:2011-10-14 16:35:32
【问题描述】:

我一直在尝试解决 Project Euler 中的Problem 20

n!表示 n (n 1) ... 3 * 2 * 1 例如,10! = 10 * 9 ... 3 * 2 * 1 = 3628800, 和数字 10 中的数字之和!是 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27。 求数字 100 中的数字之和!

这是我到目前为止想出的。我已经用这段代码得到了正确的答案(648),但我有点 OC,因为我的代码是一个无限循环。在 while 循环内结果变为 0 后,它就不会停止。谁能帮我解决这个问题?

public static BigInteger problem20(int max){
    BigInteger sum = BigInteger.valueOf(0);
    BigInteger result = BigInteger.valueOf(1);
    BigInteger currentNum = BigInteger.valueOf(0);

    for(long i = 1; i<=max; i++){
        result  = result.multiply(BigInteger.valueOf(i));
        //System.out.println(result);
    }

    while (!result.equals(0)) {
        sum = sum.add(result.mod(BigInteger.valueOf(10)));
        result = result.divide(BigInteger.valueOf(10));
        System.out.println(sum + " "+ result);
    }
    return sum;
}

【问题讨论】:

    标签: java biginteger factorial


    【解决方案1】:

    这就是问题所在:

    while (!result.equals(0))
    

    resultBigInteger,它永远不会等于 Integer。尝试使用

    while (!result.equals(BigInteger.ZERO))
    

    【讨论】:

    • 天啊!我知道它必须在这些地方的某个地方! >_
    【解决方案2】:

    另一种可能是使用while (fact.compareTo(BigInteger.ZERO) &gt; 0)

    我建议您尽可能使用BigInteger.ZEROBigInteger.ONEBigInteger.TEN

    例子:

    import java.math.BigInteger;
    
    public class P20 {
    
        public static void main(String[] args) {
            System.out.println(getSum(100));
        }
    
        private static long getSum(int n) {
            BigInteger fact = BigInteger.ONE;
            for (int i = 2; i <= n; i++) {
                fact = fact.multiply(BigInteger.valueOf(i));
            }
            long sum = 0;
            while (fact.compareTo(BigInteger.ZERO) > 0) {
                sum += fact.mod(BigInteger.TEN).longValue();
                fact = fact.divide(BigInteger.TEN);
            }
            return sum;
        }
    
    }
    

    需要 4 毫秒

    可以通过以下观察来改进:

    • 总和不受零的影响 => 你不需要乘以 10 和 100,而不是 20, 30, ... 使用 2, 3, ... 就足够了。当然,您可以使用以下事实来概括这条规则:5*k * 2*j 可以被10 整除。

    【讨论】:

      【解决方案3】:

      请将您的代码修改为:

          while (!result.equals(BigInteger.valueOf(0))) {
              sum = sum.add(result.mod(BigInteger.valueOf(10)));
              result = result.divide(BigInteger.valueOf(10));
              System.out.println(sum + " "+ result);
          }
      

      【讨论】:

        【解决方案4】:

        这是另一种方法。在这种情况下,计算总和的复杂度是 O(1)。

        import java.math.BigInteger;
        import java.util.ArrayList;
        import java.util.regex.Matcher;
        import java.util.regex.Pattern;
        
        public class Main{
            public static void main(String[] args){
        BigInteger b = BigInteger.valueOf(1);
                for(int i=2;i<=5;i++){
                    b = b.multiply(BigInteger.valueOf(i));
                }
                //System.out.println(b);
        

        计算下面的总和

        final BigInteger NINE = BigInteger.valueOf(9);
                    if(b == BigInteger.ZERO){
                        System.out.println(b);
                    }else if(b.mod(NINE) == BigInteger.ZERO){
                        System.out.println(NINE);
                    }else{
                        System.out.println(b.mod(NINE));
                    }
        
                }`
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-05
          • 2019-06-25
          • 2015-12-30
          • 2021-08-18
          • 1970-01-01
          • 2016-01-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多