charlesreid1.com blog

The Josephus Problem: Part 2: Two Examples

Posted in Computer Science

permalink

This is Part 2 of an N-part series.

Two Examples

In this blog post we'll walk through two examples of a Josephus problem:

$$ n = 8, m = 4 $$

and

$$ n = 11, m = 2 $$

We will simulate the Josephus process and show how the solution is expressed using both permutation notation and cycle notation.

n = 8, m = 4

We begin with the example that Knuth gives in his problem statement, \(n = 8, m = 4\), along with its solution, \(54613872\).

Here is the Josephus circle for this case, and the corresponding removal index on the right (the red dot indicates the starting point for the process):

Circle index:
indexed by circle location order
Removal index:
indexed by removal order

Step by Step Removal for n = 8

Starting at the red dot, we proceed \(m-1\) points forward through the circle (\(m\) if we include the starting point), skipping items that have already been removed, and remove an item from the circle.

We indicate the starting point with a red dot in Step 0, then indicate the removal index with a red dot and the corresponding removal index.

Step 0: Starting Point
indexed by removal order
Step 1:
indexed by removal order
Step 2:
indexed by removal order
Step 3:
indexed by removal order
Step 4:
indexed by removal order
Step 5:
indexed by removal order
Step 6:
indexed by removal order
Step 7:
indexed by removal order
Step 8:
indexed by removal order

Writing the Solution Permutation: Two Row Notation

As mentioned in Part 1, we can think about the final removal order of sushi plates (or mice, or soldiers) as a special permutation, the Josephus permutation. If we want to write the Josephus permutation using the standard two-row notation for permutations, we would write the circle order (the index of items around the circle) on the top row, and the removal order (the index of items as they are removed) on the bottom row. This gives:

$$ \bigl(\begin{smallmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 5 & 4 & 6 & 1 & 3 & 8 & 7 & 2 \end{smallmatrix}\bigr) $$

While this notation is more intuitive (makes it easier to answer a variety of questions), it is also inconvenient notation, since there are many equivalent ways of writing a single permutation.

Writing the Solution Permutation: Cycle Notation

In Part 1 we also introduced cycles and cycle notation. Writing a permutation as a cycle is a unique representation of that permutation. By tracing which elements are permuted with other elements, we can turn the two-row representation into a cycle representation.

Here is the Josephus permutation for \(n = 8, m = 4\) in cycle notation:

$$ \left( 1 \, 5 \, 3 \, 6 \, 8 \, 2 \, 4 \right) \left( 7 \right) $$

Visual Representation of Solution Permutation

To represent the Josephus permutation graphically, we draw lines connecting the edges that permute and form cycles:

circle with undirected permutation paths

This is a visual representation of the Josephus permutation for \(n = 8, m = 4\).

n = 11, m = 2

Next is an example with a step size of two, which is a special case of the Josephus problem with a slightly easier solution procedure.

Here is the final Josephus permutation on a circle, which is what our procedure below will yield:

Circle index:
indexed by circle location order
Removal index:
indexed by removal order

Step by Step Removal for n = 11

As before, we proceed from the red dot, moving forward \(m-1\) items and removing the \(m^{th}\) item.

We indicate the starting point with a red dot in Step 0, then indicate the removal index with a red dot and the corresponding removal index.

Step 0: Starting Point
josephus n = 11 m = 2 step 0
Step 1:
josephus n = 11 m = 2 step 1
Step 2:
josephus n = 11 m = 2 step 2
Step 3:
josephus n = 11 m = 2 step 3
Step 4:
josephus n = 11 m = 2 step 4
Step 5:
josephus n = 11 m = 2 step 5
Step 6:
josephus n = 11 m = 2 step 6
Step 7:
josephus n = 11 m = 2 step 7
Step 8:
josephus n = 11 m = 2 step 8
Step 9:
josephus n = 11 m = 2 step 9
Step 10:
josephus n = 11 m = 2 step 10
Step 11:
josephus n = 11 m = 2 step 11

Two Row Notation

Here is the two-row representation:

$$ \bigl(\begin{smallmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 \\ 6 & 1 & 9 & 2 & 7 & 3 & 11 & 4 & 8 & 5 & 10 \end{smallmatrix}\bigr) $$

Cycle Notation

When factored into a cycle, this gives:

$$ \left( 1 \, 6 \, 3 \, 9 \, 8 \, 4 \, 2 \right) \left( 5 \, 7 \, 11 \, 10 \right) $$

Circle Permutation Diagram

To represent the Josephus permutation graphically, we draw lines connecting the edges that permute and form cycles:

circle with undirected permutation paths

This is a visual representation of the Josephus permutation for \(n = 11, m = 2\).

Next Steps: Solve!

Our next steps are simple: solve it!

In Part 3 we will show the solution of the special case of \(m = 2\) (the double-step case).

In Part 4 we will show several ways to solve the general case, and walk through some examples where we apply the solution procedure.

Tags:    graphs    puzzles    algorithms    josephus    latex   

The Josephus Problem: Part 1: The Problem

Posted in Computer Science

permalink

This is Part 1 of an N-part series.



The Josephus Problem and Variations

The following problem, Cat and Mice, is Puzzle 88 in Boris Kordemsky's The Moscow Puzzles.

Purrer has decided to take a nap. He dreams he is encircled by \(n\) mice: \(n-1\) gray and 1 white. He hears is owner saying, "Purrer, you are to eat each \(m\)-th mouse, keeping the same direction. The last mouse you eat must be the white one.

Which mouse should he start with?

This problem presents a scenario that is nearly identical to a classic problem that has a more grisly backstory: the Josephus problem.

Donald Knuth presents a more unsettling version of this problem, so I'll just let his (rather gleeful) description of the original problem suffice (except - what's this "we" stuff?)

From Section 1.3.2 of The Art of Computer Programming (Vol. 1), Exercise 22 presents the Josephus Problem:

22. (The Josephus Problem.) There are \(n\) men arranged in a circle. Beginning with a particular position, we count around the circle and brutally execute every \(m^{th}\) man (the circle closing as men are decapitated). For example, the execution order when \(n = 8, m = 4\) is \(54613872\): the first man is fifth to go, the second man is fourth, etc. Write a program which prints outs the order of execution when \(n = 24, m = 11\). Try to design a clever algorithm that works at high speed when \(n\) and \(m\) are large.

The Sushi Boat Variation

You are at the sushi boat restaurant, where plates of sushi in tiny boats float by in front of you.

There are \(n\) plates of sushi, each on a sushi boat. Each plate of sushi is labeled \(1 \dots n\) and arranged in order on the boats.

Beginning at plate 1, you count \(m\) plates of sushi, stopping at the \(m^{th}\) boat and taking the plate of sushi off the boat to eat it.

In what order will the sushi plates be stacked when you are done?

Which plate of sushi will be eaten last?

More Backstory

More background on the Josephus problem and its various solutions is given in a letter to the editor from the Fibonacci Quarterly, Issue 1 of 1976 (Part 1 and Part 2) written in response to an article that gave a solution to the problem of "Idiot's Roulette" (identical to the Josephus problem as presented above) without referencing the Josephus problem. Here is the original article.

The Tools

To solve the Josephus problem, we need to use several conceptual and computational tools. Below we cover some notation we will use and give links to pages on the charlesreid1.com wiki that are useful.

Permutations

We can think of the outcome of the Josephus problem as a "Josephus permutation" - a permutation that reorders the sushi plates in the circle (numbered by their positoin in the circle) into the order in which they are removed from the circle.

For example, in Knuth's problem statement, he gives an example of \(n = 8, m = 4\) (eating every 4th plate of sushi, in a train of 8 sushi boats), which results in the plates being removed in the following order:

$$ 5, 4, 6, 1, 3, 8, 7, 2 $$

To write this permutation using mathematical notation, we write two rows. The top row is the ordering of plates in the circle, the "natural" ordering, and the second row is the order of removal of plates, which is the Josephus permutation:

$$ \bigl(\begin{smallmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 5 & 4 & 6 & 1 & 3 & 8 & 7 & 2 \end{smallmatrix}\bigr) $$

As Knuth would point out, this Josephus permutation can be written in 40,319 other equivalent ways (that's 8! total, minus the 1 way shown above) by reordering the columns (as long as we reorder the columns in the top and bottom rows in the same way).

We can read this permutation from left to right as follows:

  • The first sushi plate (labeled 1) will be eaten fifth;
  • The second sushi plate (labeled 2) will be eaten fourth;
  • etc.

Ordering the permutation as above (circle index on top, removal index on bottom) makes it easy to answer the second question, "Which sushi plate will be eaten last?"

We find 8 in the bottom row (the removal index), and read the corresponding number in the top row (the plate number/circle position), plate 6.

If we wish to answer the first question, "in what order will the plates be removed," we have a bit more work to do. We mentioned that the above permutation is 1 of a total of 40,320 equivalent ways of writing the same permutation. Another way of writing it would be to maintain the pairing between top and bottom but sort the bottom elements:

$$ \bigl(\begin{smallmatrix} 4 & 8 & 5 & 2 & 1 & 3 & 7 & 6 \\ 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \end{smallmatrix}\bigr) $$

Now we can find the order of the plates by reading the top row right-to-left. The last plate removed is plate 6, so that will be on top of the stack of plates (stacks are first in, last out).

We cover permutations and permutation notation in the context of Rubiks Cubes on the Rubiks Cube/Permutations page of the charlesreid1.com wiki.

Cycles

While the above permutation notation is useful, the variety of ways of expressing the same permutation is inconvenient. This is where cycles become useful - cycles are a way of implicitly representing both rows of the permutation.

To do this, we "thread" our way through the permutation to create the cycle of which items move to which positions.

Starting with the left-most column of the permutation,

$$ \bigl(\begin{smallmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 5 & 4 & 6 & 1 & 3 & 8 & 7 & 2 \end{smallmatrix}\bigr) $$

we know that \(1 \rightarrow 5\). Now we find the column that has 5 in the top: the fifth column. We write \(5 \rightarrow 3\). Now we find the column with 3 in the top: the third column. We write \(3 \rightarrow 6\). Next, we write \(6 \rightarrow 8\), then \(8 \rightarrow 2\), then \(2 \rightarrow 4\). Once we see that the last step of the cycle is \(4 \rightarrow 1\), which brings us back to the beginning, we write that closed cycle in parentheses. If we have elements left, we repeat the procedure starting with any of the remaining elements. Sometimes there is a single cycle, and sometimes there are multiple cycles. In this case we have two cycles:

$$ \left( 1 \, 5 \, 3 \, 6 \, 8 \, 2 \, 4 \right) \left( 7 \right) $$

This indicates that 7 does not change position, i.e., the 7th plate of sushi is eaten 7th.

This notation is very convenient for finding solutions, and it turns out that Knuth gives a general solution procedure for the Josephus problem that involves a rather complicated application of the cycle notation, so a solid understanding of cycle notation is important.

We cover cycle notation in the context of Rubiks Cubes on the Rubiks Cube/Permutations page of the charlesreid1.com wiki (in particular, the sections on Permutation Algebra that cover intercalation products).

Circular Linked Lists

Among the many ways of solving the Josephus problem, the easiest method is to just carry out the procedure by hand to find the final Josephus permutation, then use it to answer the original question. This technique is referred to as the simulation technique.

This technique would become infeasible if we were to ask more difficult versions of the Josephus problem, such as posing a scenario where there are 5 million plates of sushi, and we wish to know which position (which plate of sushi) will be eaten 2,999,998th, and we also wish to have the answer instantaneously.

If we're dealing with smaller values of n and m, though, we can simulate a solution to the Josephus problem using a circular linked list.

Linked Lists

Briefly, a linked list is a type of list whose elements consist of nodes, small bundles containing a piece of data (the list item's value) and pointers to other nodes (the next and/or previous elements in the list).

See Linked Lists for notes on linked lists and some answers to textbook exercises.

See Lists Study Guide for a summary of important information about and properties of lists.

Circular Linked Lists:

A circular linked list is just what it sounds like: each of the elements points to the next element, and the last element in the list points to the first element in the list. We can use the list by maintaining a pointer to a particular item (the first item in the list).

We can insert items into the list by creating a new node, and inserting it between two nodes (e.g., the front and back of the list) by updating the pointers of the front and back nodes to point to the new list item.

Usefully, we can also remove items from the list with some pointer manipulation. To remove a node, we modify the next/previous pointers of the nodes before/after the doomed node so that they point to each other instead of to the doomed node.

This allows us to explicitly model the sushi boat (a.k.a. the "kill ring") in the Josephus problem.

Circular linked lists are covered on the charlesreid1.com wiki on the Linked Lists/Java/Circular page, and are implemented in Java in the cs/java repo: https://git.charlesreid1.com/cs/java/src/branch/master/lists/linked-lists

A Python implementation used to solve the Josephus problem is available in the cs/josephus repo: repo: https://git.charlesreid1.com/cs/josephus

TeX for Diagrams

In addition to writing The Art of Computer Programming, which has remained a gold standard algorithm textbook for over 40 years, Knuth also invented the TeX typesetting system, which is also the gold standard for typesetting mathematical equations.

We use the PGF/TikZ package to draw polygons that are useful in illustrating the circles of the Josephus problem and in visualizing various permutations.

A few examples and links to Github Gists with TeX code follow.

Empty Josephus Circle Diagram

Link to gist with TeX code

empty josephus circle diagram

Here is the TeX code to generate this diagram:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{xintexpr}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}[scale=3]

% make a node with variable name pol (with the list of features given) at the location (0,0), and don't label it
\node (pol) [draw=none, thick, black!90!black,rotate=0,minimum size=6cm,regular polygon, regular polygon sides=11] at (0,0) {};


% anchor is "corner 1"
% label is 1/2/3/4/etc
% placement is placement w.r.t. coordinate location
\foreach \anchor/\label/\placement in
    {corner 1/$1$/above,
     corner 2/$2$/above left,
     corner 3/$3$/left,
     corner 4/$4$/left,
     corner 5/$5$/below left,
     corner 6/$6$/below,
     corner 7/$7$/below,
     corner 8/$8$/below right,
     corner 9/$9$/right,
     corner 10/${10}$/right,
     corner 11/${11}$/above right}
\draw[shift=(pol.\anchor)] plot coordinates{(0,0)} node[font=\scriptsize,\placement] {\label};


% draw a circle connecting all points
\draw circle[radius=1.01cm];


% Draw a red dot at the starting point
\filldraw[red] (pol.corner 1) circle[radius=0.8pt];

% optional: black dots at each circle location
\filldraw[black] (pol.corner 2) circle[radius=0.4pt];
\filldraw[black] (pol.corner 3) circle[radius=0.4pt];
\filldraw[black] (pol.corner 4) circle[radius=0.4pt];
\filldraw[black] (pol.corner 5) circle[radius=0.4pt];
\filldraw[black] (pol.corner 6) circle[radius=0.4pt];
\filldraw[black] (pol.corner 7) circle[radius=0.4pt];
\filldraw[black] (pol.corner 8) circle[radius=0.4pt];
\filldraw[black] (pol.corner 9) circle[radius=0.4pt];
\filldraw[black] (pol.corner 10) circle[radius=0.4pt];
\filldraw[black] (pol.corner 11) circle[radius=0.4pt];

\end{tikzpicture}
\end{document}

Josephus Circle Diagram With Permutation Paths

Next, we can illustrate cycles in the permutation by drawing paths between connected nodes.

The edges are directed (1 -> 4 is not the same as 4 -> 1). We draw both directed and undirected versions.

Link to gist with TeX code

josephus circle diagram with undirected edges

josephus circle diagram with directed edges

The code to generate these diagrams is below.

Undirected Paths:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{xintexpr}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}[scale=3]

% make a node with variable name pol (with the list of features given) at the location (0,0), and don't label it
\node (pol) [draw=none, thick, black!90!black,rotate=0,minimum size=6cm,regular polygon, regular polygon sides=11] at (0,0) {}; 


% anchor is "corner 1"
% label is 1/2/3/4/etc
% placement is placement w.r.t. coordinate location
\foreach \anchor/\label/\placement in
    {corner 1/$1$/above, 
     corner 2/$2$/above left, 
     corner 3/$3$/left, 
     corner 4/$4$/left,
     corner 5/$5$/below left,   
     corner 6/$6$/below,
     corner 7/$7$/below,
     corner 8/$8$/below right,
     corner 9/$9$/right,
     corner 10/${10}$/right,
     corner 11/${11}$/above right}
\draw[shift=(pol.\anchor)] plot coordinates{(0,0)} node[font=\scriptsize,\placement] {\label};

% solution for n = 11, m = 4:
% ( 1 3 7 6 4 ) ( 2 8 ) ( 5 9 11 ) ( 10 )

% internal paths

% cycle (1 3 7 6 4)
\path [-] (pol.corner 1) edge (pol.corner 3);
\path [-] (pol.corner 3) edge (pol.corner 7);
\path [-] (pol.corner 7) edge (pol.corner 6);
\path [-] (pol.corner 6) edge (pol.corner 4);
\path [-] (pol.corner 4) edge (pol.corner 1);

% cycle 2 (2 8)
\path [-] (pol.corner 2) edge (pol.corner 8);
\path [-] (pol.corner 8) edge (pol.corner 2);

% cycle 3 (5 9 11 )
\path [-] (pol.corner 5) edge (pol.corner 9);
\path [-] (pol.corner 9) edge (pol.corner 11);
\path [-] (pol.corner 11) edge (pol.corner 5);


% draw a circle connecting all points
\draw circle[radius=1.01cm];


% Draw a red dot at the starting point 
\filldraw[red] (pol.corner 1) circle[radius=0.8pt];

% optional: black dots at each circle location
\filldraw[black] (pol.corner 2) circle[radius=0.4pt];
\filldraw[black] (pol.corner 3) circle[radius=0.4pt];
\filldraw[black] (pol.corner 4) circle[radius=0.4pt];
\filldraw[black] (pol.corner 5) circle[radius=0.4pt];
\filldraw[black] (pol.corner 6) circle[radius=0.4pt];
\filldraw[black] (pol.corner 7) circle[radius=0.4pt];
\filldraw[black] (pol.corner 8) circle[radius=0.4pt];
\filldraw[black] (pol.corner 9) circle[radius=0.4pt];
\filldraw[black] (pol.corner 10) circle[radius=0.4pt];
\filldraw[black] (pol.corner 11) circle[radius=0.4pt];

\end{tikzpicture}
\end{document}

Directed Paths:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{xintexpr}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}[scale=3]

% make a node with variable name pol (with the list of features given) at the location (0,0), and don't label it
\node (pol) [draw=none, thick, black!90!black,rotate=0,minimum size=6cm,regular polygon, regular polygon sides=11] at (0,0) {}; 


% anchor is "corner 1"
% label is 1/2/3/4/etc
% placement is placement w.r.t. coordinate location
\foreach \anchor/\label/\placement in
    {corner 1/$1$/above, 
     corner 2/$2$/above left, 
     corner 3/$3$/left, 
     corner 4/$4$/left,
     corner 5/$5$/below left,   
     corner 6/$6$/below,
     corner 7/$7$/below,
     corner 8/$8$/below right,
     corner 9/$9$/right,
     corner 10/${10}$/right,
     corner 11/${11}$/above right}
\draw[shift=(pol.\anchor)] plot coordinates{(0,0)} node[font=\scriptsize,\placement] {\label};

% solution for n = 11, m = 4:
% ( 1 3 7 6 4 ) ( 2 8 ) ( 5 9 11 ) ( 10 )

% internal paths

% cycle (1 3 7 6 4)
\path [->, shorten > = 3 pt, blue, shorten < = 4 pt, > = stealth] (pol.corner 1) edge (pol.corner 3);
\path [->, shorten > = 3 pt, blue, shorten < = 4 pt, > = stealth] (pol.corner 3) edge (pol.corner 7);
\path [->, shorten > = 3 pt, blue, shorten < = 4 pt, > = stealth] (pol.corner 7) edge (pol.corner 6);
\path [->, shorten > = 3 pt, blue, shorten < = 4 pt, > = stealth] (pol.corner 6) edge (pol.corner 4);
\path [->, shorten > = 3 pt, blue, shorten < = 4 pt, > = stealth] (pol.corner 4) edge (pol.corner 1);

% cycle 2 (2 8)
\path [->, shorten > = 3 pt, green, shorten < = 4 pt, > = stealth] (pol.corner 2) edge (pol.corner 8);
\path [->, shorten > = 3 pt, green, shorten < = 4 pt, > = stealth] (pol.corner 8) edge (pol.corner 2);

% cycle 3 (5 9 11 )
\path [->, shorten > = 3 pt, red, shorten < = 4 pt, > = stealth] (pol.corner 5) edge (pol.corner 9);
\path [->, shorten > = 3 pt, red, shorten < = 4 pt, > = stealth] (pol.corner 9) edge (pol.corner 11);
\path [->, shorten > = 3 pt, red, shorten < = 4 pt, > = stealth] (pol.corner 11) edge (pol.corner 5);


% draw a circle connecting all points
\draw circle[radius=1.01cm];


% draw a red dot at the starting point 
\filldraw[red] (pol.corner 1) circle[radius=0.8pt];

% optional: black dots at each circle location
\filldraw[black] (pol.corner 2) circle[radius=0.4pt];
\filldraw[black] (pol.corner 3) circle[radius=0.4pt];
\filldraw[black] (pol.corner 4) circle[radius=0.4pt];
\filldraw[black] (pol.corner 5) circle[radius=0.4pt];
\filldraw[black] (pol.corner 6) circle[radius=0.4pt];
\filldraw[black] (pol.corner 7) circle[radius=0.4pt];
\filldraw[black] (pol.corner 8) circle[radius=0.4pt];
\filldraw[black] (pol.corner 9) circle[radius=0.4pt];
\filldraw[black] (pol.corner 10) circle[radius=0.4pt];
\filldraw[black] (pol.corner 11) circle[radius=0.4pt];

\end{tikzpicture}
\end{document}

Next Steps: Examples and Solutions

So far in Part 1 we have covered some common forms of the Josephus problem.

In Part 2 we'll cover some examples of different \(n, m\) values (\(n\) is circle size, \(m\) is skip length) and show how the process plays out.

In Part 3 we will show the solution of the special case of \(m = 2\) (the double-step case).

In Part 4 we will show several ways to solve the general case, and walk through some examples where we apply the solution procedure.

Tags:    graphs    puzzles    algorithms    josephus    latex   

Approximating Pi (Happy Pi Day)

Posted in Mathematics

permalink

Favorite Pi Approximations

What's your favorite \(\pi\) approximation?

Some of my favorite approximations of \(\pi\) come from Ramanujan-Sato series. These are mathematical series that generalize from a remarkable formula for \(\pi\) given by Srinivasa Ramanujan, an Indian mathematician:

$$ \pi^{-1} = \dfrac{\sqrt{8}}{99^2} \sum_{k \geq 0} \dfrac{ (4k)! }{ \left( 4^k k! \right)^4 } \dfrac{ 1103 + 26390k }{ 99^{4k} } $$

This completely novel formula opened up new branches of mathematics and provided a whole new class of \(\pi\) approximations (the Ramanujan-Sato series) and approximations that are extremely accurate, making them very useful for computer applications. (Each term of the above sequence yields 8 additional decimal points of accuracy of \(\pi\).) These approximations have also enabled world record calculations of numbers of digits of \(\pi\).

But those are not the \(\pi\) approximations that this blog post is about.

This blog post is about another set of \(\pi\) approximations that I like - these come from another field Ramanujan had mastery over, continued fractions.

Continued fractions provide a whole alternative way of representing all the real numbers - rational and irrational.

Continued Fractions and Convergents

Back in July of 2017, we wrote a blog post about how to find rational approximations of square roots using continued fractions and convergents, and we implemented a Java program to represent the irrational number \(\sqrt{n}\) (where \(n\) is not a perfect square).

In short, continued fractions are a way of expressing numbers, rational and irrational, in terms of recursive fractions, which look something like this:

$$ x = b_0 + \cfrac{1}{ b_1 + \cfrac{1}{ b_2 + \cfrac{1}{ b_3 + \cfrac{1}{ b_4 + \cfrac{1}{ b_5 + \dots } } } } } $$

denoted

$$ [b_0; b_1, b_2, b_3, \dots] $$

and, when evaluated, results in a series of fractions (rational approximations) called the convergents of the continued fraction.

In the denominator of the above representation, we have a 1 in each position, which makes the continued fraction a simple continued fraction. If the denominators are not 1, the continued fraction is a general continued fraction.

The continued fraction expansion of any rational number (or equivalently, the sequence of convergents) must terminate at some point (even if the number of terms ends up being very large).

The convergents always terminate if a number is rational. Conversely, if you can prove a number's continued fraction representation is a repeated sequence or continues forever, you can prove a number is irrational (a strategy employed in several proofs about properties of \(\pi\)).

Simple Continued Fractions to Approximate Pi

For \(\pi\), the convergents of the simple continued fraction (there is a single unique simple continued fraction representation of \(\pi\)) are unpredictable; the first few are:

$$ \pi = [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2...] $$

Each additional term leads to increasingly precise fractional approximations for \(\pi\); they are:

$$ [3;7] = \dfrac{22}{7} = 3.\overline{142857} $$
$$ [3;7,15] = \dfrac{333}{106} = 3.1415094... $$
$$ [3;7,15,1] = \dfrac{355}{113} = 3.1415929... $$
$$ [3;7,15,1,292] = \dfrac{103993}{33102} = 3.14159265... $$

General Continued Fractions to Approximate Pi

Every number has exactly one representation as a simple continued fraction, if we restrict the values of \(b_i\) to be positive.

However, if we generalize the simple continued fraction to allow any integer in the numerator of the continued fractions, we get a general continued fraction:

$$ x = b_0 + \cfrac{a_1}{ b_1 + \cfrac{a_2}{ b_2 + \cfrac{a_3}{ b_3 + \cfrac{a_4}{ b_4 + \cfrac{a_5}{ b_5 + \dots } } } } } $$

Unlike simple continued fractions, one number can be expressed in many ways with many different general continued fractions.

We use two general continued fractions as well as the simple continued fraction representation of \(\pi\) to generate some \(\pi\) approximations below.

Odd Squares and Twos

The following general continued fraction

$$ \dfrac{4}{\pi} = 1 + \cfrac{1^2}{ 2 + \cfrac{3^2}{ 2 + \cfrac{5^2}{ 2 + \cfrac{7^2}{ 2 + \cfrac{9^2}{ 2 + \dots } } } } } $$

can be turned into approximations for \(\pi\) by finding the convergents of the above continued fraction, then reversing the numerator and denominator and multilpying the new numerator by 4.

Implementing a recurrence to turn the above into convergents and printing the first 24 terms and their approximate value:

                                      Pi
Convergent                                                          Approx 
--------------------------------------------------------------------------------
8 / 3                                                               2.6666666666666665
52 / 15                                                             3.4666666666666668
304 / 105                                                           2.8952380952380952
3156 / 945                                                          3.3396825396825398
30936 / 10395                                                       2.9760461760461761
443748 / 135135                                                     3.2837384837384835
6115680 / 2027025                                                   3.0170718170718169
112074660 / 34459425                                                3.2523659347188758
1991580840 / 654729075                                              3.0418396189294024
44442113940 / 13749310575                                           3.2323158094055926
967171378320 / 316234143225                                         3.0584027659273318
25444221030900 / 7905853580625                                      3.2184027659273320
655370553511800 / 213458046676875                                   3.0702546177791836
19859578238549700 / 6190283353629375                                3.2081856522619421
590885791980523200 / 191898783962510625                             3.0791533941974261
20266826271207308100 / 6332659870762850625                          3.2003655154095472
684008280009204381000 / 221643095476699771875                       3.0860798011238333
26194878742247361184500 / 8200794532637891559375                    3.1941879092319412
988797092817095519958000 / 319830986772877770815625                 3.0916238066678385
41820004752592427401540500 / 13113070457687988603440625             3.1891847822775947
1745807922530722423852479000 / 563862029680583509947946875          3.0961615264636411
80816804632604843113153342500 / 25373791335626257947657609375       3.1850504153525301
3696894652389922594527576660000 / 1192568192774434123539907640625   3.0999440323738066

(16 decimal points are printed for decimal approximations above.)

This exhibits a pattern seen with general continued fractions, which is that they tend to jump above and below the value they approximate, depending on whether there are an even or odd number of terms in the continued fraction being included.

Odd Squares, Threes, and Sixes

Another related continued fraction, this one for \(\pi\) and with a more predictable pattern, is given by:

$$ \pi = 3 + \cfrac{1^2}{ 6 + \cfrac{3^2}{ 6 + \cfrac{5^2}{ 6 + \cfrac{7^2}{ 6 + \cfrac{9^2}{ 6 + \dots } } } } } $$

Implementing a computer program to evaluate the convergents from the above patterns yields even more \(\pi\) approximations:

                                Even More Pi
Convergent                                                          Approx 
--------------------------------------------------------------------------------
19 / 6                                                              3.1666666666666665
141 / 45                                                            3.1333333333333333
1321 / 420                                                          3.1452380952380952
14835 / 4725                                                        3.1396825396825396
196011 / 62370                                                      3.1427128427128426
2971101 / 945945                                                    3.1408813408813407
50952465 / 16216200                                                 3.1420718170718169
974212515 / 310134825                                               3.1412548236077646
20570537475 / 6547290750                                            3.1418396189294020
475113942765 / 151242416325                                         3.1414067184965018
11922290683065 / 3794809718700                                      3.1417360992606653
322869019821075 / 102776096548125                                   3.1414796890042549
9388645795842075 / 2988412653476250                                 3.1416831892077552
291703390224616125 / 92854250304440625                              3.1415189855952756
9646071455650881825 / 3070380543400170000                           3.1416533941974261
338203386739761387075 / 107655217802968460625                       3.1415419859977827
12533792135642378629875 / 3989575718580595893750                    3.1416353566793886
489501901570061970946125 / 155815096120119939628125                 3.1415563302845726
20095772843114788169975625 / 6396619735457555416312500              3.1416238066678388
865107029346752986828909875 / 275374479611447760672253125           3.1415657346585473
38971636325356476834702484875 / 12404964652972837218854831250       3.1416160719181865
1833412715214285133654869268125 / 583597200719403932796125015625    3.1415721544829651
89918039850132576392201747480625 / 28621636626586418964957783375000 3.1416106990404735

When to Use Simple Vs. General Continued Fractions

While general continued fractions are more flexible, allowing a given rational or irrational number to be expressed in a wider variety of ways, it is important to point out how much faster the simple continued fractions converge - with 15 terms, we arrive at around 15 accurate decimal places, versus the 1-3 decimal places of accuracy from over 20 terms in the above approximations.

Using the simple continued fraction representation of \(\pi\),

$$ \pi = 3 + \cfrac{1}{ 7 + \cfrac{1}{ 15 + \cfrac{1}{ 1 + \cfrac{1}{ 292 + \cfrac{1}{ 1 + \dots } } } } } $$

and implementing it with a computer, we get the following table of convergents and their approximate values:

                               Pi Simple
Convergent                                                          Approx 
--------------------------------------------------------------------------------
22 / 7                                                          3.1428571428571428
333 / 106                                                       3.1415094339622640
355 / 113                                                       3.1415929203539825
103993 / 33102                                                  3.1415926530119025
104348 / 33215                                                  3.1415926539214212
208341 / 66317                                                  3.1415926534674368
312689 / 99532                                                  3.1415926536189365
833719 / 265381                                                 3.1415926535810779
1146408 / 364913                                                3.1415926535914038
4272943 / 1360120                                               3.1415926535893890
5419351 / 1725033                                               3.1415926535898153
80143857 / 25510582                                             3.1415926535897927
165707065 / 52746197                                            3.1415926535897936

A Note on the Program

We implemented a program to convert terms in a continued fraction representation (simple or general) into convergents (rational approximations).

We covered how to do this with Java in a previous blog post, but this time we re-implemented it in Python and used some basic object-oriented Python techniques and class decorators (class methods, memoized functions, etc.)

You can find the code here: https://git.charlesreid1.com/cs/python/src/branch/master/math/pi_continued_fraction_convergents.py

We will return to the topic of continued fractions and cover general continued fractions (and importantly, the recurrence relation used to convert them into convergents in the above program) in a future blog post.

Tags:    pi    continued fractions    number theory    mathematics    python    irrational numbers   

November 2026

Baby Mac Crypto

March 2022

How to Read Ulysses

July 2020

Applied Gitflow

September 2019

Mocking AWS in Unit Tests

November 2017

A Hard(y) Math Problem