Problem 16
Power digit sum
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?
幂的数字和
215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26。
21000的各位数字之和是多少?
package projecteuler;
import org.junit.Test;
public class Prj16 {
/**
* 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 2^1000?
*/
@Test
public void test() {
System.out.println(getSum(1000));
}
public int getSum(int bit) {
int[] buf = new int[bit];
buf[bit - 1] = 2;
// 1000 - 1;
for (int i = 0; i < bit - 1; i++) {
for (int iter = 0; iter < bit; iter++) {
buf[iter] *= 2;
}
for( int j = bit - 1; j > 0 ; j --){
int remain = buf[j] % 10;
buf[j - 1] += buf[j] / 10;
buf[j] = remain;
}
}
int sum = 0;
for (int i = 0; i < bit; i++) {
sum += buf[i];
}
return sum;
}
static void print_Arr( int [] arr){
for( int i = 0 ; i < arr.length; i ++){
System.out.println("[" + i + "]=" + arr[i]);
}
}
}