Home Project Euler Teaching Projects Gallery

Problem 30: 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.


Algorthim:

They key part of this algorthim is find the limit of our brute force search. What number should we go up to check for this special property? Consider a 4 digit number. The maximum of digit fifth power is 236196 which has 6 digit. Hence, searching up to 4 digit number isn't enough. How about 5 digits? 5 * 9^5 is 295245 which still has 6 digit. 6 digits? 6 * 9^5 is 354294 which has 6 digits! So, we can search all the way to 6 digits, specifically, 354294.

Code:

Result:

Answer: 443839
Runtime: 2.9 seconds