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


In [56]:
5*9**5


Out[56]:
295245

In [62]:
def calc(p):
    n = {str(i):i**p for i in range(0, 10)}
    a = []
    for i in range(2, len(str(p*9**p))*(9**p)):
        s = sum(n[i] for i in str(i))
        if s == i and i > 9:
            print(i)
            a.append(i)
    return sum(a)

In [63]:
calc(4)


1634
8208
9474
Out[63]:
19316

In [64]:
calc(5)


4150
4151
54748
92727
93084
194979
Out[64]:
443839