16 Power digit sum - Project Euler

来源:互联网 发布:手机散热软件 编辑:程序博客网 时间:2024/06/10 03:03
package xxx.xxx.xxx;


import java.math.BigInteger;


/*
 * 2^15 = 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?
 */
public class PowerDigitSum {

public PowerDigitSum(int value, int exp){
System.out.println(this.sum(this.pow(value, exp)));
}

private BigInteger pow(int value, int exp){
BigInteger result = BigInteger.ONE;
int count = 1;
while(count<=exp){
result = result.multiply(BigInteger.valueOf(value));
count++;
}

return result;
}

private int sum(BigInteger result){
char[] strResult = result.toString().toCharArray();
int sum = 0;
for(char c: strResult){
sum+=Integer.parseInt(String.valueOf(c));
}
return sum;
}


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PowerDigitSum powerDigitSum = new PowerDigitSum(2,1000);
}


}
0 0
原创粉丝点击