This seems to work ``` >>> from sympy import sqrt, sympify >>> from sympy.simplify.simplify import rad_rationalize >>> def continued_fraction(x, y): ... x = sympify(x) ... v, res = [], [] ... q = x/y ... while 1: ... v.append(q) ... a = int(q) ... res.append(a) ... if q == a: ... return res ... num, den = rad_rationalize(1,(q - a)) ... q = num / den ... if q in v: ... return res ... >>> continued_fraction(sqrt(17) + 5, 4) [2, 3, 1, 1] ```
On Thursday, June 27, 2013 10:16:34 PM UTC+2, Thilina Rathnayake wrote: > > Hi all, > > Is there a function in sympy which gives the continued fraction > representation > of quadratic irrationalities like, (sqrt(17) + 5 ) / 4? > > I found an implementation of a continued fraction function in Shor's > algorithm: > http://docs.sympy.org/dev/modules/physics/quantum/shor.html > > But it doesn't seem to give correct results when irrationalities are > involved. > > >>> from sympy.physics.quantum.shor import continued_fraction >> >>> continued_fraction(sqrt(17)+5, 4) >> [2, 4] >> > > Actual answer should be 2, [3, 1, 1] with the numbers inside [] repeating > forever. > Any help would be appreciated. > > Regards, > Thilina. > . > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.
