feat(eular_5.py):添加计时装饰器和Python内置lcm解决方案

📝 docs(eular_5.py):增加性能测试功能,比较两种实现方式的执行时间
This commit is contained in:
2025-12-15 15:19:14 +08:00
parent be3f920e72
commit 7df59c9bcf

View File

@@ -5,6 +5,18 @@ What is the smallest positive number that is evenly divisible by all of the numb
import math
import random
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} took {end_time - start_time:.6f} seconds")
return result
return wrapper
def is_probable_prime(n: int, trials: int = 10) -> bool:
@@ -54,6 +66,7 @@ def primes_up_to(n: int) -> list[int]:
return primes
@timer
def smallest_multiple(n: int) -> int:
result = 1
for p in primes_up_to(n):
@@ -63,5 +76,12 @@ def smallest_multiple(n: int) -> int:
return result
@timer
def python_solution(n: int) -> int:
ls = list(range(1, n + 1))
return math.lcm(*ls)
if __name__ == "__main__":
print(smallest_multiple(20))
print(python_solution(20))