""" GROWING CONTINUED FRACTIONS A continued fraction with remainder looks like [a,b,c,r], where r is a nonnegative number less than one. We can "grow" this continued fraction by replacing the remainder r by the pair int(r), 1/frac(r) Here is an example, done by hand [1.4142135623730951] [1, 2.4142135623730945] [1, 2, 2.4142135623730985] [1, 2, 2, 2.4142135623730749] Here is another example, done using the code in "growcf.py" listed below. >>> from growcf import * >>> from math import * >>> [sqrt(13)] [3.6055512754639891] >>> growcf(_) [3, 1.6513878188659978] >>> growcf(_) [3, 1, 1.5351837584879953] >>> growcf(_) [3, 1, 1, 1.8685170918213339] NOTE: shortencf below is the inverse of growcf: >>> shortencf([3, 1, 1, 1.8685170918213339]) [3, 1, 1.5351837584879953] """ frac = lambda x: x - int(x) growcf = lambda x: x[:-1] + [int(x[-1]), 1/frac(x[-1]) ] shortencf = lambda x: x[:-2] + [(1 + x[-2]*x[-1])/float(x[-1])] """ NOTES: 1. If x = [a,b] and y = [c,d,e] are lists, then x + y = [a,b,c,d,e] is the list obtained by putting x and y together. 2. x[-1] is the last element of a list. x[-2] is the next to the last element. 3. x[:-1] is the list consisting of everything but the last element. """