Problem 16 of 2的1000次方

来源:互联网 发布:滬深300指数每日数据 编辑:程序博客网 时间:2024/06/14 00:56

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?

http://projecteuler.net/problem=16

import java.math.BigInteger;public class SumDigitsTwo {public static void main(String args[]) {int sum=0;String str=pow("2", 1000);for(int i=0;i<str.length();i++){sum+=Integer.parseInt(str.substring(i,i+1));}System.out.print(sum);}public static String pow(String a,int b){BigInteger bi=new BigInteger(a).pow(1000);return bi.toString();}}

Answer:
1366

原创粉丝点击