← Previous | ↑ All problems | Go to Project Euler | Go to GitHub | Next →

Project Euler Problem 16

Power Digit Sum

$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}$?

This is pretty easy. Just compute $2^{1000}$ using BigInt and sum its digits.

function sum_of_digits(n)
    return sum(digits(n))
end

function power_digit_sum(base, exponent)
    big_num = BigInt(base)^exponent
    return sum_of_digits(big_num)
end

It computes the solution in 990.952 ns.

Going a bit further, we find that the sum of the digits of $2^{10^6}$ is 1351546 in 10.017 ms.