# Continued fractions, music, and calendars

A couple of applications of continued fractions:

## Equal temperament

The most important intervals in Western music are the ones at the beginning of the harmonic series: First the octave, corresponding to a frequency ratio of about 2/1, then the perfect fifth, frequency ratio of about 3/2, and after that the perfect fourth (4/3), major third (5/4), and minor third (6/5). The fourth is just the (logarithmic) difference between an octave and a perfect fifth, and the minor third is the difference between a perfect fifth, and a major third, so we can focus on octaves, perfect fifths, and major thirds.

“Pure” octaves, fifths, and major thirds are exactly 2/1, 3/2, and 5/4, respectively. However, no combination of pure fifths or pure thirds can ever equal a combination of pure octaves; that is, (3/2) or (5/4) raised to any integer power can never equal (2/1) to an integer power (aside from the trivial case where both powers are zero). To obtain a closed tuning system, one usually tempers the fifth, adjusting it so that m fifths equal n octaves:

$(2/1)^n=f^m$

where f is close to 3/2. This can be rewritten

$n\log(2/1) = m\log (f)$

or

$n / m = \log (f) / \log (2)$

Suppose what one wants is an octave division that gives accurate approximations to pure fifths; that is, values of m and n such that m / n is nearly equal to log (3/2) / log (2) = 0.5849625007… This looks like a job for continued fractions.

$\log (3/2) / \log (2) = 0 + 1/(1 + 1 / (1 + 1 / (2 + 1 / (2 + 1 / (3 + ...)))))$

This gives successive approximations:

0 / 1 = 0.0000000…
1 / 1 = 1.0000000…
1 / 2 = 0.5000000…
3 / 5 = 0.6000000…
7 / 12 = 0.5833333…
24 / 41 = 0.5853658…
31 / 53 = 0.5849056…

The first reasonably close approximation is 7/12, meaning that 7 just octaves is nearly equal to 12 just fifths; conversely, if you divide the octave into 12 equal parts and take 7 such parts as a tempered fifth, then 12 tempered fifths is exactly 7 octaves.

So an equal division of the octave into 12 parts gives a good approximation to a just fifth. It happens also to give adequate approximations to major  thirds. That’s why we use 12-equal temperament for most Western music today.

The continued fraction expansion shows you’d get a better approximation to a just fifth using a 41-equal division of the octave, and an extremely good approximation using 53-equal. Of course these divisions are considerably less practical for any mechanical instrument or controller. Then again, why bother trying to improve the fifth? It’s very good in 12-equal.

But the thirds are not so good; how about if you repeat the above exercise for log (5/4) / log (2) = 0.3219280…? Then you get

0 / 1 = 0.0000000…
1 / 3 = 0.3333333…
9 / 28 = 0.3214285…
19 / 59 = 0.3220338…

And so on. Which is not so useful, either. Even if 28 notes to the octave isn’t too impractical for you, it contains no good approximations to a fifth. 59 to the octave has a better fifth, but still not very good.

But how about this? In regular tunings generated by a fifth, the major third is four fifths minus two octaves; with pure fifths, 4 * 0.5849625… – 2 = 0.3398500…, which is an adequate but not great approximation to a pure third. Conversely, if you want four fifths to give you a pure third, you use for your fifth (0.3219280…+2) / 4 = 0.5804820…, not too far from a pure fifth — this idea is called quarter comma meantone tuning. Now, what equal divisions of the octave give you fifths of that size? You get

0 / 1 = 0.0000000…
1 / 1 = 1.0000000…
1 / 2 = 0.5000000…
3 / 5 = 0.6000000…
4 / 7 = 0.5714286…
7 / 12 = 0.5833333…
11 / 19 = 0.5789474…
18 / 31 = 0.5806452…
101 / 174 = 0.5804598…

So 31 to the octave is an excellent approximation to quarter comma meantone, and 19 isn’t bad — actually it’s a very good approximation to one-third comma meantone, which gives you pure minor thirds and impure but very good major thirds.

What you really want, though, is some way to optimize thirds and fifths simultaneously. Continued fractions can be generalized to find simultaneous good rational approximations to several different real numbers, and this can be applied to tuning theory, but not as cleanly as the above.

## Calendars

The length of the mean tropical year is 365.242190419 days. Let’s consider successive approximations to this number using continued fractions:

365 / 1 = 365.00000000…: This says a year is about 365 days long, of course, and of course if every calendar year were 365 days, it would work well in the short term but get badly out of sync with the sun in a century or so.

1461 / 4 = 365 1/4 = 365.25000000…: This says 4 years is about 1461 days, which is the number of days in 3 standard years of 365 days plus one leap year of 366 days. If the calendar years are always in that pattern, they’ll be off by 0.00781 days per year or 0.781 days per century. That, of course, is the Julian calendar. The improvement of the Gregorian calendar was that century years are not leap years (subtracting one day per century, so the discrepancy now is 0.219 days per century) — except for years divisible by 400, which are leap years (adding back 0.25 days per century, for a discrepancy of 0.031 days per century).

10592 / 29 = 365 7/29 = 365.24137931… and 12053 / 33 = 365 8/33 = 365.24242424…: This says another way we could have done better than the Julian calendar would have to been to make an adjustment every 29 or 33 years. For instance, 12053 = 365 * 25 + 366 * 8, so if we did the 3 standard years, 1 leap year thing for 8 cycles, then did 1 standard year, and then repeated, we’d have a discrepancy of 0.0210 days per century. Of course this would be ridiculously confusing, and the benefit relative to the Gregorian calendar would be tiny.

46751 / 128 = 365 31/128 = 365.2421875…: If we did 3 standard years, 1 leap year for 32 cycles, that would be 46752 days. So if every 128 years we omitted a leap year, that would give us 46751 days, and the discrepancy would be 0.00029 days per century! This is 100 times more accurate than the Gregorian calendar. Of course, you’d lose the advantage of being able to tell at a glance whether a given year is a leap year or not, though that wouldn’t matter to most people most of the time.

Now, the length of the mean tropical year is not constant. It changes by about 5 ms per year, or about 0.00000006 days per year, or about 0.000006 days per century. Any calendar rule that claims to give discrepancies about this size or smaller is too accurate! The above 128-year calendar, then, is considerably more accurate than the Gregorian but nowhere near too accurate. Even so, don’t hold your breath waiting for its adoption…

The next approximation from the continued fraction series is 947073 / 2593 = 365.242190512, which is too accurate.