Pluck a string, then pluck another one exactly two-thirds as long. The second note vibrates 3 times for every 2 vibrations of the first. That 3:2 ratio is a perfect fifth, the interval at the start of "Twinkle, Twinkle, Little Star" (between the first "twinkle" and the second). Halve the string and you get 2:1, an octave: a note so similar to the original that we give it the same letter name.
These two ratios are the backbone of almost every musical tradition. So here is a natural plan for building a scale: start on one note, keep stacking pure fifths, and fold every result back into one octave. You collect new notes one by one, and eventually you should arrive back where you started, with a neat, closed set of notes.
That plan fails, and the way it fails is the reason a piano has twelve keys per octave.
The circle below is one octave. Going once around doubles the frequency, so every C sits at the top. Each tap adds another pure 3:2 fifth and folds it back into the circle. Watch what happens around the twelfth fifth.
After twelve fifths you land almost exactly on the starting note, but not quite. You overshoot by a sliver called the Pythagorean comma. In numbers: twelve fifths is (3/2)12 ≈ 129.75, while seven octaves is 27 = 128. The ratio between them, 531441:524288, is about 23.5 cents, roughly a quarter of a semitone.
No amount of stacking ever closes the loop exactly. Powers of 3 are odd and powers of 2 are even, so 3a can never equal 2b. Pure fifths spiral around the octave forever without landing on their starting point.
If you can't close the circle, you can cheat: chop the octave into N equal steps and let the fifth be whichever step lands closest to 3:2. That's equal temperament. The question is which N gives a good fifth without an absurd number of keys.
Small numbers are bad: with 5 or 7 notes the best fifth is off by 16–18 cents, which sounds clearly sour. Then 12 appears and the error suddenly drops to about 2 cents. The twelve-note fifth is 700 cents instead of 701.96, a difference most listeners can't hear in a melody.
There's a mathematical reason. A pure fifth is log2(1.5) ≈ 0.58496 of an octave. The fraction 7/12 = 0.58333 happens to be an unusually good approximation for such a small denominator. The next ones that beat it are 24/41 and 31/53, which would need 41 or 53 keys per octave. Twelve is the sweet spot where the numbers happen to line up with what a hand can play.
Equal temperament spreads the Pythagorean comma evenly: each of the twelve fifths is shaved by about 2 cents, so twelve of them add up to exactly seven octaves and the circle finally closes. The reward is huge. Every key is identical, so a song can move to any key and sound the same, just higher or lower.
But nothing is free. The major third, which ideally is a 5:4 ratio (386 cents), becomes 400 cents in equal temperament, almost 14 cents sharp. Play a sustained chord on a well-tuned piano and listen closely: the slight shimmer or wobble you hear is partly those compromised intervals, whose overtones drift in and out of step. Even the tempered fifth wobbles gently: tune A to 220 Hz and the tempered E above it, and their overtones near 660 Hz beat roughly three times every four seconds.
Singers, violinists and barbershop quartets, who can bend pitch freely, often drift toward the purer ratios by ear. A piano can't, so its builders chose consistency over purity.
The idea of dividing the octave into twelve equal semitones was worked out precisely in 1584 by the Chinese prince and scholar Zhu Zaiyu, who computed the twelfth root of 2 (about 1.059463) with remarkable precision. In Europe, the Flemish mathematician Simon Stevin described the same system around 1585 with less precise numbers; his manuscript wasn't published until the 1880s. Equal temperament only became the usual way to tune European keyboards much later, during the 1800s.
So every time you look at a piano, the pattern of 7 white and 5 black keys is a frozen answer to an old number puzzle: how to get as close as possible to 3a = 2b, which can never be solved exactly.