fork download
  1. # http://e...content-available-to-author-only...a.org/wiki/Pell%27s_equation
  2. # http://e...content-available-to-author-only...a.org/wiki/Continued_fraction#Best_rational_approximations
  3.  
  4. from fractions import *
  5. from math import *
  6.  
  7.  
  8. def PellsEquation(convergent, n):
  9. return (convergent.numerator**2) - n*(convergent.denominator**2)
  10.  
  11. # The sequence of convergents for the square root of seven are
  12. L = [Fraction(2,1), Fraction(3,1), Fraction(5,2), Fraction(8,3)]
  13.  
  14. for f in L:
  15. print(PellsEquation(f,7))
  16.  
  17. tmp = (sqrt(7)
  18. print(Fraction(tmp).limit_denominator())
Runtime error #stdin #stdout 0.03s 5852KB
stdin
Standard input is empty
stdout
Standard output is empty