An introduction to equal temperament.
You have been given a keyboard. Inconveniently, this keyboard has not been tuned. You'd like to play it, and therefore decide to tune it yourself. Luckily, the mechanics of tuning this keyboard are easy... you just need to work out which frequency to assign to each note...
The octave doubling rule
You try to recollect any relevant info you might have picked up over the years.. and remember the following:
A note an octave up, has double the frequency.
In other words, if note C5 has frequency f, then note C6 will have frequency 2*f. As far as you remember, this should apply to all the notes on the keyboard.
It follows from this that the note an octave below must have half the frequency. So C4 would have a frequency of f/2.
Great! But where to go from here?..
The first frequency
You allow your eyes to wander up and down the 88 key keyboard...
You realise that you could pick a relatively middling key on the keyboard, and assign to this a relatively middling frequency.
You choose A4 to be your middling key. You then play around with a tone generator, and decide that you perceive ~440Hz to be a fairly middling frequency.
You turn on your piano, and program in freq(A4) = 440Hz.
Great! You have now tuned one note!
All the As
Now that you have decided on a frequency for A4, you realise that you can work out the frequencies of all the other As, using the octave doubling rule:
A4 <- 440 Hz
A5 <- 2*440 Hz
A6 <- 4*440 Hz
A3 <- 440/2 Hz
A2 <- 440/4 Hz
So you program these in...
Great! Now you have tuned all of your As!
Join the dots
You have all the As tuned, but you're not sure what to do about the other notes.
You wonder if a graph might help, so you painstakingly draw out the following:
Looking at this graph, you realise that drawing a curve through the points would assign a frequency to each key on the keyboard. However, drawing the curve looks somewhat difficult..
On a whim, you decide to plot the points again, but this time using logarithmic graph paper:
Aha! Now you can use a ruler to draw a straight line through the points:
If you want to find the frequency of a certain key, you can read it off the graph!
You wonder if the octave doubling rule still applies to any note, or just to the As. You read a few values from the graph, and it seems like it does apply to all notes! "Hurrah!" you shout.
Great! By reading from the graph, you can now tune all the notes on your piano!
Finding the expression
Unfortunately, you don't enjoy reading values from the graph. It's slow and imprecise.
"If only I had a mathematical expression for this curve", you think to yourself, "then I could accurately compute the frequencies for this keyboard!"...
You write out the frequencies of the A keys on your keyboard, and re-number them:
You notice that 880 is 440 * 21, and that 1760 is 440 *22, and that 3520 is 440 *23... And realise that you can write the exponent in terms of k...
freq = 440 * 2k-5
You double check that this works for k=4, k=3 and k=1... and it does!
Now you write a table, showing how the 'k' number maps to the keyboard number (e.g. A4 is note number 49 on the keyboard, A5 is 61, etc.):
You play around, and discover that n = (k-1)*12 + 1.
You realise you need it the other way around, and re-arrange to: k = (n-1)/12 + 1.
You realise you can sub this into your expression for freq!
freq = 440 * 2(n-1)/12 + 1 - 5
Some more re-arranging and you get:
freq = 440 * 2(n-49)/12
Using this expression, you compute the frequency for all 88 keys on your keyboard accurately and conveniently!
You have succesfully (theoretically) tuned your keyboard!
You started with the octave doubling rule.
You selected a key on the keyboard, and assigned it a frequency, fairly arbitrarily, but also fairly sensibly (a middling frequency for middling key).
You were then able to fit a curve, which gave a sensible tuning for the whole keyboard.
This tuning is called twelve-tone equal temperament. It's "the most common tuning system since the 18th century"  (but not the only option available )..
Preserved frequency ratios
A property of this tuning system is that a major third sounds like a major third wherever it is played on the piano. A fifth sounds like a fifth wherever it is played on the piano. A fourth sounds like a fourth wherever it is played on the piano.. As you move a given interval up and down the piano, the pitch changes, but the interval always sounds like that interval.
Let's try and find an explanation for this...
Pick a key on the keyboard, and call it 'n'.
Now pick a key 'k' semitones above 'n' on the keyboard, and call it 'n+k'.
You can write the ratio of frequencies for two notes as:
r = freq(n+k)/freq(n)
Let's plug in our expression for 'freq' from above:
r =(440 * 2(n+k-49)/12) / (440 * 2(n-49)/12)
r = 2(n+k-49)/12 / 2(n-49)/12
r = 2(n+k-49)/12 - (n-49)/12
r = 2(n+k-49-n+49)/12
r = 2k/12
The 'n's have cancelled out..! In other words, the ratio of frequencies, 'r', only depends on the number of semitones between the two notes. This explains why a fifth sounds like a fifth even as you move it up and down the piano: the 'n' changes as you move it, but the 'k' stays the same, and hence the ratio of frequencies stays the same. This holds true for all intervals in this tuning system.
Other tuning systems lack this property, meaning that a given interval can sound more like the interval, or less like the interval, as you move it up and down the keyboard, because the frequency ratio doesn't stay constant. For certain notes and intervals these tuning systems may sound 'better' than equal temperament, but then for other notes and intervals the sound will be 'worse.
The reader should note that real world pianos deviate from this tuning . And that the author has never tuned an acoustic piano.
References / further reading:
 Hacker News discussion of this article
First published 03.03.2019. Last edited 25.09.2019.