euler/python/e243.py
2024-04-06 10:17:05 -04:00

41 lines
896 B
Python

from math import gcd
from fractions import Fraction
def product(xs):
r = 1
for x in xs:
r *= x
return r
def ratio(d):
r = 0
for i in range(1, d):
if gcd(i, d) == 1:
r += 1
return Fraction(r, (d - 1))
def euler_243():
target = Fraction(15499, 94744) # 0.1635881955585578
# The more prime factors, the lower the initial fraction gets. We figure
# out empirically that using primes up to 23 yields results close to the
# target fraction. We then iterate over the multiples to find the
# solution.
factors = [2, 3, 5, 7, 11, 13, 17, 19, 23]
for mul in range(1, 10):
d = mul * product(factors)
r = ratio(d)
if r < target:
return d
return None
if __name__ == "__main__":
solution = euler_243()
print("e243.py: " + str(solution))
assert(solution == 892371480)