{{{id=1|
c = continued_fraction(pi, bits=33); c
///
[3, 7, 15, 1, 292, 2]
}}}
{{{id=2|
c.convergents()
///
[3, 22/7, 333/106, 355/113, 103993/33102, 208341/66317]
}}}
{{{id=10|
[c.pn(n) for n in range(len(c))]
///
[3, 22, 333, 355, 103993, 208341]
}}}
{{{id=11|
[c.qn(n) for n in range(len(c))]
///
[1, 7, 106, 113, 33102, 66317]
}}}
{{{id=9|
for n in range(-1, len(c)):
print c.pn(n)*c.qn(n-1) - c.qn(n)*c.pn(n-1),
///
1 -1 1 -1 1 -1 1
}}}
{{{id=8|
for n in range(len(c)):
print c.pn(n)*c.qn(n-2) - c.qn(n)*c.pn(n-2),
///
3 -7 15 -1 292 -2
}}}
{{{id=3|
c = continued_fraction(golden_ratio,bits=15)
v = [(i, c.pn(i)/c.qn(i)) for i in range(len(c))]
eps=0.5
G = plot(golden_ratio,0,len(v))+line(v,color='grey') + points(v,pointsize=50)
show(G,ymin=golden_ratio-eps, ymax=golden_ratio+eps)
///
}}}
{{{id=4|
x = 92902834/292034827; x
///
92902834/292034827
}}}
{{{id=5|
continued_fraction(x)
///
[0, 3, 6, 1, 33, 1, 14, 1, 4, 1, 1, 2, 1, 1, 6, 4, 1, 4, 2]
}}}
We do repeated long division using the Euclidean algorithm and output the partial quotients. As we proved, they are the convergents in the above continued fraction.
{{{id=6| (a, b) = (x.numerator(), x.denominator()) while True: q, r = a.quo_rem(b) print q, a, b = b, r if r == 0: break /// 0 3 6 1 33 1 14 1 4 1 1 2 1 1 6 4 1 4 2 }}} {{{id=7| /// }}}