diff --git a/project_euler/problem_034/sol1.py b/project_euler/problem_034/sol1.py index 11c84ab96..8d8432dbb 100644 --- a/project_euler/problem_034/sol1.py +++ b/project_euler/problem_034/sol1.py @@ -8,6 +8,8 @@ Note: As 1! = 1 and 2! = 2 are not sums they are not included. from math import factorial +DIGIT_FACTORIAL = {str(d): factorial(d) for d in range(10)} + 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) 1 """ - return sum(factorial(int(char)) for char in str(n)) + return sum(DIGIT_FACTORIAL[d] for d in str(n)) def solution() -> int: