# http://e...content-available-to-author-only...a.org/wiki/Pell%27s_equation
# http://e...content-available-to-author-only...a.org/wiki/Continued_fraction#Best_rational_approximations

from fractions import *
from math import *


def PellsEquation(convergent, n):
 return (convergent.numerator**2) - n*(convergent.denominator**2)

# The sequence of convergents for the square root of seven are
L = [Fraction(2,1), Fraction(3,1), Fraction(5,2), Fraction(8,3)]

for f in L:
 print(PellsEquation(f,7))

tmp = (sqrt(7)
print(Fraction(tmp).limit_denominator())