Prob 16: Power digit sum

*Difficulty: Easy

Problem:

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?

How to solve:

Using BigInteger in Java.

There is no type of data in any programming languages that can store a number having hundreds or thousands digits.
Solution took 6ms

Implementation: Java (Very short)

import java.math.BigInteger;

public class Prob16 {

publicstaticvoidmain(String[] args) {

long cur = System.currentTimeMillis();

Stringresult = BigInteger.valueOf(2).pow(1000).toString();

int sumDigits = 0;

for(int index = 0; index < result.length(); ++index) {

sumDigits+= Integer.valueOf(result.charAt(index)+“”);

}

System.out.println(sumDigits);

System.out.println(“Took “ + (System.currentTimeMillis()cur) + ” ms”);

}

}

Advertisements