# Typesetting and computing continued fractions

## Pi

The other day I ran across the following continued fraction for π. 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^3}{6+\cfrac{7^2}{6+\cdots}}}}

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

## Computing

Continued fractions of the form 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 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 =  + (*N)
b = [(2*k+1)**2 for k in range(0,N)]
A = *(N+1)
B = *(N+1)

A[-1] = 1
A[ 0] = a
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 = *N


## 5 thoughts on “Typesetting and computing continued fractions”

1. JDS Purohit

Just curious. How do you generate your image dealing with math expressions.

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, ->{ 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 )
{"ft_lang_label":"__label__en","ft_lang_prob":0.84391636,"math_prob":0.99251187,"size":3518,"snap":"2020-45-2020-50","text_gpt3_token_len":1183,"char_repetition_ratio":0.12749004,"word_repetition_ratio":0.0030487804,"special_character_ratio":0.3757817,"punctuation_ratio":0.15393795,"nsfw_num_words":1,"has_unicode_error":false,"math_prob_llama3":0.9985681,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,9,null,9,null,9,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-28T06:23:39Z\",\"WARC-Record-ID\":\"<urn:uuid:80759471-dbd3-4c5e-a23a-1e7481902ebd>\",\"Content-Length\":\"45247\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a913af6c-c0b7-4055-bc42-b74c78955aaa>\",\"WARC-Concurrent-To\":\"<urn:uuid:61370401-f5b5-4bd1-bc5d-90bb0882d312>\",\"WARC-IP-Address\":\"74.208.236.113\",\"WARC-Target-URI\":\"https://www.johndcook.com/blog/2015/12/15/continued-fractions/\",\"WARC-Payload-Digest\":\"sha1:JJKHBBHBIT4BG2EFPEK7QKD55WFYQA64\",\"WARC-Block-Digest\":\"sha1:KPIGHBTY3YWV6DE5JFM5UKXK3BEECH7H\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107896778.71_warc_CC-MAIN-20201028044037-20201028074037-00653.warc.gz\"}"}