Skip to content

Improve Project Euler problem 030 solution 1 #6267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions project_euler/problem_030/sol1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Problem Statement (Digit Fifth Power ): https://projecteuler.net/problem=30
""" Problem Statement (Digit Fifth Powers): https://projecteuler.net/problem=30

Surprisingly there are only three numbers that can be written as the sum of fourth
powers of their digits:
Expand All @@ -13,26 +13,32 @@
Find the sum of all the numbers that can be written as the sum of fifth powers of their
digits.

(9^5)=59,049‬
59049*7=4,13,343 (which is only 6 digit number )
So, number greater than 9,99,999 are rejected
and also 59049*3=1,77,147 (which exceeds the criteria of number being 3 digit)
So, n>999
and hence a bound between (1000,1000000)
9^5 = 59049
59049 * 7 = 413343 (which is only 6 digit number)
So, numbers greater than 999999 are rejected
and also 59049 * 3 = 177147 (which exceeds the criteria of number being 3 digit)
So, number > 999
and hence a number between 1000 and 1000000
"""


def digitsum(s: str) -> int:
DIGITS_FIFTH_POWER = {str(digit): digit**5 for digit in range(10)}


def digits_fifth_powers_sum(number: int) -> int:
"""
>>> all(digitsum(str(i)) == (1 if i == 1 else 0) for i in range(100))
True
>>> digits_fifth_powers_sum(1234)
1300
"""
i = sum(pow(int(c), 5) for c in s)
return i if i == int(s) else 0
return sum(DIGITS_FIFTH_POWER[digit] for digit in str(number))


def solution() -> int:
return sum(digitsum(str(i)) for i in range(1000, 1000000))
return sum(
number
for number in range(1000, 1000000)
if number == digits_fifth_powers_sum(number)
)


if __name__ == "__main__":
Expand Down