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);
}
}
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);
}
}