Improve Project Euler problem 034 solution 1 (#5165)

This commit is contained in:
Maxim Smolskiy 2021-10-25 11:03:22 +03:00 committed by GitHub
parent ba71005484
commit 5f7bb3e9f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,8 @@ Note: As 1! = 1 and 2! = 2 are not sums they are not included.
from math import factorial from math import factorial
DIGIT_FACTORIAL = {str(d): factorial(d) for d in range(10)}
def sum_of_digit_factorial(n: int) -> int: def sum_of_digit_factorial(n: int) -> int:
""" """
@ -17,7 +19,7 @@ def sum_of_digit_factorial(n: int) -> int:
>>> sum_of_digit_factorial(0) >>> sum_of_digit_factorial(0)
1 1
""" """
return sum(factorial(int(char)) for char in str(n)) return sum(DIGIT_FACTORIAL[d] for d in str(n))
def solution() -> int: def solution() -> int: