Improve 66 in Python based on the idea that we can search the Stern-Brocot tree for a solution.
This commit is contained in:
@@ -62,7 +62,7 @@ def get_fractions(n, x):
|
|||||||
return (n, d)
|
return (n, d)
|
||||||
|
|
||||||
|
|
||||||
def get_minimal_solution(d):
|
def get_minimal_solution_old(d):
|
||||||
for i in range(0, 100):
|
for i in range(0, 100):
|
||||||
x, y = get_fractions(d, i)
|
x, y = get_fractions(d, i)
|
||||||
if x * x - d * y * y == 1:
|
if x * x - d * y * y == 1:
|
||||||
@@ -73,8 +73,32 @@ def is_square(n):
|
|||||||
return math.sqrt(n).is_integer()
|
return math.sqrt(n).is_integer()
|
||||||
|
|
||||||
|
|
||||||
|
def get_minimal_solution(d, d1=0, n1=1, d2=1, n2=1):
|
||||||
|
"""
|
||||||
|
This is an alternative to the alternate fraction approach explained here
|
||||||
|
[1]. Instead of calculating the continued fraction we search the
|
||||||
|
Stern-Brocot tree for fractions (x / y) that satisfy the pell equation.
|
||||||
|
|
||||||
|
If the solution does not satisfy the equation we can searh the respective
|
||||||
|
branch by calculating the mediant between the current fraction and one of
|
||||||
|
the previous fractions.
|
||||||
|
|
||||||
|
[1] https://en.wikipedia.org/wiki/Pell's_equation#Fundamental_solution_via_continued_fractions
|
||||||
|
[2] https://en.wikipedia.org/wiki/Stern–Brocot_tree
|
||||||
|
"""
|
||||||
|
|
||||||
|
x = n1 + n2
|
||||||
|
y = d1 + d2
|
||||||
|
p = x * x - d * y * y
|
||||||
|
if p == 1:
|
||||||
|
return (x, y)
|
||||||
|
if p < 1:
|
||||||
|
return get_minimal_solution(d, d1, n1, y, x)
|
||||||
|
else:
|
||||||
|
return get_minimal_solution(d, y, x, d2, n2)
|
||||||
|
|
||||||
|
|
||||||
def euler_066():
|
def euler_066():
|
||||||
# XXX: This seems really long and complicated. We can do better.
|
|
||||||
x_max = 0
|
x_max = 0
|
||||||
d_max = 0
|
d_max = 0
|
||||||
|
|
||||||
@@ -84,8 +108,7 @@ def euler_066():
|
|||||||
|
|
||||||
x, y = get_minimal_solution(d)
|
x, y = get_minimal_solution(d)
|
||||||
if x > x_max:
|
if x > x_max:
|
||||||
x_max = x
|
x_max, d_max = x, d
|
||||||
d_max = d
|
|
||||||
|
|
||||||
print("d: {} x: {}".format(d_max, x_max))
|
print("d: {} x: {}".format(d_max, x_max))
|
||||||
s = d_max
|
s = d_max
|
||||||
|
|||||||
Reference in New Issue
Block a user