Ask Your Question

Revision history [back]

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)