Digit fifth powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


Idea

Naive solution. The key is the upper bound.

It is always true that $ n * 9^k \lt 10^{n+1}, if \; k \lt 10 $.

So the upper bound for numbers that can be written as the sum of fifth powers of their digits is $ 10^{5+1} $


In [1]:
def digits_power_sum(n, power):
    s = 0
    while n:
        s += pow(n % 10, power)
        n //= 10
    return s
In [2]:
digits_power_sum(1634, 4)
Out[2]:
1634
In [3]:
def solve(power):
    upper_bound = pow(10, (power+1))
    s = 0
    for i in range(2, upper_bound):
        if i == digits_power_sum(i ,power):
            s += i
    return s
In [4]:
solve(4)
Out[4]:
19316
In [5]:
solve(5)
Out[5]:
443839