# Continued Fractions Continued. It seems to me that continued fractions (CFs) are perhaps too advanced for ‘A’ levels and too elementary for a degree maths course and are perhaps undervalued or ignored in schools and universities? Since my last post about fractions I’ve looked a little more at CFs and found they have applications ranging from factorising large numbers to gear ratio calculations. And they’re really interesting when their layers are peeled away with a bit of Haskell. So, a bit of playing with numbers and a bit of Haskelling- what’s not to like?

Let’s start with a fraction, any fraction – say 103/24 and in CF form this is and using the list notation this is [4,3,2,3]
The Haskell code to derive a (finite) CFList is quite simple. (The type CFList is just a synonym for [Integer])

it just builds up the list of terms by repeated application of ‘divMod‘ and terminates when the remainder is zero.
Note that the previous post used a list format for CFs but that was from the perspective of CFs that never terminated – e.g. CFs for square roots. The lists here are, at the moment, finite and represent rational numbers.
If we wanted the fractions 2/2, 2/3, 2/4, 2/5 … 2/20 in list form. Then map toCFList . map (\n -> F 2 (Numbr n)) \$ [2..10] gives

which has a very pleasing pattern to it.

Another useful aspect of a CF is the idea of a convergent which we looked at in a previous post when approximating square roots. The convergents are esentailly the ‘partial sums’ of the CF. And for finite CFs the list of convergents is finite and the final convergent is, of course, the original fraction.
For example

gives

and each convergent is a better approximation to the original fraction. Initially, to calculate convergents, I wrote this function

which involved a slightly awkward creation of a FracStruc after creating a CFList representation of the Fraction.
A much simpler and direct way is to calculate the convergents using a recurrence relationship described here.

Where CFList is just a list of Integer and Convergents is a synonym for a list of Fraction. This shows the elegance of Haskell in the way the terms list is built up recursively with ‘two views’ of the same list one view ‘terms‘ ‘out of step’ with the second view – ‘tail terms‘. And the zipWith3 function pulls it all together using the nextConv function. Finally the list is mapped over to convert (p, q) into a Fraction! 🙂

We can examine how close each convergent is to the original Fraction with a function like this:

Playing with these in GHCI:

Notice that the last convergent is the original fraction – ‘all the partial sums have been added together’.
We can see the error for each convergent by applying

and notice that the error, whilst decreasing, also alternates either side of the convergents. Also the convergents of a CF give the best rational approximation to that fraction and any better approximating fraction would need a larger denominator than the convergent.

## A Simple Application

A practical application of CFs is in calculating gear ratios. Christian Huygens used CFs back in 1700 to calculate the gears needed to build a planetarium.

Huygens, by observation and calculation, derived the ratio of the orbital periods of the planets to Earth’s as

• Mercury 25335 / 105190
• Venus 64725 / 105190
• Mars 197836 / 105190
• Jupiter 1247057 / 105190
• Saturn 3095277 / 105190

If we represent these ratios as Fraction in a list we can map over them to get the convergents:

From these Huygens selected 33/137 for Mercury, 8/13 for Venus pushed up to 32/52. For Mars he used 79/42 doubled to 158/84. Jupiter had the value 83/7 and Saturn 59/2 made into 118/4.
Which all gave quite satisfactory results!
In the next part of this series we’ll look at Pells equation and its solutions and in the process obtain a simple way of expressing the square root of an integer as a CF in list form then find better and better approximations by computing the convergents directly from the list.