# 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 = float(sqrt(7))
print(Fraction(tmp).limit_denominator())
IyBodHRwOi8vZS4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4uYS5vcmcvd2lraS9QZWxsJTI3c19lcXVhdGlvbgojIGh0dHA6Ly9lLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5hLm9yZy93aWtpL0NvbnRpbnVlZF9mcmFjdGlvbiNCZXN0X3JhdGlvbmFsX2FwcHJveGltYXRpb25zCgpmcm9tIGZyYWN0aW9ucyBpbXBvcnQgKgpmcm9tIG1hdGggaW1wb3J0ICoKCgpkZWYgUGVsbHNFcXVhdGlvbihjb252ZXJnZW50LCBuKToKIHJldHVybiAoY29udmVyZ2VudC5udW1lcmF0b3IqKjIpIC0gbiooY29udmVyZ2VudC5kZW5vbWluYXRvcioqMikKCiMgVGhlIHNlcXVlbmNlIG9mIGNvbnZlcmdlbnRzIGZvciB0aGUgc3F1YXJlIHJvb3Qgb2Ygc2V2ZW4gYXJlCkwgPSBbRnJhY3Rpb24oMiwxKSwgRnJhY3Rpb24oMywxKSwgRnJhY3Rpb24oNSwyKSwgRnJhY3Rpb24oOCwzKV0KCmZvciBmIGluIEw6CiBwcmludChQZWxsc0VxdWF0aW9uKGYsNykpCgp0bXAgPSBmbG9hdChzcXJ0KDcpKQpwcmludChGcmFjdGlvbih0bXApLmxpbWl0X2Rlbm9taW5hdG9yKCkp