Project Euler : Problem 16 - Power digit sum

less than 1 minute read

Problem Statement : Power digit sum

Problem 16 : and the sum of its digits is . What is the sum of the digits of the number ?

Concept and Theory

This is a fairly easy problem. One can use any modern language and solve it directly.

Code

public static int powerDigitSum(int number, int power){
    BigInteger val = new BigInteger(new String(""+number));
    val = val.pow(power);

    String num = val.toString();

    int result = 0;

    for (int j = 0; j < num.length(); ++j) {
        char c = num.charAt(j);
        result += (c-'0');
    }

    return result;
}

Test Your Skills

Wanna try a harder version of the above problem ? Check this HackerRank problem.

Solution

The solution will remain same as given in the above mentioned post, , you just need to call it for each test case.

References and Further Readings

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...