求数字 100 中各个数字的总和! (我的 while 循环不会停止)

发布于 2024-12-10 02:25:05 字数 947 浏览 1 评论 0原文

我一直在尝试解决欧拉项目中的问题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;
}

I've been trying to solve Problem 20 in Project Euler:

n! means n (n 1) ... 3 * 2 * 1
For example, 10! = 10 * 9 ... 3 * 2 * 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!

This is what I came up with so far. I already got the correct answer (which was 648) with this code, but I got a bit OC, because my code is an infinite loop. After the result became 0 inside the while loop, it just wouldn't stop. Can anybody help me fix this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久伴你 2024-12-17 02:25:05

这就是问题所在:

while (!result.equals(0))

result 是一个BigInteger,它永远不会等于Integer。尝试使用

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

This is the problem:

while (!result.equals(0))

result is a BigInteger, which will never be equal to an Integer. Try using

while (!result.equals(BigInteger.ZERO))
风尘浪孓 2024-12-17 02:25:05

另一种可能性是使用 while (fact.compareTo(BigInteger.ZERO) > 0)。

我建议您在可能的情况下使用 BigInteger.ZERO、BigInteger.ONE 和 BigInteger.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 整除。

Another possibility is to use while (fact.compareTo(BigInteger.ZERO) > 0).

I recommend you to use BigInteger.ZERO, BigInteger.ONE and BigInteger.TEN where it is possible.

Example:

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

}

It takes 4 ms.

It can be improved using the following observation:

  • the sum is not influenced by zeros => you don't need to multiply by 10 and 100 and instead of 20, 30, ... it's enough to use 2, 3, ... . Of course, you can generalize this rule using the following fact 5*k * 2*j is divisible by 10.
旧夏天 2024-12-17 02:25:05

请将您的代码修改为:

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

Please modify your code to :

    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);
    }
好菇凉咱不稀罕他 2024-12-17 02:25:05

这是执行相同操作的另一种方法。在这种方法中,计算总和的复杂度是 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));
            }

        }`
}

Here is another way of doing the same.In this, the complexity to compute the sum is 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);

computing the sum below

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

        }`
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文