简体   繁体   English

在Java中找到数字的总和

[英]Find the sum of the digits of a number in Java

I'm new to Java and I've come up with a solution for a Euler Problem 我是Java的新手,并且为欧拉问题提供了解决方案

public class Problem16{

    public static void main(String[] args){
        System.out.println(DecomposeNumber(PowerDigitSum(1000)));
    }// end main

    public static double PowerDigitSum(double iExposant){
        return Math.pow(2, iExposant);
    }// end PowerDigitSum()

    public static int DecomposeNumber(double iNb){
        int[] intTab = String.valueOf(iNb).chars().map(Character::getNumericValue).toArray();
        int iMax = intTab.length;
        int sum = 0;
            for(int i = 0; i < iMax; i++){
                sum += intTab[i];
            }
            return sum + 1;
    }// end DecomposeNumber()

}// end classe

What I don't understand is my solution works for small numbers such as n^15 but not n^1000 for example, I understand it doesn't work because the number isn't a full set of digits but is there a way to convert let's say 1.071509e+301 into full digits? 我不明白我的解决方案适用于较小的数字,例如n ^ 15但不适用于n ^ 1000,我知道它不起作用,因为数字不是完整的数字,但是有一种方法可以假设将1.071509e + 301转换为完整数字?

If I got your question right, you want to print a big double without scientific notation. 如果我的问题正确,那么您希望打印出没有科学符号的大字体。 you can do that using String.format() . 您可以使用String.format()做到这一点。

String stringNumber = String.format("%f", iNb);

While the other answer certainly solves a part of your problem. 虽然其他答案肯定可以解决您的部分问题。 I don't recommend using the double type for a computation as such of 2 ^ 1000 . 我不建议将double类型用于2 ^ 1000这样的计算。

The answer to your problem is to utilise BigInteger . 您问题的答案是利用BigInteger As a hint, I will provide the logic for the PowerDigitSum method and I'll leave the rest of the logic as an exercise as you're trying to learn from it. 提示一下,我将为PowerDigitSum方法提供逻辑,在您尝试从中学习时,将其余逻辑PowerDigitSum练习。

The function PowerDigitSum now becomes: 函数PowerDigitSum现在变为:

public static BigInteger powerDigitSum(int exponent){
      return BigInteger.TWO.pow(exponent); // 2 ^ exponent
}

Note, if you're going to proceed with this approach (which I hope you do) then after the call to map in the stream pipeline inside the DecomposeNumber method, you can simply call .sum() and return the result instead of toArray() , this means you don't need to perform the subsequent loop to sum the digits. 请注意,如果您打算继续使用这种方法(希望这样做),那么在DecomposeNumber方法内部的流管道中调用map后,您可以简单地调用.sum()并返回结果,而不是toArray() ,这意味着您无需执行后续循环即可对数字求和。

Here is an implementation using BigInteger and no string conversions. 这是使用BigInteger的实现,没有任何字符串转换。 As it turns out, modulo of large double values is no better than the string approach. 事实证明,大double值的取模并不比字符串方法好。

Note that this is actually the basis of a string conversion, without the overhead that isn't needed in this case. 请注意,这实际上是字符串转换的基础,没有这种情况下不需要的开销。

int sumDigits(BigInteger number) {
    int sum = 0;

    while (number.compareTo(BigInteger.ZERO) > 0) {
        sum += number.mod(BigInteger.TEN).intValue();
        number = number.divide(BigInteger.TEN);
    }

    return sum;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM