The Babylonian method might be the first known algorithm for approximating square roots. In this post we’re going to visualize its amazing geometric interpretation.

Contents:

Exposition belongs on Wikipedia

So, in case you’re not aware, there’s a Babylonian clay tablet from ~1700 BCE with an incredibly accurate approximation of \(\sqrt{2}\) written on it [Wiki]. This is a big thing: it seems like they knew it’s just an approximation of (what we call today) an irrational number. More than 1,000 years later, the Pythagoreans allegedly drowned Hippasus because he rediscovered this fact [more Wiki]. Obviously Math.sqrt(2) didn’t exist yet, so how did they calculate it?

But… Pythagoras was Greek!

To approximate \(\sqrt{2}\) like they do in Babylon we need to think like the ancients, and they were really big on geometry. What’s \(\sqrt{2}\) in geometry? Well, the usual suspect is the hypotenuse of a right triangle with two legs of length 1.

Let’s draw one in p5.js: Most of the numbers you see in this code point to locations in that gray canvas on the right. We start from the top-left and count in pixels. Since pixels are so small, let’s say that 100 of them are worth one unit of length. Then, by the Pythagorean theorem,

\[c^2 = a^2 + b^2 = 1^2+1^2\] \[c = \sqrt{2}.\]

There are over 300 proofs of Pythagoras’ theorem, and if you’ve seen a few of them you know that \(c^2\) usually comes into play as the area of a square sitting on the hypotenuse. Da Vinci’s proof is one example of this, expertly animated on YouTube.

The Babylonian method is all about finding this square with area 2:

Step 1: start with a rectangle of area 2, but one with side lengths that you can actually calculate, like 1 for the short side and 2 for the long side.

Step 2: the short side is obviously too short, so let’s make it a bit bigger. How much bigger? Let’s just split the difference.

OK, but now the rectangle is too big! Its area is new_side*b and that’s surely more than 2. Patience please, we’re not done yet.

Step 3: shorten that long side so that the total area is still 2.

Are the sides equal?

  • Yes? Congratulations, you just found a square of area 2. Conveniently, you also know its side length, new_side, that’s your \(\sqrt{2}\).
  • Oh, they’re not? Then back to Step 2: make the short side a bit bigger…

This is all you need to see, basically

Let’s see the method in play. I already drew that first rectangle, it’s up to you to make it (click a bunch of times). Also useful:

You can run this sketch yourself in the p5.js web editor BTW.

Up for a challenge? Rewrite the code to calculate the square root of any given x. If you still draw squares after a few iterations, it works.

Are you sure it works? Did you check with x smaller than 1?

Final thoughts and random sqrt trivia

  • We only used basic arithmetic for the calculations, cool huh? Even cooler is the fact that almost 4,000 years later, the algorithm behind Math.sqrt(2) is not that much different.
  • Does it always work? Yes. Why? [Wiki].
  • Look at this line of code:
     i  = * ( long * ) &y; // evil floating point bit level hacking
    

    This is part of the “Quake III” algorithm for the (inverse) square root, and it gets even better. I obviously can’t write about approximating square roots without mentioning this immortal stroke of genius. Read up on it [Wiki], or watch this great video if you’re not afraid of C and its pointers, but at the very least, read this xkcd comic.

  • Did you know that the A series of paper sizes, which includes the GOAT, A4 paper, is designed such that the long side is exactly \(\sqrt{2}\) times the length of the short side. Why? It has to do with preserving the ratio when you fold it in half [Wiki].