Solve problem 243.

main
felixm 2024-04-06 10:17:05 -04:00
parent 0509ee4f7a
commit 927cd2011b
1 changed files with 40 additions and 0 deletions

40
python/e243.py Normal file
View File

@ -0,0 +1,40 @@
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)