Typesetting and computing continued fractions

Pi

The other day I ran across the following continued fraction for π.

\pi = 3 + \cfrac{1^2}{6+\cfrac{3^2}{6+\cfrac{5^2}{6+\cfrac{7^2}{6+\cdots}}}}

Source: L. J. Lange, An Elegant Continued Fraction for π, The American Mathematical Monthly, Vol. 106, No. 5 (May, 1999), pp. 456-458.

While the continued fraction itself is interesting, I thought I’d use this an example of how to typeset and compute continued fractions.

Typesetting

I imagine there are LaTeX packages that make typesetting continued fractions easier, but the following brute force code worked fine for me:

    \pi = 3 + \cfrac{1^2}{6+\cfrac{3^2}{6+\cfrac{5^2}{6+\cfrac{7^2}{6+\cdots}}}}

This relies on the amsmath package for the \cfrac command.

Computing

Continued fractions of the form

\pi = a_0 + \cfrac{b_1}{a_1+\cfrac{b_2}{a_2 +\cfrac{b_3}{a_3+\cfrac{b_4}{a_4+\cdots}}}}

can be computed via the following recurrence. Define A−1 = 1, A0 = a0, B−1 = 0, and B0 = 1. Then for k ≥ 1 define Ak+1 and Bk+1 by

A_{k+1} = a_{k+1}A_k + b_k A_{k-1} \\ B_{k+1} = a_{k+1}B_k + b_k B_{k-1}

Then the nth convergent the continued fraction is Cn = An / Bn.

The following Python code creates the a and b coefficients for the continued fraction for π above then uses a loop that could be used to evaluate any continued fraction.

    N = 20
    a = [3] + ([6]*N)
    b = [(2*k+1)**2 for k in range(0,N)]
    A = [0]*(N+1)
    B = [0]*(N+1)

    A[-1] = 1
    A[ 0] = a[0]
    B[-1] = 0
    B[ 0] = 1

    for n in range(1, N+1):
        A[n] = a[n]*A[n-1] + b[n-1]*A[n-2]
        B[n] = a[n]*B[n-1] + b[n-1]*B[n-2]
        print( n, A[n], B[n], A[n]/B[n] )

Python uses −1 as a shortcut to the last index of a list. I tack A−1 and B−1 on to the end of the A and B arrays to make the Python code match the math notation. This is either clever or a dirty hack, depending on your perspective.

Back to pi

You may notice that these approximations for π are not particularly good. It’s a trade-off for having a simple pattern to the coefficients. The continued fraction for π that has all b‘s equal to 1 has a complicated set of a‘s with no discernible pattern: 3, 7, 15, 1, 292, 1, 1, etc. However, that continued fraction produces very good approximations. If you replace the first three lines of the code above with that below, you’ll see that four iterations produces an approximation to π good to 10 decimal places.

    N = 4
    a = [3, 7, 15, 1, 292]
    b = [1]*N

3 thoughts on “Typesetting and computing continued fractions

  1. Brad Gilbert (b2gills)

    I played with continued fractions of pi a while ago “Perl 6 Pi and continued fractions”. I didn’t try and come up with generative code like that, opting instead for just reversing the input and calling reduce on that.

    I decided to convert your code to Perl 6.

    my \a = 3, |( 6 xx * );
    my \b = ->{ (2 * $++ + 1)² } … *;
    my (@A,@B);
    @A = a[0], ->{ my \n = ++$; a[n]*@A[n-1] + b[n-1]*(@A[n-2]//1) } … *;
    @B = 1, ->{ my \n = ++$; a[n]*@B[n-1] + b[n-1]*(@B[n-2]//0) } … *;

    my \N = 20;
    for 0..N -> \n {
    put ( n, @A[n], @B[n], @A[n]/@B[n] );
    }

    It gets a bit simpler if I combine the two arrays together, and remove the b array.

    constant pi-continued = ( 3,7,15,1,292,1,1,1 );
    my \AB = gather {
    my \iter = pi-continued.iterator;

    take my ($a1,$b1) = (iter.pull-one,1);
    my ($a2,$b2) = (1,0);

    until IterationEnd =:= my \a = iter.pull-one {
    my $a = a*$a1 + $a2;
    my $b = a*$b1 + $b2;
    NEXT {
    ($a2,$b2) = ($a1,$b1);
    ($a1,$b1) = take ($a,$b);
    }
    }
    }
    for 0..* Z AB -> (\n, (\A,\B)) {
    put (n,A,B,FatRat.new: A,B)
    }

    What I find interesting is that this is not faster than just:
    my \one = FatRat.new(1,1);
    say pi-continued.reverse.reduce: -> \a, \b { one / a + b }
    ( The FatRat is there so that it will do something useful on the list of 250 that I tried it with )

Comments are closed.