Symbol(clack:cancel)

This commit is contained in:
2025-12-26 17:35:14 +08:00
parent 266544ac47
commit 25469e1022
54 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
"""
Sum of primes below 2 million
"""
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} took {end - start:.6f} seconds")
return result
return wrapper
def sieve_primes(max: int) -> list[int]:
primes = [True] * (max + 1)
primes[0:2] = [False, False]
for i in range(2, max + 1):
if primes[i]:
primes[i * i : max : i] = [False] * ((max - 1 - i * i) // i + 1)
return [i for i in range(max) if primes[i]]
@timer
def sum_primes(max: int) -> int:
return sum(sieve_primes(max))
if __name__ == "__main__":
print(sum_primes(2_000_000))