Luís Ramalho bio photo

Luís Ramalho

Engineering, Product, Business & Management

Twitter LinkedIn Github Stackoverflow

Powerful digit sum is a problem in which one must find the maximum digital sum considering natural numbers of the form, a^b, where a, b < 100.

Since we are dealing with big numbers, we should use BigIntegers. This library has the method pow() which makes our life easier when calculating powers of BigIntegers.

int max = 0;
BigInteger n;
for (int a = 0; a < 100; a++) {
    for (int b = 0; b < 100; b++) {
        n = BigInteger.valueOf(a).pow(b);
        max = Math.max(digitalSum(n), max);
return max;

Then, it is just a matter of calculating the sum of all the digits in the BigInteger, and returning the maximum.

int sum = 0;
String digits = n.toString();
for (int i = 0; i < digits.length(); i++) {
    sum += (int) digits.charAt(i) - '0';
return sum;

The full solution can be found here.