PyPE solution to PE problem 30
PyPE solution to PE problem 30
Blueprint information
- Status:
- Started
- Approver:
- None
- Priority:
- Undefined
- Drafter:
- None
- Direction:
- Needs approval
- Assignee:
- None
- Definition:
- Approved
- Series goal:
- None
- Implementation:
- Beta Available
- Milestone target:
- None
- Started by
- Scott Armitage
- Completed by
Whiteboard
ProjectEule
===
Surprisingly there are only three numbers that can be written as the sum of
fourth powers of their digits:
1634 = 1^(4) + 6^(4) + 3^(4) + 4^(4)
8208 = 8^(4) + 2^(4) + 0^(4) + 8^(4)
9474 = 9^(4) + 4^(4) + 7^(4) + 4^(4)
As 1 = 1^(4) 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
--------
\begin{quote}
05 Feb 2006 12:14 pm
Silverfish (Python) Silverfish is from England
I've taken a different approach to this. Instead of going through all
the numbers from 1 to 999999, I've throught about the sets of digits
of each number. For example, 123456 has the set {1,2,3,4,5,6}. Any two
numbers with the same digits set has the same sum of fifth powers of
digits, so there can only be one member of our sum for each set of
digits.
If the sum of fifth powers of a particular digit set s, has has the set
of digits s, then the sum of powers is equal to the sum of its digits.
For example, if the sum of the digits 1,2,3,4,5,6 is 213456, then
213456 is equal to the sum of its digits.
digits extracts the number of digits of n, and digits2 the actual digits
as a list, with 0 entries to make them up to 6 digits. sort sorts lists.
These are then included in solve which actually solves the problem. It
goes through each ordering of lists, represented as an increasing
sequence, or list. It sorts the digits of the sum of the numbers in it,
and compares these digits to that of the original set, including the sum
of fifth powers to the sum if they agree.
The sorting is just a convenient way of allowing me to compare lists
without thinking about order. The sum is just to extract the actual
values of the sums of fifth powers, and sum them, as solve() returns
a list of lists, each list having the digits that satify the condition,
and the sum of fifth powers of the digits as the 7th element.
\end{quote}
Answer
------
443839