2020-08-13 14:52:47 +00:00
|
|
|
"""
|
2020-10-09 03:16:55 +00:00
|
|
|
Problem 34: https://projecteuler.net/problem=34
|
|
|
|
|
2020-08-13 14:52:47 +00:00
|
|
|
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
|
|
|
|
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
|
|
|
|
Note: As 1! = 1 and 2! = 2 are not sums they are not included.
|
|
|
|
"""
|
|
|
|
|
2020-08-25 11:48:19 +00:00
|
|
|
from math import factorial
|
2020-08-13 14:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sum_of_digit_factorial(n: int) -> int:
|
|
|
|
"""
|
|
|
|
Returns the sum of the digits in n
|
|
|
|
>>> sum_of_digit_factorial(15)
|
|
|
|
121
|
|
|
|
>>> sum_of_digit_factorial(0)
|
|
|
|
1
|
|
|
|
"""
|
2020-08-25 11:48:19 +00:00
|
|
|
return sum(factorial(int(char)) for char in str(n))
|
2020-08-13 14:52:47 +00:00
|
|
|
|
|
|
|
|
2020-10-09 03:16:55 +00:00
|
|
|
def solution() -> int:
|
2020-08-13 14:52:47 +00:00
|
|
|
"""
|
|
|
|
Returns the sum of all numbers whose
|
|
|
|
sum of the factorials of all digits
|
|
|
|
add up to the number itself.
|
2020-10-09 03:16:55 +00:00
|
|
|
>>> solution()
|
2020-08-13 14:52:47 +00:00
|
|
|
40730
|
|
|
|
"""
|
2020-08-25 11:48:19 +00:00
|
|
|
limit = 7 * factorial(9) + 1
|
|
|
|
return sum(i for i in range(3, limit) if sum_of_digit_factorial(i) == i)
|
2020-08-13 14:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-09 03:16:55 +00:00
|
|
|
print(f"{solution()} = ")
|