The Oldest Algorithm in the World Plays the Clave
Abstract
Euclid’s algorithm for computing greatest common divisors, applied to the problem of distributing k drum beats as evenly as possible among n time slots, produces patterns that can be compared with timelines documented in several musical traditions. The mathematical similarity does not establish a shared origin, cultural equivalence or historical transmission.
Contents
The first time I encountered the West African standard bell pattern it was in a Music and Physics seminar. The lecturer played a twelve-beat cycle on a wood block — seven strokes distributed unevenly but with a rightness that arrested the room. She then played the Cuban clave, the bossa nova timeline, a Bulgarian aksak rhythm. Different cultures, different instruments, different centuries. She asked whether there was a pattern. One modern mathematical description uses an algorithm from Euclid’s Elements, written in Alexandria around 300 BCE by a man whose life is barely documented. The music did not acquire its history from Euclid.
Euclid’s Algorithm
Every student who has taken a number theory course has encountered the algorithm for computing the greatest common divisor of two positive integers. Given $a \geq b$, repeatedly replace $(a, b)$ with $(b, a \bmod b)$ until the remainder is zero; the last non-zero remainder is the GCD.
For example, $\gcd(8, 3)$:
$$8 = 2 \times 3 + 2 \;\Rightarrow\; \gcd(8, 3) = \gcd(3, 2)$$$$3 = 1 \times 2 + 1 \;\Rightarrow\; \gcd(3, 2) = \gcd(2, 1)$$$$2 = 2 \times 1 + 0 \;\Rightarrow\; \gcd(2, 1) = 1.$$Three steps, result 1 (8 and 3 are coprime). The algorithm is efficient: in the worst case the number of steps grows in proportion to the number of digits of the smaller input. It is Propositions 1 and 2 of Book VII of Euclid’s Elements [[1]], the first for numbers with no common factor such as 8 and 3, the second for the rest, composed around 300 BCE [[2]]. That makes it one of the oldest surviving algorithms in the Greek mathematical record. Euclid subtracts rather than divides: “the less is continually subtracted in turn from the greater” [[1]]. Calling it “the oldest algorithm in the world” is the title’s joke, not a settled priority claim across all ancient mathematical traditions.
Distributing Onsets: Toussaint’s Observation
In 2005, Godfried Toussaint — a computer scientist and ethnomusicologist at McGill University — published the observation that the problem of distributing $k$ musical onsets as evenly as possible among $n$ time slots has the same recursive structure as Euclid’s algorithm applied to the pair $(k, n-k)$ [[2]]. He later developed the subject at book length [[3]].
An algorithm for this distribution problem had already arisen in accelerator timing. In a 2003 Los Alamos technical note on the timing system of the Spallation Neutron Source (SNS), Eric Bjorklund [[4]] needed to enable gates for components such as high-voltage power supplies in $k$ of $n$ equal time intervals, spread as evenly as possible [[2]], [[5]]. His grouping procedure, worked through below, has the same structure as Euclid’s algorithm and, like Euclid, works by repeated subtraction, as Toussaint noticed [[2]]. This is an algorithmic comparison, not evidence that a musical timeline, Euclid, and the accelerator system share a historical line of transmission.
The resulting pattern is denoted $E(k, n)$: the Euclidean rhythm with $k$ onsets distributed among $n$ pulses. A 1 denotes an onset; a 0 denotes a rest. Pulses are numbered from 0 and read as a cycle. A rotation starts the cycle on a different pulse: $[0,1,0,0,1,0,0,1]$ is the tresillo below, started one pulse early. Implementations of Bjorklund’s procedure can return different rotations; “canonical” here means the form Toussaint prints, which begins with an onset [[2]].
Working Through $E(3, 8)$: The Tresillo
Let us derive $E(3, 8)$ — 3 onsets distributed in 8 pulses — step by step.
Start: 3 onset groups and 5 rest groups:
$$[1]\; [1]\; [1]\; [0]\; [0]\; [0]\; [0]\; [0]$$Step 1: Distribute one rest group into each onset group, pairing until the shorter list is exhausted. Three pairs, with $5 - 3 = 2$ rest groups remaining:
$$[1,0]\; [1,0]\; [1,0]\; [0]\; [0]$$Step 2: Now 3 longer groups and 2 shorter groups. Distribute one shorter group into each longer group, $3 - 2 = 1$ longer group unpaired:
$$[1,0,0]\; [1,0,0]\; [1,0]$$Step 3: Only one group is left over, $[1,0]$, and Bjorklund stops here. One more round would insert it after one of the $[1,0,0]$ groups, which on a cycle changes nothing but the starting point [[2]]. Read the sequence left to right:
$$E(3, 8) = [1, 0, 0, 1, 0, 0, 1, 0].$$This is the Cuban tresillo — one of the foundational rhythmic cells of Afro-Cuban music, used across son, salsa, and mambo. Its onset positions are $\{0, 3, 6\}$, giving gap sizes $[3, 3, 2]$: two wide gaps and one narrow gap, arranged as evenly as the integers allow.
The parallel with Euclid’s algorithm lies in the group counts. Done by subtraction, the computation of $\gcd(8, 3)$ above runs $8 - 3 = 5$, $5 - 3 = 2$, $3 - 2 = 1$, $2 - 1 = 1$, $1 - 1 = 0$. The first subtraction gives the 5 rest groups of the start, the second the 2 groups left over after Step 1, the third the single group left over after Step 2. Grouped into divisions, $8 = 2 \times 3 + 2$ is the first two subtractions: its quotient 2 is the length of each onset group once every onset has a rest, the short gap, and its remainder 2 counts the leftover rests, which become the two long gaps. Step 2 is $3 = 1 \times 2 + 1$. The procedure then stops one division short, at the pair $(2, 1)$ instead of computing $2 = 2 \times 1 + 0$, because inserting a single leftover group would only rotate the cycle.
The Gap Structure
For any $E(k, n)$, the spacing between consecutive onsets takes at most two values:
$$\text{gap} \in \left\{\left\lfloor \frac{n}{k} \right\rfloor,\ \left\lceil \frac{n}{k} \right\rceil\right\}.$$When $k$ divides $n$ the two coincide and every gap is $n/k$; $E(4, 16)$ is four on the floor. The number of each gap size is determined by the constraint that all $k$ gaps sum to $n$. Writing $\alpha = n \bmod k$:
$$\alpha \cdot \left\lceil \frac{n}{k} \right\rceil \;+\; (k - \alpha) \cdot \left\lfloor \frac{n}{k} \right\rfloor = n.$$So $E(k,n)$ has $\alpha$ gaps of the larger size and $k - \alpha$ gaps of the smaller size.
Why at most two? Up to rotation, the onsets of $E(k, n)$ sit at
$$s_j = \left\lfloor \frac{j \cdot n}{k} \right\rfloor, \qquad j = 0, 1, \ldots, k-1. \tag{1}$$That Eq. (1) and Bjorklund’s procedure give the same cycle is a theorem of Demaine and colleagues [[5]]. Each gap is $s_{j+1} - s_j = \lfloor x + y \rfloor - \lfloor x \rfloor$ with $x = jn/k$ and $y = n/k$. If $k$ does not divide $n$, that difference is $\lfloor y \rfloor$ or $\lfloor y \rfloor + 1 = \lceil y \rceil$; if it does, $x$ and $y$ are integers and every gap is exactly $n/k$. The gap that closes the cycle, $n - s_{k-1} = \lceil n/k \rceil$, fits both cases.
Two gap sizes are necessary, not sufficient. Five onsets in twelve pulses need two gaps of 3 and three of 2; the cycle $[3, 3, 2, 2, 2]$ has them, but $E(5, 12)$ is $[3, 2, 3, 2, 2]$. The Euclidean property is that the two gap types are themselves distributed as evenly as possible — not clustered at one end of the cycle. Clough and Douthett’s maximally even sets [[6]] formalise this for scales; Eq. (1) is their construction, and Demaine and colleagues prove that the notions coincide [[5]]. For each $k \le n$ there is, up to rotation, one rhythm of maximum evenness, meaning the largest sum of straight-line (chordal) distances between all pairs of onsets on the circle; Bjorklund’s procedure and Eq. (1) both produce it; and in it any $\ell$ consecutive gaps span $\lfloor \ell n/k \rfloor$ or $\lceil \ell n/k \rceil$ pulses. The two adjacent 3s of $[3, 3, 2, 2, 2]$ span 6, more than $\lceil 24/5 \rceil = 5$. Maximising only the smallest adjacent gap does not single out a pattern: three different five-in-twelve cycles have smallest gap 2.
For $E(3, 8)$: $\lfloor 8/3 \rfloor = 2$, $\lceil 8/3 \rceil = 3$, $\alpha = 8 \bmod 3 = 2$. Two gaps of 3, one gap of 2. Gap sequence $[3, 3, 2]$. The asymmetry is the smallest deviation from perfect regularity that eight pulses allow. That describes the tresillo exactly. It does not explain why the tresillo sounds “right”, which is a question about listeners and traditions rather than about integers.
A Gallery of World Rhythms
The following table shows Euclidean rhythms alongside identifications from Toussaint’s paper [[2]] and the longer list of Demaine and colleagues [[5]]. Each pattern is in canonical form, and its gap sequence is read from the first onset around the cycle. Many traditions start the cycle elsewhere; “rotated” means that the identification refers to a rotation of the pattern shown.
| Pattern | Gap structure | Musical tradition |
|---|---|---|
| $E(2,3) = [1,0,1]$ | $[2,1]$ | Afro-Cuban conga pattern; West Africa; cueca |
| $E(3,8) = [1,0,0,1,0,0,1,0]$ | $[3,3,2]$ | Cuban tresillo (in the USA often called the habanera rhythm); Ewe atoke bell, Ghana |
| $E(5,8) = [1,0,1,1,0,1,1,0]$ | $[2,1,2,1,2]$ | Cuban cinquillo |
| $E(4,9) = [1,0,1,0,1,0,1,0,0]$ | $[2,2,2,3]$ | Turkish aksak |
| $E(7,12) = [1,0,1,1,0,1,0,1,1,0,1,0]$ | $[2,1,2,2,1,2,2]$ | West African bell (Ashanti mpre); rotated, the bembé bell |
| $E(5,16) = [1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0]$ | $[3,3,3,3,4]$ | Brazilian bossa nova, rotated |
Three of these are worth examining in more detail.
$E(5,8)$: the cinquillo. Five onsets in eight pulses: $\lfloor 8/5 \rfloor = 1$, $\lceil 8/5 \rceil = 2$, $\alpha = 3$. Three gaps of 2 and two gaps of 1. Gap sequence $[2,1,2,1,2]$ or a rotation thereof. The cinquillo is a fundamental pattern in Cuban music, used as a melodic rhythmic figure in the nineteenth-century contradanza and in much of what followed.
$E(7,12)$: the West African standard bell. Seven onsets in a twelve-beat cycle: $\lfloor 12/7 \rfloor = 1$, $\lceil 12/7 \rceil = 2$, $\alpha = 5$. Five gaps of 2 and two gaps of 1. As printed, $E(7,12)$ is the bell pattern of the Ashanti mpre [[2]]; the most widely known African bell timeline, which travels under its Cuban name bembé, $[1,0,1,0,1,1,0,1,0,1,0,1]$, is a rotation of it [[5]]. So is the diatonic scale. C major, $\{0, 2, 4, 5, 7, 9, 11\}$, is the bembé’s onset set [[5]], and it is seven consecutive notes of the circle of fifths, F–C–G–D–A–E–B: the orbit of $+7$ in $\mathbb{Z}_{12}$ from Twelve Is Not an Accident, stopped after seven notes. That is a statement about cyclic spacing, not equivalence between a bell timeline and a Western scale.
Toussaint compares the 3-in-8 distribution with timelines in several traditions. Binary similarity alone cannot show independent invention, common ancestry, or transmission. It also discards accent, swing, timbre, metre, tempo, and the social function that make a rhythm part of a particular practice.
Circular Notation and Necklaces
Euclidean rhythms are most naturally represented as necklaces — equivalence classes of binary sequences under cyclic rotation. Mathematically, rotations of $E(3,8)$ belong to one necklace. Musically, changing the downbeat can change accent, metre, and identity; necklace equivalence deliberately ignores that information.
In circular notation, place $n$ equally spaced dots on a circle and mark the $k$ onset positions; Toussaint joins consecutive onsets into a polygon and writes each gap beside its edge [[2]]. Fig. 1 draws four rhythms from the table this way. The pattern is immediately visible: the $k$ onset-dots divide the circle as evenly as possible. For $E(7,12)$, the seven onset dots on a twelve-position circle form a heptagon whose sides take only two lengths. For $E(3,8)$, the three onset dots form a triangle with two long sides and one short.
This geometric representation makes the maximum-evenness property transparent in a way that the linear binary string does not. It also shows the sense in which Euclidean rhythms are “balanced”: the onset dots distribute the “weight” of the cycle as uniformly as the integer constraints allow. The same circle serves a different problem in Star Polygons and Drum Machines: there a fixed step visits all $n$ positions in turn, rather than selecting $k$ of them.
The mathematical theory of necklaces belongs to combinatorics on words. For coprime counts and suitable upper/lower and rotation conventions, these balanced binary patterns are related to Christoffel words [[7]]: words over the alphabet $\{0,1\}$ whose combinatorial properties encode the slope of a line segment, which brings us to the third independent context in which the same algorithm appears.
The Bresenham Connection
Jack Bresenham’s line algorithm (1965) [[8]] rasterises a line from $(0,0)$ to $(n,k)$, $k \le n$, column by column. It tracks how far the true line has risen since the last row increment; when that error exceeds $\frac{1}{2}$, it increments the row and subtracts one, so that each column gets the row nearest the line. The columns at which the row increments mark $k$ onsets among $n$ pulses.
Taking the row at or below the line instead, $\lfloor mk/n \rfloor$, puts an onset at position $m$ if and only if the line crosses a row between columns $m$ and $m+1$:
$$\left\lfloor \frac{(m+1)\, k}{n} \right\rfloor > \left\lfloor \frac{m\, k}{n} \right\rfloor. \tag{2}$$Fig. 2 shows this for $E(3,8)$. The line from $(0,0)$ to $(8,3)$ reaches heights 1, 2 and 3 at $x = 8/3$, $16/3$ and $8$, so Eq. (2) gives $\{2, 5, 7\}$. Eq. (1) rounds the crossings of heights 0, 1 and 2 down and gives $\{0, 2, 5\}$. Read the same way, with an onset wherever the row steps up, the nearest-row rule gives $\{1, 4, 6\}$, or $\{1, 3, 6\}$ if it breaks the tie at $x = 4$ upwards. All are rotations of the tresillo $\{0, 3, 6\}$. I checked that Eq. (1), Eq. (2), both tie rules and Bjorklund’s procedure give the same necklace for all $1 \le k \le n \le 24$; Demaine and colleagues, citing Harris and Reingold, note that Bresenham’s algorithm is itself described by the Euclidean algorithm [[5]].
The retrospective comparison joins ancient Greek number theory, binary rhythm analysis, accelerator timing, and computer graphics around a discrete distribution problem. It does not imply that performers generated their music by executing Euclid’s or Bresenham’s procedure.
The Clave Is One Pulse Away
The title promised the clave, and the clave is where the Euclidean description stops. The son clave, “the ubiquitous two-bar clave Son” in Toussaint’s phrase, opens with the tresillo [[2]] and strikes at pulses 0, 3, 6, 10 and 12 of sixteen [[9]]. Its gaps are $[3,3,4,2,4]$: three sizes, so it is not a Euclidean rhythm. Started on its third onset, $E(5,16)$ becomes the bossa nova timeline, onsets 0, 3, 6, 10 and 13 and gaps $[3,3,4,3,3]$ [[2]], which Toussaint notes is maximally even [[9]].
Fig. 3 puts the two side by side. On two graded measures the son clave loses little. On Demaine and colleagues’ straight-line measure it is still the second most even of six common clave and bell timelines, after the bossa nova [[5]]. In Toussaint’s comparison of the same six, where each rhythm is reduced to its sequence of gaps, the son has the smallest summed distance to the other five, which he offers as a possible explanation for its worldwide popularity [[9]].
So the oldest algorithm plays the clave’s first bar and the whole bossa nova. The son clave it misses by one pulse.
Euclidean Rhythms in Contemporary Practice
Toussaint’s 2005 paper was a contribution to computational ethnomusicology. Euclidean rhythm generators also appear in hardware and software sequencers. A common interface exposes $k$, $n$, and a rotation offset. Product availability and implementation conventions vary, so this is a design pattern rather than a timeless inventory.
This has opened a compositional mode in which the mathematical structure is operational: a producer can stack several generated patterns and listen to their interaction. Calling the result “African-style” from binary spacing alone would erase the performance practices the algorithm does not encode.
Implications for Teaching Rhythm
Music conservatories in the European tradition teach rhythm almost entirely through Western notation: time signatures, note values, dotted notes, ties. This system is well-suited to the repertoire it was designed for. It handles Euclidean rhythms awkwardly. In 4/4 the tresillo $E(3,8)$ is written as dotted quarter, dotted quarter, quarter: $3 + 3 + 2$ eighths, onsets $0$, $3/2$ and $3$ beats into the bar. That notation correctly represents the sound but obscures the structural principle entirely. It is not a triplet either: three equal notes across the bar would fall at $0$, $4/3$ and $8/3$ beats, and rounding those up to the eighth-note grid gives the tresillo. That rounding is what $E(3,8)$ computes [[5]].
The Euclidean framework suggests a different pedagogical starting point. Rather than beginning from the bar line and asking how notes fill it, begin from the cycle length $n$ and the onset count $k$ and ask how to distribute the onsets as evenly as possible. The answer is computable, but whether the result is recognised as a particular rhythm depends on rotation, accent, tempo, and context.
For students who encounter West African, Afro-Cuban, or Middle Eastern music — which conservatory students increasingly do — having a framework that presents these rhythms as cyclic structures rather than deviations from one notational norm can be useful. In that framework the tresillo is a maximally even 3-in-8 necklace, with rotation and performance details still to choose. The mathematics explains the spacing criterion. It does not explain why a culturally situated performance sounds right.
Whether conservatory curricula are ready to incorporate the Euclidean framework alongside Western notation is a separate question. The mathematics does not demand it. But it offers a language for rhythm that transcends the Western bar-line without abandoning precision — and that seems worth something, especially in a world where the music students will perform and teach is no longer exclusively European.
Mathematical and music-historical sources checked through 2026-10-24.
References
[1] Euclid. Elements, Book VII, Propositions 1 and 2. In D. E. Joyce (Ed.), Euclid’s Elements (online edition). Clark University. https://mathcs.clarku.edu/~djoyce/elements/bookVII/propVII1.html, https://mathcs.clarku.edu/~djoyce/elements/bookVII/propVII2.html
[2] Toussaint, G. T. (2005). The Euclidean algorithm generates traditional musical rhythms. In R. Sarhangi & R. V. Moody (Eds.), Renaissance Banff: Mathematics, Music, Art, Culture (Bridges 2005 Conference Proceedings, pp. 47–56). https://archive.bridgesmathart.org/2005/bridges2005-47.html
[3] Toussaint, G. T. (2020). The Geometry of Musical Rhythm: What Makes a “Good” Rhythm Good? (2nd ed.). Chapman & Hall/CRC Press. DOI: 10.1201/9781351247771
[4] Bjorklund, E. (2003). The theory of rep-rate pattern generation in the SNS timing system (SNS ASD Technical Note SNS-NOTE-CNTRL-99). Los Alamos National Laboratory.
[5] Demaine, E. D., Gomez-Martin, F., Meijer, H., Rappaport, D., Taslakian, P., Toussaint, G. T., Winograd, T., & Wood, D. R. (2009). The distance geometry of music. Computational Geometry, 42(5), 429–454. DOI: 10.1016/j.comgeo.2008.04.005 (preprint: arXiv:0705.4085)
[6] Clough, J., & Douthett, J. (1991). Maximally even sets. Journal of Music Theory, 35(1–2), 93–173. DOI: 10.2307/843811
[7] Lothaire, M. (2002). Algebraic Combinatorics on Words. Cambridge University Press. DOI: 10.1017/CBO9781107326019
[8] Bresenham, J. E. (1965). Algorithm for computer control of a digital plotter. IBM Systems Journal, 4(1), 25–30. DOI: 10.1147/sj.41.0025
[9] Toussaint, G. T. (2002). A mathematical analysis of African, Brazilian, and Cuban clave rhythms. In Bridges: Mathematical Connections in Art, Music, and Science (pp. 157–168). https://archive.bridgesmathart.org/2002/bridges2002-157.html
Changelog
- 2026-07-11: Corrected the algorithm-priority, maximal-evenness, necklace, Christoffel-word, product, and teaching claims; removed erroneous table rows; and made cultural timeline comparisons explicitly non-genealogical.
- 2026-10-03: Corrected the Bresenham section: the increment condition gives onset positions {2, 5, 7} for $E(3,8)$ while $\lfloor jn/k\rfloor$ gives {0, 2, 5}, so the two are equal only up to rotation (checked for all $1\le k\le n\le 24$), not “exactly” identical.
- 2026-10-24: Corrected the derivation: the quotient 2 → the short gap, not the pairing steps before the remainder (one); “no further pairing is possible” → one more round would only rotate the cycle. “Exactly two” gap sizes → at most two, with a proof. “Maximum-evenness is why the tresillo sounds ‘right’” and “feel ‘balanced’ when played” → descriptions, matching the closing section. “A triplet feel” → removed: the tresillo’s onsets fall at 0, 1.5 and 3 beats, a triplet’s at 0, 1.33 and 2.67. Euclid “died around 270 BCE” → his life is barely documented (MacTutor: “little is known”; about 325 – about 265 BC). Table: canonical rotations, no asterisks; “Iambic foot”, “Balkan” and “Flamenco” (unsupported by the sources) → their identifications. Corrected Bjorklund’s problem and report, Bresenham’s error step and the Bridges 2005 editors. Added the son clave, the uniqueness theorem and three figures.