euler/python/e108.py

44 lines
1.2 KiB
Python

from fractions import Fraction
from lib_misc import proper_divisors
from math import ceil
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 get_distinct_solutions2(n):
ds = proper_divisors(n * n)
return ceil((len(ds) + 1) / 2)
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_solutions2(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)