Project Euler Problem 30 Statement
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.
Solution
These numbers differ from Armstrong numbers (or narcissistic numbers) by allowing more or fewer digits than the exponent.
Brute-force, simple search solution
The easiest and fastest way to solve this problem is to use brute force which solves this and the HackerRank version within the time limit.
print (sum(i for i in range(2, 5*9**5) if i==sum(int(j)**5 for j in str(i))))
We only need to check up to a certain limit because, beyond a certain point, numbers become too large to be written as the sum of their digit powers. This is calculated as: n*9**n, n=5
Using Combinations
In the problem statement, 1634 is presented as the first example of a number that satisfies the requirement of being equal to the sum of the fourth powers of its digits. It becomes clear that different permutations of 1634 (such as 1643, 1346, 1364, and 20 others) will all yield the same sum when their digits are raised to the fourth power. We can use this knowledge to only have one representative from this group instead of 23 thereby eliminating redundancy.
For instance, 1634 or 6431 will NOT be in the set created by combinations_with_replacement
. Only the lowest variant 1346 will. But, when we check pow_digits(1346)=1634
and it equals pow_digits(pow_digits(1346))=1634
then 1634 is a hit.
Program Explanation
The program can be broken down as follows:pow_digits()
: First, we need a helper function that computes the sum of each digit in a number raised to the n-th power. We use a pre-calculated list, q[], for the powers to increase efficency.- Check Each Number in the Range: For each combination, t, we calculate the sum of its digits raised to the n-th power using
pow_digits()
. If this sum equals t itself, then t is a number we are looking for. - Sum the Numbers Found: The final result is the sum of all numbers that meet this criterion. We subtract 1 from the sum to remove the 1n case as instructed.
HackerRank version
HackerRank varies the exponent to 3, 4, 5, or 6.
Python Source Code
import itertools
exp, s, x = int(input()), 0, set()
q = [pow(d,exp) for d in range(10)]
pow_digits = lambda n: sum(q[int(d)] for d in n)
for cx in itertools.combinations_with_replacement('0123456789', exp+1):
t = pow_digits(cx)
if t == pow_digits(str(t)): x.add(t)
print (sum(x)-1)
Last Word
- Reference: The On-Line Encyclopedia of Integer Sequences (OEIS) A052455: Fixed points for operation of repeatedly replacing a number by the sum of the fourth power of its digits.
- With a little further searching, I found:
Reference: The On-Line Encyclopedia of Integer Sequences (OEIS) A052464: Fixed points for operation of repeatedly replacing a number by the sum of the fifth power of its digits.
Digit Power | Number Set |
---|---|
2 | *NONE* |
3 | 153, 370, 371, 407 |
4 | 1634, 8208, 9474 |
5 | 4150, 4151, 54748, 92727, 93084, 194979 |
6 | 548834 |
7 | 1741725, 4210818, 9800817, 9926315, 14459929 |
8 | 24678050, 24678051, 88593477 |
9 | 146511208, 472335975, 534494836, 912985153 |
10 | 4679307774 |
11 | 32164049650, 40028394225, 42678290603, 44708635679, 49388550606, 32164049651, 82693916578, 94204591914 |
12 | *NONE* |
13 | 564240140138 |
14 | 28116440335967 |
15 | *NONE* |
16 | 4338281769391370, 4338281769391371 |
17 | 233411150132317, 21897142587612075, 35641594208964132, 35875699062250035 |
18 | *NONE* |
19 | 1517841543307505039, 3289582984443187032, 4498128791164624869, 4929273885928088826 |
20 | 63105425988599693916 |
21 | 449177399146038697307, 128468643043731391252, 577646042189770088559 |
22 | *NONE* |
23 | 27907865009977052567814, 35452590104031691935943, 27879694893054074471405, 28361281321319229463398, 21887696841122916288858 |
24 | 174088005938065293023722, 239313664430041569350093, 188451485447897896036875, 601853155816004758410690 |
25 | 832662335985815242605070, 1550475334214501539088894, 832662335985815242605071, 3706907995955475988644380, 4422095118095899619457938, 3706907995955475988644381, 114735624485461118832514, 1553242162893771850669378 |