Commissioned for this museum · after Euclid, Elements Book VII, c. 300 BC
The Oldest One Here
TypeScript·1956·13 lines·275 bytes
/**
* Euclid's algorithm — the greatest common divisor, by taking remainders
* until nothing is left over.
*/
function gcd(a: number, b: number): number {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
console.log(gcd(1071, 462));
console.log(gcd(270, 192));Curator’s note
Everything else in this museum was written by someone whose name we know, in a language that still exists. This one is about twenty-three centuries old and predates the word algorithm by a thousand years, the machines it runs on by two thousand, and the notion that arithmetic could be *mechanical* by rather longer than that.
Euclid did not write a loop. Book VII of the *Elements* describes repeatedly subtracting the smaller quantity from the larger, phrased in terms of measured lengths, because Greek mathematics worked in geometry rather than numbers. The version here replaces repeated subtraction with a remainder, which is the same process with the repetition folded into one operation — the only real modernisation in four lines, and it is an optimisation Euclid would have recognised immediately.
What earns it a wall is that nothing has replaced it. This is not a historical curiosity kept for sentiment. It is still the method: your cryptography library computes greatest common divisors this way, in the extended form that also produces the modular inverse, every time it generates an RSA key. Two and a half thousand years is an unreasonably long service record for anything, and the code has barely changed shape.
The year on the plaque is 1956 rather than 300 BC, because that is when Dijkstra gave the algorithm the form the loop takes here. The idea is Euclid's. The three lines are not quite.