euler/python/e204.py

39 lines
931 B
Python
Raw Normal View History

2024-04-25 03:39:34 +02:00
from lib_prime import primes
from functools import lru_cache
@lru_cache
def next_prime(n):
if n == 1:
return 2
ps = primes(105)
assert n < ps[-1]
for i, p in enumerate(ps):
if p == n and i + 1 < len(ps):
return ps[i + 1]
assert False
def count(current_product, max_product, current_prime, max_prime):
if current_product > max_product:
return 0
r = 1
if current_prime > 1:
r += count(current_product * current_prime, max_product, current_prime, max_prime)
while (current_prime := next_prime(current_prime)) <= max_prime:
r += count(current_product * current_prime, max_product, current_prime, max_prime)
return r
def euler_204():
assert count(1, 10**8, 1, 5) == 1105
return count(1, 10**9, 1, 100)
if __name__ == "__main__":
solution = euler_204()
print("e204.py: " + str(solution))
assert solution == 2944730