Project Euler Problem 30
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: As 1 = 1^4 is
Solution
Answer: 443839
We seek all integers that can be written as the sum of the fifth powers of their digits.
1. Mathematical analysis
We first determine an upper bound.
For an $n$-digit number, the maximum possible sum of fifth powers of its digits is:
$$n \cdot 9^5$$
Since:
$$9^5 = 59049$$
Check when an $n$-digit number can no longer equal such a sum:
- $6 \cdot 9^5 = 354294$
- A 7-digit number is at least $1{,}000{,}000$
But:
$$7 \cdot 9^5 = 413343 < 1{,}000{,}000$$
So no 7-digit number can satisfy the condition. Therefore we only need to search up to:
$$354294$$
excluding $1$, since it is not considered a sum.
2. Clean Python code
def digit_fifth_power_sum():
limit = 6 * (9 ** 5) # 354294
total = 0
for n in range(2, limit + 1):
if n == sum(int(d) ** 5 for d in str(n)):
total += n
return total
print(digit_fifth_power_sum())
3. Mental trace / correctness check
The numbers that satisfy the property are:
- 4150
- 4151
- 54748
- 92727
- 93084
- 194979
Check one example:
$$4150 = 4^5 + 1^5 + 5^5 + 0^5$$
$$= 1024 + 1 + 3125 + 0 = 4150$$
Summing them:
$$4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839$$
Answer: 443839