Java 2^1000 数字的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/377361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:02:24  来源:igfitidea点击:

Sum of the digits of the number 2^1000

javaalgorithm

提问by DonX

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

2^15 = 32768,其数字之和为 3 + 2 + 7 + 6 + 8 = 26。

What is the sum of the digits of the number 2 power of 1000 (2^1000)?

1000 (2^1000) 的 2 次方数字之和是多少?

Can anyone provide the solution or algorithm for this problem in java?

任何人都可以在java中为这个问题提供解决方案或算法吗?

回答by Boojum

I won't provide code, but java.math.BigIntegershould make this trivial.

我不会提供代码,但java.math.BigInteger应该让这变得微不足道。

回答by soulmerge

2^1000 is a very large value, you would have to use BigIntegers. The algorithm would be something like:

2^1000 是一个非常大的值,您必须使用 BigIntegers。该算法将类似于:

import java.math.BigInteger;
BigInteger two = new BigInteger("2");
BigInteger value = two.pow(1000);
int sum = 0;
while (value > 0) {
  sum += value.remainder(new BigInteger("10"));
  value = value.divide(new BigInteger("10"));
}

回答by Andreas Petersson

something like that sould do it bute force: - although there is a nice analytic solution (think pen& paper) using mathematics - that may also work for numbers greater than 1000.

像这样的东西应该可以做到:-尽管使用数学有一个很好的分析解决方案(想想笔和纸)-它也可能适用于大于 1000 的数字。

    final String bignumber = BigInteger.valueOf(2).pow(1000).toString(10);
    long result = 0;
    for (int i = 0; i < bignumber.length(); i++) {
        result += Integer.valueOf(String.valueOf(bignumber.charAt(i)));
    }
    System.out.println("result: " + result);

回答by Steve McLeod

How can 2^1000 be alternatively expressed?

2^1000 如何交替表示?

I don't remember much from my maths days, but perhaps something like (2^(2^500))? And how can that be expressed?

我不太记得我的数学时代,但也许像 (2^(2^500)) 这样的东西?怎么表达呢?

Find an easy way to calculate 2^1000, put the result in a BigInteger, and the rest is perhaps trivial.

找到一个简单的方法来计算 2^1000,把结果放在一个 BigInteger 中,剩下的可能就不重要了。

回答by bruno conde

Here is my solution:

这是我的解决方案:

public static void main(String[] args) {

    ArrayList<Integer> n = myPow(2, 100);

    int result = 0;
    for (Integer i : n) {
        result += i;
    }

    System.out.println(result);
}

public static ArrayList<Integer> myPow(int n, int p) {
    ArrayList<Integer> nl = new ArrayList<Integer>();
    for (char c : Integer.toString(n).toCharArray()) {
        nl.add(c - 48);
    }

    for (int i = 1; i < p; i++) {
        nl = mySum(nl, nl);
    }

    return nl;
}

public static ArrayList<Integer> mySum(ArrayList<Integer> n1, ArrayList<Integer> n2) {
    ArrayList<Integer> result = new ArrayList<Integer>();

    int carry = 0;

    int max = Math.max(n1.size(), n2.size());
    if (n1.size() != max)
        n1 = normalizeList(n1, max);
    if (n2.size() != max)
        n2 = normalizeList(n2, max);

    for (int i = max - 1; i >= 0; i--) {
        int n = n1.get(i) + n2.get(i) + carry;
        carry = 0;
        if (n > 9) {
            String s = Integer.toString(n);
            carry = s.charAt(0) - 48;
            result.add(0, s.charAt(s.length() - 1) - 48);
        } else
            result.add(0, n);
    }

    if (carry != 0)
        result.add(0, carry);

    return result;
}

public static ArrayList<Integer> normalizeList(ArrayList<Integer> l, int max) {
    int newSize = max - l.size();
    for (int i = 0; i < newSize; i++) {
        l.add(0, 0);
    }
    return l;
}

This code can be improved in many ways ... it was just to prove you can perfectly do it without BigInts.

这段代码可以在很多方面得到改进……这只是为了证明你可以在没有 BigInts 的情况下完美地完成它。

The catch is to transform each number to a list. That way you can do basic sums like:

问题是将每个数字转换为一个列表。这样你就可以做基本的求和,比如:

123456
+   45
______
123501

回答by ya23

Alternatively, you could grab a double and manipulate its bits. With numbers that are the power of 2, you won't have truncation errors. Then you can convert it to string.

或者,您可以抓取一个 double 并操纵它的 bits。如果数字是 2 的幂,则不会出现截断错误。然后您可以将其转换为字符串。

Having that said, it's still a brute-force approach. There must be a nice, mathematical way to make it without actually generating a number.

话虽如此,它仍然是一种蛮力方法。必须有一种很好的数学方法来制作它而无需实际生成数字。

回答by Bill Zeller

This problem is not simply asking you how to find the nearest big integer library, so I'd avoid that solution. This page has a good overviewof this particular problem.

这个问题不仅仅是问你如何找到最近的大整数库,所以我会避免这个解决方案。这个页面很好地概述了这个特定问题。

回答by kennytm

In[1162] := Plus @@ IntegerDigits[2^1000]
Out[1162] = 1366

回答by Aniket Sawant

Here is my code... Please provide the necessary arguments to run this code.

这是我的代码...请提供运行此代码所需的参数。

import java.math.BigInteger;

导入 java.math.BigInteger;

public class Question1 {
    private static int SumOfDigits(BigInteger inputDigit) {
        int sum = 0;
        while(inputDigit.bitLength() > 0) {
            sum += inputDigit.remainder(new BigInteger("10")).intValue();
            inputDigit = inputDigit.divide(new BigInteger("10"));       
        }                       
        return sum;
    }

    public static void main(String[] args) {
        BigInteger baseNumber = new BigInteger(args[0]);
        int powerNumber = Integer.parseInt(args[1]);
        BigInteger powerResult = baseNumber.pow(powerNumber);
        System.out.println(baseNumber + "^" + powerNumber + " = " + powerResult);
        System.out.println("Sum of Digits = " + Question1.SumOfDigits(powerResult));
    }

}    

回答by tony

int result = 0;

String val = BigInteger.valueOf(2).pow(1000).toString();

for(char a : val.toCharArray()){
    result = result + Character.getNumericValue(a);
}
System.out.println("val ==>" + result);

It's pretty simple if you know how to use the biginteger.

如果您知道如何使用 biginteger,这将非常简单。