Sitemap

Making “miracle fractions” with pencil and paper

5 min readNov 3, 2023

--

It all started when my deep freezer broke. With a bit of a panic we managed to get a new one, and move our frozen foods to the new one without losing the contents.

The energy label (that is required for household devices in the Netherlands) of our shiny new fridge claims an average energy use of 149 kWh/yr. I didn’t know what that meant, so I calculated the average energy use in watt.

I got, rounded to two decimals: 17.00 W. I did not expect such a nice round number; I checked again but this was actually the answer: to be exact, I got 16.9978 W (taking a year of 365.2425 days according to the Gregorian calendar).

Press enter or click to view image in full size
Generic energy label of a deep freezer

Of course the actual energy use depends on factors like room temperature, how often you open the fridge and how much is in it, but that isn’t the point of this story. I accidentally found a cool mathematical coincidence, and thought it was fun to talk about this.

The underlying mathematical theory uses continued fractions. It is not well known among non-mathematicians, but I think it deserves more attention because it is beautiful, sometimes useful and you can understand it with a bit of intuition.

What continued fractions do is approximate numbers by fractions. These fractions are the “best” fractions that you can get, and it is surprisingly easy to find them. Below I’ll give you a recipe that you can do yourself with a tiny piece of paper.

Press enter or click to view image in full size
You can approximate any number as a fraction

Back to the fridge. The conversion factor from kWh/yr to W is equal to 8.76582, using a Gregorian year.

With continued fractions you can quickly find approximations, and we get the corresponding “coincidences”:

  • 9 (9 kWh/yr = 1.027 W)
  • 35/4 (35 kWh/yr = 3.993 W)
  • 114/13 (114 kWh/yr = 13.005 W)
  • 149/17 (149 kWh/yr = 16.998 W)

The approximations get more and more accurate, a bit irregularly, increasing with a decimal digit about every two steps.

My deep freezer label fits nicely in this list, as you can see.

Press enter or click to view image in full size
Continued fraction, mathematical view

Making your own approximations

It is quite easy to make your own approximations on paper. Here’s a paper and pencil recipe (I’ll give you a program below, don’t worry).

Make a table with five rows. Call the rows N (for numerator), D (for denominator), Q (for quotient), A, and B (for running out of names).

Write your number as a fraction n/d (e.g., 8.76582 = 876582/100000) and start the table like this:

N (numerator)
D (denominator)
Q
A 0 1
B 1 0

All you then have to do is fill the table by repeating the steps below. Each step only deals with the last numbers of the rows, that I’ll call n,d,q,a,b, respectively.

  • Divide n by d, rounding down, and write this at the end of Q. This isn’t hard, because it is often just 1 or 2. In short: q := ⌊n/d⌋
  • Write n-q*d at the end of D, and the old value of d at the end of n. In short: n,d := d,n-q•d
  • Write a’+q*a at the end of A (where a’ is the second last value).
  • Likewise, write b’+q*b at the end of B.

(I like to show off doing this in my head, because I am of that generation. However, feel free to use a calculator: it’s more fun that way.)

Get Jurjen Bos’s stories in your inbox

Join Medium for free to get updates from this writer.

Example: Let’s try to find an approximation of π as a fraction. We start with the four digit value 3.1416 = 31416/10000.

N 31416 10000 1416  88
D 10000 1416 88 8
Q 3 7 16
A 0 1 3 22 355
B 1 0 1 7 113

The approximations you need show up as values a/b. And indeed, the first two fractions from the beginning of the article appear immediately. (The fact that the 355/113 gives more than 4 digits is a property of π itself.)

With the recipe, you can find endless examples of nice approximations. Here are a few I made:

  • 44 kg = 97.003 lb
  • 2 bar = 97.003 psi
  • 16 cal = 66.99 J
  • €54 = ƒ119.003
  • 25 kW = 33.99 HP
  • 1 ha = 42.008 acre

It always feels like a small miracle if you find such an approximation, even if you know they always exist.

The values in the Q row in the pencil and paper procedure are called the continued fraction coefficients of your number, like in the picture above. There’s a lot of mathematical theory on this. Some interesting properties:

  • The approximations get better and better.
  • They alternate between too high and too low.
  • The process ends with (the reduced form of) the starting fraction.
  • The computation in this algorithm can be done forwards (as in the paper procedure), backwards (as in the mathematical formula), and in any other order.
  • There’s a variant of this procedure for all real numbers; in this case the calculation goes on forever.
  • The precision of the approximation is about equal to the total number of digits in the fraction.
  • If the q value is high, the fraction just before it is a “good” one, like the two approximations for π.
  • Most numbers continued fraction coefficients are just 1.
  • Square roots have nice regular continued fractions.

You can look these and more properties up in Wikipedia, and the underlying theory is quite extensive. Many algorithms use continued fractions as an intermediate step.

Finally, here’s a simple Python function that approximates a floating point value as a fraction:

from math import floor
def approximate(x:float, steps=7):
"""Approximate number as fractions
>>> approximate(31416/10000, 3)
3 gives 3/1
7 gives 22/7
16 gives 355/113
"""
a1, a, b1, b = 0, 1, 1, 0
for i in range(steps):
q = floor(x)
if q == x: return
x = 1 / (x - q)
a1, a = a, a1 + q * a
b1, b = b, b1 + q * b
print(f"{q} gives {a}/{b}")

Finally, in 1982 a multidimensional version of this algorithm was discovered. It is called the LLL algorithm, after its discoverers Arjen Lenstra, Hendrik Lenstra and László Lovász.

This algorithm is so versatile that a 500 page book has been written just about applications! For example, you can find crazy expressions like 48π³+45π²-561π=170.00000004. Maybe I’ll write about that later.

--

--

Jurjen Bos
Jurjen Bos

Written by Jurjen Bos

Cryptographer, born in 1964, raised in the Netherlands. Love to dive deep into little technical details, from random numbers to programming languages.