These settings apply to every instrument on the page. Sound starts only when you press a play button.

Deep dive · Music ∩ Math · Pi and the primes, by ear and by eye

The Rhythm of Numbers

It started with a simple guess of Teddy Rook’s: π ≈ 3 + k/(7k + 1). Put in k = 16 and you get 355/113, a famous fraction for pi. Then play pi as a beat. You hear a rhythm: gaps of 7 beats, with a gap of 8 about every 16th time, and a rare short phrase after 292 longer ones. Each section below follows that rhythm, or the rule that builds it, to a place where it meets the prime numbers.

The number in the bar at the top drives every instrument. Swap pi for another number to see what makes pi special. Each section has a green “Pi ↔ primes” line that names the link.

22/7 · E(1,7)
7 is prime
223/71 · E(10,71)
71 and 223 are prime
355/113 · E(16,113)
113 is prime
A hit falls wherever ⌊n·(π − 3)⌋ steps up by one. The gaps run 7, 7, 7, … with an 8 after every fifteen 7s, so one cycle is 113 beats holding 16 hits: 355/113.

Chapter I

Pi’s machine

The continued fraction: the rule that builds pi’s rhythm, and the rule every later section reuses.

1 · The starting guess

3 + k/(7k + 1) finds pi at k ≈ 16

The fraction $\frac{k}{7k+1}$ is the same as $\cfrac{1}{7+\frac{1}{k}}$. Set it equal to π − 3 and solve, and you get k = 15.9966. Round that to 16 and you get 355/113, which matches pi to six decimal places. The Chinese mathematician Zu Chongzhi found the same fraction about AD 480; it is called Milü, “the close ratio.” Put in k = 10 instead and you get 223/71, the lower bound Archimedes proved.

16
Fraction
Value
Error vs π
Factors
  • |error|, log scale
  • 7k + 1 is prime
  • selected k

Remix

2 · The levels

The 16 was the next level down

1/(π − 3) = 7.0625, so the gaps between hits are 7 or 8 beats. The leftover 0.0625 is 1/15.9966, which is the k from the guess. It says an 8 comes about once every 16 gaps. Go one level deeper and 15.9966 = 15 + 1/(1 + 1/292.6). Every term of pi’s continued fraction, [7; 15, 1, 292, …], is a repeat count in the rhythm. The table follows the number in the bar.

LevelTermBlock (beats)HitsFractionBlock length prime?What you hear

$$\role[next block]{output}{q_{n+1}} = \role[repeat count, the term]{coefficient}{a_{n+1}}\cdot \role[this block]{base}{q_n} + \role[previous block]{index}{q_{n-1}}$$

In words: next block = (term × this block) + previous block. It is the textbook rule for continued-fraction denominators, read here as a rule for musical form.

rhythm(terms a1, a2, a3, …):      # any number between 0 and 1
  prev = "x"                        # one hit
  this = "."                        # one rest
  this, prev = "."×(a1 − 1) + "x", this
  for each next term a:
      this, prev = this×a + prev, this
  return this                       # x = hit, . = rest

For pi, the level-4 block is 292 phrases of 113 beats followed by one short phrase of 106. Counting the first beat as beat 0, the short phrase starts at beat 32,996 (that is, 292 × 113) and ends the block at beat 33,102. Checked by computer over 140,000 beats: in the first 80,000 beats there are 706 whole phrases of 113 and 2 of 106.

Mathematicians call these Sturmian or Christoffel words. They are the most evenly spread rhythms possible, which is why Euclid’s algorithm and Euclidean rhythms (section 6) produce them too. What this page adds is reading the continued fraction as musical form: bar, phrase, section, movement.

Remix

3 · Composer

Hear pi’s form as music

The number in the bar plays here, with one voice per level. Hits carry the melody, and the starts of level 2, 3 and 4 blocks play bass, bell and gong. In “gap = interval” mode each hit moves the melody up by its gap in half steps, so pi’s gaps of 7 walk the circle of fifths and each 8 slips by a half step. “Jump to the rarest block” goes straight to pi’s short phrase after the 292 long ones.

Whole numbers with commas, like 3,3,3 or 2,1,2,1. Each one is a repeat count. Using them sets the bar’s number too.

The number
Block lengths
Gaps used
Now playing
beat 0
  • long block
  • short block
  • hits
14

Remix

4 · The ladder

Are pi’s best fractions unusually prime?

Stop the continued fraction after each term and you get the number’s best fractions, called convergents. For pi they are 3, 22/7, 333/106, 355/113, 103993/33102, …. A large term means the fraction before it was unusually good, which is why the 292 makes 355/113 so accurate. Prime numerators and denominators are marked ◆. Switch the bar’s number to compare pi with other constants.

#NumeratorDenominatorError

Remix

Chapter II

Pi and primes in sound

Tuning, polyrhythm, clocks that fall into step, and the primes as a melody.

5 · The circle of fifths

12 notes come from the same ladder as 22/7

A pure fifth is the frequency ratio 3:2, which is log₂(3/2) = 0.58496 of an octave. Its best fractions run 1/2, 3/5, 7/12, 24/41, 31/53. “A fifth is 7 of 12 half steps” is the 7/12 fraction, found by the same rule that finds 22/7 for pi. The 12-note fifth is 1.96 cents flat, and over 12 fifths that small miss adds up to the Pythagorean comma, 23.46 cents. The next good stops are 41 and 53 notes per octave, and 53 is prime.

12
Best fifth
Error
Steps that visit all notes
n is prime?
  • fifth error in cents
  • prime n
  • selected n

Remix

6 · Euclid’s algorithm

One algorithm builds pi’s ladder, finds shared factors, and spreads beats

Euclid’s algorithm divides and keeps the remainder, again and again. Run it on 355 and 113 and the quotients are 3, 7, 16: pi’s continued fraction, cut off. It ends at the greatest common divisor, the oldest test for whether two numbers share a prime. In 2003 the physicist Eric Bjorklund used the same steps to spread pulses evenly in a particle accelerator, and in 2005 Godfried Toussaint showed that the results are rhythms found in music around the world.

Remix

7 · Mode locking

Clocks lock to fractions, and there are about 3N²/π² of them

Things that swing or spin and nudge each other fall into step at simple ratios. Christiaan Huygens saw two pendulum clocks on one beam fall into step in 1665. Mercury spins exactly 3 times for every 2 trips around the Sun. Heart cells follow the beat of a pacemaker. A simple model, the circle map, shows all of these: as the coupling K grows, “tongues” open at each fraction p/q, and inside a tongue the motion snaps to that fraction.

1.0050
Tongues (fractions in lowest terms)
3N²/π²
  • locked (darker = smaller denominator)
  • the bar’s number

Remix

8 · Prime music

Primes as melody, prime gaps as rhythm

Number the 12 notes of the octave 0 to 11, starting at C. Every prime above 3 leaves a remainder of 1, 5, 7 or 11 when divided by 12, so it lands on one of four notes: C♯, F, G or B. Play the primes in order and let each gap to the next prime set the note’s length. In 2016 Robert Lemke Oliver and Kannan Soundararajan found that a prime tends not to repeat the remainder of the prime before it. With no memory, repeats would be 25% of pairs. Among the primes up to a million they are 15%, and up to ten million 16.4%. You hear it as fewer repeated notes than chance would give.

A scrolling score. Each row is a note, and each bar’s length is the gap to the next prime. Repeated notes are amber.

Remix

Chapter III

Where pi is made of primes

6/π², π²/6, and the patterns primes leave on pi’s own spirals.

9 · The orchard

Two random whole numbers share no prime 6/π² of the time

Stand at the corner of an orchard planted on a square grid. The trees you can see, with no tree in front of them, are the points (a, b) where a and b share no factor. They fill 6/π² ≈ 60.79% of the grid.

40
Visible share
6/π²
0.607927
Product over primes ≤ grid
  • visible (coprime)
  • hidden
  • pi’s convergents (q, p − 3q)

Remix

10 · The correlation matrix

Coprime means uncorrelated

The orchard is more than a picture that looks like a correlation matrix. It is one. Treat “a hit every a-th beat” as a signal, and give every a from 2 to N its own pulse train. Two pulse trains a and b line up once every lcm(a, b) beats, and their correlation works out to exactly

$$\operatorname{corr}(a,b) = \frac{\role[shared factor]{coefficient}{\gcd(a,b)} - 1}{\sqrt{(a-1)(b-1)}}$$

On the diagonal the gcd is a itself, so the correlation is 1. Off the diagonal the correlation is zero exactly when a and b are coprime. So the zero cells of this matrix are the orchard’s visible trees, and they fill 6/π² of it.

40 12
Uncorrelated pairs
6/π²
0.607927
corr(a, b)
a is prime?
  • positive correlation
  • zero: coprime
  • corr = 1 line
Real linkFinance uses random matrix theory to tell real correlations among stocks from noise (Laloux and colleagues, and Plerou and colleagues, both 1999). It is the same mathematics whose spacings match the zeros of the zeta function (section 15). Stock correlations, heavy nuclei and the primes all show the same family of random-matrix statistics.

Remix

11 · Chemistry: hydrogen

Hydrogen’s 1/n² adds up to π²/6, a product over primes

Hydrogen’s energy levels go as −1/n², and its visible lines, the Balmer series, come from the differences 1/4 − 1/n². In wavelengths that is λ = 364.56 · n²/(n² − 4) nanometers, the formula Johann Balmer found in 1885. Electron shells hold 2n² electrons: 2, 8, 18, 32.

8
Σ 1/n²
Π over primes
π²/6
1.644934

Remix

12 · Pi’s sunflower, seeded with primes

Primes avoid the arms that share a factor

Place seed number k at k × (the bar’s number) turns around a circle, at a distance √k from the center. For π − 3 the seeds fall into 7 spokes that curl into 113 arms, because those are pi’s block lengths. Now light up only the seeds whose numbers are prime.

3000

Remix

13 · Sieves and hidden periods

Wrap the line, and hidden periods stand up

The sunflower, straightened out. Lay the numbers in rows of a chosen width. When the width matches a hidden period, the hits line up in straight columns. When it is slightly off, the columns lean, and the lean shows the error. Pi’s rhythm stands up at its block lengths 7, 106 and 113. The primes line up in the columns that share no factor with the width.

30

Remix

Chapter IV

Primes in matter

X-ray diffraction and nuclear physics both treat pi’s rhythm and the primes as the same kind of object.

14 · Chemistry: quasicrystals

Pi’s rhythm and the primes both diffract like crystals

In 1982 Dan Shechtman found a crystal that was ordered but never repeated, a quasicrystal. Many scientists doubted it at first; it won him the 2011 Nobel Prize in Chemistry. Crystallographers read order from diffraction, which is the Fourier transform of where the atoms sit. Pi’s rhythm is a one-dimensional quasicrystal: its diffraction has sharp spikes at multiples of π − 3, wrapped around 1.

3000
  • diffraction intensity
  • predicted spikes
  • random row

Remix

15 · Nuclear physics: zeta zeros

The primes are a sum of waves, tuned like heavy nuclei

Bernhard Riemann’s explicit formula rebuilds the prime staircase, which climbs by log p at every prime power, from waves whose frequencies are the zeros of the zeta function: 14.13, 21.02, 25.01, …. Add more zeros and the curve snaps to the steps. In 1972 Hugh Montgomery and Freeman Dyson noticed that the spacings between the zeros follow the same statistics as the energy levels of heavy atomic nuclei. No one yet knows why.

20
  • true prime staircase ψ(x)
  • rebuilt from zeta zeros
  • primes

Remix

Chapter V

Hunting primes with pi’s tools

The continued fraction, the rhythm and the necklace, each tried on the primes, with the results.

16 · Factoring

The machine that finds 355/113 also splits numbers into primes

In 1975 Michael Morrison and John Brillhart published how they used continued fractions to factor 2¹²⁸ + 1. Run the continued fraction of √N, the same steps as pi’s ladder. Each step gives a small number Q that matches a perfect square, counting remainders after dividing by N. When Q is itself a perfect square, one gcd splits N. This page runs the simple version that D. H. Lehmer and R. E. Powers described in 1931.

Result
—
Steps of √N’s fraction
—
Multiplier
—
StepTerm aQ (matches a square)Square?

Remix

17 · Mersenne circles

Doubling on a 2p − 1 clock rotates rhythms

A times-table circle joins each point x to 2x. Use 2p − 1 points and doubling does something exact: write x in binary with p digits, and doubling just rotates the digits one place. So each orbit is one p-beat rhythm and all of its rotations, a necklace. When p is prime, every orbit has exactly p members, so p divides 2p − 2. That is Fermat’s little theorem, proved with rhythms. It does not make 2p − 1 prime, though: try p = 11, where 2047 = 23 × 89. The real test is the Lucas–Lehmer test. The largest prime known, 2136,279,841 − 1, with 41,024,320 digits, was found with it in October 2024 by the Great Internet Mersenne Prime Search.

7
2p − 1
Prime?
Orbits
Orbit lengths

Each necklace below is one orbit. Tap one to hear it as a rhythm.

Remix

18 · Music in, numbers out

Tap a rhythm, read its primes

A rhythm of n beats is also an n-digit binary number, with a hit as 1 and a rest as 0. The readout tests it for being prime, lists its rotations that are prime, finds the continued fraction of its density (hits per beat), and checks whether it is spread as evenly as possible. A rhythm needs a hit on its last beat to be an odd number.

7
Binary
Number
Even spacing?
Density
Rotations (amber = prime):

Remix

19 · The prime hunt

Does pi’s rhythm attract primes?

Read the first n beats of the bar’s number as one long binary number, and test it for being prime as n grows. Compare with what chance predicts: an odd number with n binary digits is prime about 2 times in n·ln 2.

800
Prime prefixes at beat
—
Found vs chance
—
Progress
ready

Remix

Chapter VI

Play and compose

Stack the rhythms, drum with them, and write a whole piece from a number.

20 · The polyrhythm layerer

Stack the levels of a number, one ring each

Each ring is one track: a convergent p/q played as p hits spread evenly over q slots, all rings taking the same time to go around. Stack pi’s 1/7, 15/106 and 16/113 and you hear three versions of the same beat drift and meet. Or layer different constants. Each track has its own voice, a mute and a solo.

5 s

Remix

21 · The Euclidean drum machine

Four drums, each spread as evenly as possible

Pick how many hits (pulses) and how many slots each drum gets, and the machine spreads the hits as evenly as it can with Bjorklund’s method from section 6. Rotate a track to start it on a different hit. Tracks of different lengths drift against each other and line up again after their least common multiple. Each track shows the continued fraction of slots/pulses and whether the two numbers are coprime.

420

Remix

22 · Compose a piece

Write a piece from a number, then keep it

This is Teddy Rook’s idea grown into a small composing machine. The number’s rhythm gives the beats. Its levels give the form: the shortest blocks are bars, the next level up gives phrases, and the next gives sections. (A level that only adds one beat is skipped, so for pi the bars are 7 beats, the phrases 113, and the sections 33,102.) Each level gets a voice: melody on the hits, a bass note at each bar, a chord at each phrase, and a cymbal and key change at each section. Choose the number, the scale, the voices and the length, then play it, look at the score, and download it as a MIDI file for any music program, or as a WAV sound file.

300

The link holds only the settings above, nothing about you. Opening it rebuilds the same piece.

Remix

23 · The Continued-Fraction Suite

Original music from these rhythms

Teddy Rook used the ideas on this page to compose original music based on continued fractions: pieces whose bars, phrases and sections follow the levels of pi and other numbers, the way the Composer and “Compose a piece” do. The page came first, as the workshop, and the music grew out of it.

Remix

Epilogue

The map

Every section, joined wherever two share an idea. Pick a thread in the bar to see its web, and press a dot to jump to it.

    Hover or tap a dot to see what it connects to.

    Threads to pull

    Questions this page raises about pi and the primes. Some are open, some are known, and some are experiments to try. Treat them as starting points, not results.

    Open

    Prime convergents

    Does pi’s continued fraction have infinitely many prime denominators? No one knows. The same question for φ is the Fibonacci prime problem, and for √2 it is the Pell primes. Both are unsolved. A good experiment: count prime convergents for many constants and compare each count with chance.

    Open

    Primes and nuclei

    The Montgomery–Dyson match (section 15) remains one of the strangest links in science. Hearing the zeros’ spacings next to random and nuclear spacings is a good way to sense why mathematicians take it seriously.

    Known

    Rhythm necklaces count primes

    The rhythm proof of Fermat’s little theorem in section 17 is classical. Counting necklaces also gives the number of irreducible polynomials, and the number of different p-beat rhythms up to rotation.

    Try

    Constants as composed music

    Each convergent of pi is a Euclidean rhythm, and each refines the last: E(1,7), then E(16,113), then a 33,102-beat cycle. Layer them in the Polyrhythm layerer. The same works for any constant: √2 makes a rhythm built only from 2s, and e’s rhythm has a growing pattern.

    Try

    Tunings with a prime number of notes

    7 notes in a scale, the 31-note tuning Christiaan Huygens favored (its major third is within a cent of pure), and 127 are all Mersenne primes. 12, 41 and 53 come from the fifth’s ladder. Rank every tuning up to 200 notes by how well it does the fifth, the third and the seventh at once, then see whether prime sizes stand out. They probably won’t, but a clear “no” is still a result.

    Try

    The correlation spectrum

    Find the eigenvalues of the correlation matrix in section 10. They are the arithmetic version of a stock-market correlation spectrum. How far do they sit from random noise, and does the gap involve 6/π²?

    Try

    Two diffraction patterns at once

    Diffract the primes and pi’s rhythm on one strip (section 14). The primes peak at fractions with small square-free denominators, and pi peaks near multiples of 1/7. Where do the two patterns line up?

    Try

    A golden sunflower of primes

    Set the bar to φ − 1 and seed the sunflower of section 12 with primes. Its arms come in Fibonacci numbers, and primes skip the arms that share a factor with the arm count. The arm counts that are prime (2, 3, 5, 13, 89, 233, …) leave just one arm empty.

    Try

    Squares, shells and the zeta sum

    Shells hold 2, 8, 18, 32 electrons; hydrogen’s levels go as 1/n²; and ζ(2) = Σ 1/n² = π²/6 = Π 1/(1 − p⁻²). One picture could show the squares stacked as shells, their reciprocals adding up to π²/6, and the prime product beside them, labeled clearly as a shared formula rather than shared physics.

    Try

    A 113-note movement

    Write a pi movement of the Continued-Fraction Suite in a tuning with 113 notes per octave. 113 is prime, so every step size walks all 113 notes.

    @teddyrookbook GitHub Sources and credits