euler/python/e108.py

37 lines
1.0 KiB
Python
Raw Normal View History

2021-07-03 17:07:40 +02:00
from fractions import Fraction
def get_distinct_solutions(n):
""" Get the number of distinct solutions for 1/x + 1/y = 1/n. The idea is
that 1/n*2 + 1/n*2 = 1/n is always a solution. So what we do is
brute-force all 1/x for x in [2, n * 2]. We have a solution if the
difference between 1/n and 1/x has a numerator of 1. """
n_inv = Fraction(1, n)
n_distinct = 0
for x in range(2, n * 2 + 1):
x_inv = Fraction(1, x)
y_inv = n_inv - x_inv
if y_inv.numerator == 1:
n_distinct += 1
return n_distinct
def euler_108():
d_max, n_prev = 0, 0
# I arrived at the starting values empirically by observing the deltas.
for n in range(1260, 1000000, 420):
d = get_distinct_solutions(n)
if d > d_max:
# print("n={} d={} delta={}".format(n, d, n - n_prev))
n_prev = n
d_max = d
if d > 1000:
return n
if __name__ == "__main__":
solution = euler_108()
print("e108.py: " + str(solution))
assert(solution == 180180)