# Recovering numbers from continued fraction

Okay, not really, since we won't type in infinitely many numbers. But it would be nice to have a way to convert [1,1,1,1,1,1,1,1,1] to a rational approximation of the golden ratio. I couldn't find anything built-in, but maybe I didn't look hard enough?

edit retag close merge delete

Sort by » oldest newest most voted

It used to be that continued_fraction could only deal with finite continued fraction expansions. Thanks to work made in 2014 by Vincent Delecroix and other authors/reviewers, Sage deals with ultimately periodic CF expansions (i.e., quadratic irrationals). If you look at the documentation of continued_fraction?, you will see this example:

sage: cf = continued_fraction([(1,1),(2,8)]); cf
[1; 1, (2, 8)*]
sage: cf.value()
2/11*sqrt5 + 14/11


Therefore, to construct the golden ratio as a quadratic number from the CF expansion you do:

sage: preperiod = ()
sage: period = (1,)
sage: cf = continued_fraction([preperiod, period])
sage: cf
[(1)*]
sage: cf.value()
1/2*sqrt5 + 1/2
sage: cf.value().parent()
Number Field in sqrt5 with defining polynomial x^2 - 5
sage: cf.value().n()
1.61803398874989


Conversely, starting from a quatratic irrational, you may find the preperiod and period of its CF expansion:

sage: K.<sqrt13> = NumberField(x^2-13, embedding=3.5)
sage: continued_fraction(sqrt13/6+1/5)
[0; 1, (4, 43, 30, 43, 4, 3, 2, 42, 1, 4, 1, 1080, 1, 4, 1, 42, 2, 3)*]
sage: continued_fraction(sqrt13/6+1/5).preperiod()
(0, 1)
sage: continued_fraction(sqrt13/6+1/5).period()
(4, 43, 30, 43, 4, 3, 2, 42, 1, 4, 1, 1080, 1, 4, 1, 42, 2, 3)

more

What's wrong with this :

sage: continued_fraction(*12).n()
1.61805555555556
sage: continued_fraction(*12)._rational_()
233/144
sage: continued_fraction(*12)._rational_().n()
1.61805555555556

more

Hmm, I guess so and I should have thought of this, but that wasn't exactly what I meant.