Continued fractions (连分数)¶
Continued fractions - Diophantine approximation (sagemath.org)
在 python / sage math 中,连分数的表示方式如下:
$[a_0;a_1,a_2,\ldots]=a_0+\frac1{[a_1;a_2,\ldots]}=a_0+\frac1{a_1+\frac1{a_2+\frac1{\ldots}}}$
In [7]:
Copied!
# 创建一个正常的分数 normal fraction
nf = Rational(13/27)
nf, type(nf)
# 创建一个正常的分数 normal fraction
nf = Rational(13/27)
nf, type(nf)
Out[7]:
(13/27, <class 'sage.rings.rational.Rational'>)
In [6]:
Copied!
cf = nf.continued_fraction()
cf, type(cf)
cf = nf.continued_fraction()
cf, type(cf)
Out[6]:
([0; 2, 13], <class 'sage.rings.continued_fraction.ContinuedFraction_periodic'>)
cf 还有很多属性和方法,构建方法也有很多,按下不表,记住 cf.value()
即可。
In [10]:
Copied!
cf.quotients(), cf.value(), cf.period(), cf.preperiod()
cf.quotients(), cf.value(), cf.period(), cf.preperiod()
Out[10]:
([0, 2, 13], 13/27, (), (0, 2, 13))