The Mathematics Behind Detroit

The Mathematics Behind Detroit

An algorithm written to time a neutron accelerator turns out to generate the rhythms of West Africa, Cuba and Brazil — and it explains why a well-programmed electro pattern refuses to sit still. Here is how it works, and how to use it.

Put five kicks in a bar of sixteen steps. Not four — five. There is no obvious way to do it. Four divides sixteen cleanly and gives you four-to-the-floor; five does not divide anything, so you have to make a decision about where the awkward gap goes.

Most producers make that decision by ear. Move a step, listen, move it back. After twenty years you develop instincts and stop thinking about it.

It turns out there is exactly one arrangement that spreads those five hits as evenly as sixteen steps allow, and there is an algorithm that finds it in a few operations. It was not written for music. It was written in the 1990s to fire the timing gates on a particle accelerator.

A problem in nuclear physics

Eric Bjorklund worked on the timing system for the Spallation Neutron Source, a particle accelerator facility. Certain components — high-voltage power supplies among them — needed to be switched on during some time intervals and not others. Given a number of intervals and a smaller number of pulses, the pulses had to be distributed as evenly as possible across those intervals. Bursting them together would stress the hardware. Spacing them out kept the load steady.

He described the problem as a string of ones and zeros: ones where a pulse fires, zeros where it does not. Build a sequence of n bits containing k ones, with the ones spread as evenly as the arithmetic permits. Then he wrote an algorithm that constructs it directly, without searching.

In 2005, the computer scientist Godfried Toussaint noticed something. He ran Bjorklund’s algorithm across dozens of values and found that the sequences it produced were not abstract at all — they were the rhythmic timelines of traditional world music, sub-Saharan African music in particular. He named them Euclidean rhythms, because the structure of the algorithm is the same as Euclid’s method for finding the greatest common divisor of two numbers, written down around 300 BC.

A timing routine for an accelerator, an algorithm from ancient Greece, and the bell patterns of West Africa are the same object seen from three directions.

How it actually works

The method is easier to do than to describe. Start with your ones and your zeros in two separate piles. Repeatedly pair each item in the larger pile with one from the smaller, gluing them into single units. Keep going until the remainder is one group or less. Read the result left to right.

Here is E(5,16) — five onsets across sixteen steps — worked through completely.

Start xxxxx ...........
Pair x.x.x.x.x. ......
Pair x..x..x..x..x.. .
Stop x..x..x..x..x…
The remainder is down to a single group, so the process stops. E(5,16) = x . . x . . x . . x . . x . . . — onsets at steps 0, 3, 6, 9 and 12. Four gaps of three, one gap of four.

The remainder is down to a single group, so the process stops. Onsets land on steps 0, 3, 6, 9 and 12 — four gaps of three, one gap of four.

Why Euclid? Look at the arithmetic hiding in those three lines. Sixteen divided by five gives three with one left over; five divided by one gives five with nothing left over. That is precisely the sequence of divisions Euclid’s algorithm performs to establish that 16 and 5 share no common factor. The rhythm is a by-product of a division you were already doing.

One practical note: implementations differ in exactly where they stop, so you will see E(5,16) published as x..x..x..x...x.. elsewhere. That is the same necklace read from a different starting point. Because the pattern is a loop, rotation does not change what it is — but it very much changes what it feels like, which is a point we will come back to.

Hear it

LOGOS MACHINE · EUCLIDEAN STOPPED
129
Click a voice name to mute it. The 1/8 button halves a voice's resolution — that is what turns E(3,8) into the tresillo rather than a double-time figure. Try this: load ELECTRO, then switch the kick to 1/16 and hear it collapse into something much closer to techno.

Four voices, each with its own onset count and its own bar length. Change k to add or remove hits — the algorithm redistributes everything instantly. Change rot to spin the pattern without altering its shape. Change n to put a voice into a different meter from the rest.

Why it sounds like Detroit

Electronic dance music lives on a sixteen-step grid. That grid is the whole reason Euclidean patterns behave the way they do here, and the reason four of the five possible onset counts are interesting while one is not.

Sixteen is 2⁴. Any onset count that shares a factor with it — 2, 4, 8 — divides the bar into equal blocks. E(4,16) is four-to-the-floor: four gaps of four, perfectly symmetric, the pattern that anchors a track precisely because it carries no information. It tells you where you are and nothing else.

Every odd onset count is coprime with sixteen, and something different happens. The pattern cannot be decomposed into repeating blocks, so no group of onsets recurs inside the bar. E(5,16) gives four strides of three and then one of four: the ear locks onto a triplet-feeling pulse, follows it across the bar, and gets caught by the extra step at the end. Then it starts again.

Maximal evenness is the mathematical name for what a producer means by “it grooves but it doesn’t sit down.”

That tension — a subdivision the bar cannot accommodate — is syncopation. Not swing, which pushes notes off the grid in time, but structural syncopation, which keeps every hit exactly on a step and still refuses to align with the four-beat frame.

Resolution is part of the pattern

E(k,n) says nothing about how fast those n steps go by, and the same notation covers two completely different rhythms depending on whether a step is a sixteenth note or an eighth.

Take E(3,8). Read at sixteenth-note resolution it fills half a bar and repeats twice — busy and rolling, a techno figure. Read at eighth-note resolution it fills the bar exactly, with onsets on the one, the “and” of two, and the “and” of three. That is the tresillo, and it is the backbone of electro: the broken kick that runs from Planet Rock through Cybotron and Drexciya and out the other side.

Put that kick against a clap on two and four and you have the genre in two lines. Now swap the kick for E(4,16) — four to the floor — and keep everything else identical. Same clap, same hats, same bass, and the track reads as techno instead. The Euclidean patterns did not change. The grid underneath one of them did.

Now consider where Detroit producers were working. Model 500, Underground Resistance, Drexciya, Aux 88 — these records were programmed on step sequencers with sixteen buttons in a row. Nobody was running Bjorklund’s algorithm. But when you are spreading five or seven hits across a sixteen-step machine and moving them by ear until they feel right, maximal evenness is what “right” converges on. The algorithm did not invent those patterns. It describes what your hands already find.

The same argument explains why Euclidean patterns appear across so much of the world’s music. They are not a style. They are what you get when a small number of events has to be spread across a cycle that does not divide.

Patterns worth knowing

A handful of these come up constantly. The names come from the traditions where they were documented long before anyone connected them to an algorithm.

PatternStepsKnown as / use
E(2,5)x.x..Persian and Balkan five-beat figures
E(3,8)x..x..x.The tresillo. The single most useful pattern in electronic music — put it on a bassline and it works
E(5,8)x.xx.xx.The cinquillo, from Cuban music. Dense; excellent for closed hats
E(4,16)x…x…x…x…Four-to-the-floor. The degenerate case — perfectly even, no tension
E(5,12)x..x.x..x.x.A South African bell timeline. Sits beautifully over a 4/4 kick
E(5,16)x..x..x..x..x…Documented in Brazilian music; the core electro figure
E(7,16)x..x.x.x..x.x.x.Samba-related. Ideal hat or ride pattern under a straight kick
E(9,16)x.xx.x.x.xx.x.x.Busy and propulsive — shakers, ghost percussion, gated stabs

Using this in a track

Rotation is the real control

Onset count determines the pattern’s shape; rotation determines its relationship to the downbeat. E(3,8) starting on step 0 is a familiar, settled figure. The same necklace rotated by two steps starts in a hole and feels like it is falling forward. Same maths, completely different function in an arrangement. When a Euclidean pattern sounds wrong, it is usually rotated wrong rather than counted wrong.

Anchor first, then complicate

A pattern with no even reference to push against just sounds unstable. Keep something plain — a straight kick, or an offbeat hat on every second step — and let the Euclidean voices work against it. The tension needs both sides to exist.

Use different bar lengths, not just different counts

Two Euclidean patterns sharing the same n repeat together every bar, which caps how far the interest can go. Give them different lengths and the composite cycle stretches to the least common multiple: E(7,16) against E(5,12) takes 48 steps to realign — three bars of 4/4 before the combination repeats. That is where the hypnotic quality of a lot of Detroit and Berlin techno comes from, and it costs nothing to program.

Coprime counts on the same grid

If you want to stay in sixteen, pick onset counts that share no factors with each other: 3 and 5, 5 and 7, 7 and 9. Their accent points collide rarely and unpredictably, which keeps a two-element loop alive far longer than it has any right to be.

It is a starting point, not a finish

The algorithm gives you the maximally even solution. It has no opinion about your track. Generate the pattern, then move one hit, drop a step, add a ghost note. Some of the best results come from a Euclidean skeleton with a single deliberate error in it — the structure holds the groove together while the error gives it a signature.

Machines were always mathematical

There is an old argument that programmed music is cold because it is calculated. The history here points the other way. The most human rhythms we have — bell patterns carried orally across centuries in West Africa, Cuban clave, Brazilian timelines — turn out to sit on the same mathematical structure that a physicist reached for when he needed to fire pulses evenly at an accelerator.

The maths was never opposed to the feel. It is a description of it. Detroit producers found these patterns with their fingers on a sixteen-step sequencer; Bjorklund found them with a pencil; Euclid wrote down the underlying procedure twenty-three centuries earlier without knowing any of the rest. What that should tell you is not that rhythm is mechanical, but that some things feel right for reasons you can actually write down.

Which does not make the sequencer above a substitute for taste. It just means you can stop hunting for the pattern and start deciding what to do with it.

Sources

E. Bjorklund — The Theory of Rep-Rate Pattern Generation in the SNS Timing System, Los Alamos National Laboratory

G. T. Toussaint — The Euclidean Algorithm Generates Traditional Musical Rhythms, BRIDGES, Banff, 2005