新增欧拉第34、35题解法及说明

This commit is contained in:
2026-01-04 22:21:57 +08:00
parent 0be6fed37c
commit 0ea015f3af
5 changed files with 367 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
"""
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.
"""
import time
from functools import lru_cache
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} time: {end_time - start_time:.6f} seconds")
return result
return wrapper
@lru_cache(maxsize=None)
def factorial(n: int) -> int:
if n == 0:
return 1
elif n == 1:
return 1
elif n == 2:
return 2
elif n == 3:
return 6
elif n == 4:
return 24
elif n == 5:
return 120
elif n == 6:
return 720
elif n == 7:
return 5040
elif n == 8:
return 40320
elif n == 9:
return 362880
elif n == 10:
return 3628800
else:
return n * factorial(n - 1)
def sum_digits(n: int) -> int:
tmp = str(n)
return sum(factorial(int(digit)) for digit in tmp)
@timer
def main() -> None:
res = list()
for i in range(12, 9999999):
if sum_digits(i) == i:
res.append(i)
print(sum(res))
if __name__ == "__main__":
main()

8
solutions/0034/readme.md Normal file
View File

@@ -0,0 +1,8 @@
参考 [Factorion](https://mathworld.wolfram.com/Factorion.html)
---
本来想的是是否可以确定满足这个规则的数列上限。口算估计是999999然后就是暴力计算了。
看到结果则比较让我惊讶这样的数只有两个145和40585。
再加上看别人的思考,从我个人来说,可能不是所有问题都适合这个思路?