Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168742
Image: ubuntu2004
c = continued_fraction(pi, bits=33); c
[3, 7, 15, 1, 292, 2]
c.convergents()
[3, 22/7, 333/106, 355/113, 103993/33102, 208341/66317]
[c.pn(n) for n in range(len(c))]
[3, 22, 333, 355, 103993, 208341]
[c.qn(n) for n in range(len(c))]
[1, 7, 106, 113, 33102, 66317]
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
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
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)
x = 92902834/292034827; x
92902834/292034827
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.

(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