215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?
/**
* I don't have a good idea.
*
* I just calculate the result of 2 to the 1000th power.
*
* Then get the summary of each digits.
*
*/
public static int twoPower(int index) {
BigDecimal result = BigDecimal.ONE;
BigDecimal two = BigDecimal.ONE.add(BigDecimal.ONE);
for (int i = 0; i < index; i++) {
result = result.multiply(two);
}
String s = result.toString();
int sum = 0;
System.out.print(s);
for (char c : s.toCharArray()) {
sum += (c - '0');
}
return sum;
}
本文介绍了一种计算2的1000次方后的数字各位相加的方法。通过使用`BigDecimal`类型进行精确计算,避免了大数运算中的溢出问题。文章提供了一个具体实现方案,即先计算指数结果,再逐位累加得到最终答案。
1292

被折叠的 条评论
为什么被折叠?



