From 5f7bb3e9f7b458d039e1027b48b3d24d9a577f96 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 25 Oct 2021 11:03:22 +0300 Subject: [PATCH] Improve Project Euler problem 034 solution 1 (#5165) --- project_euler/problem_034/sol1.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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: