Heron's method for square roots, from the Metrica, in Python
Heron's method for square roots as Heron of Alexandria works it in Metrica I.8 on the root of 720, with the Greek, tested Python and the tie to Newton's method.
What is the square root of 720? Heron of Alexandria needed it for the area of a triangle with sides 7, 8 and 9, and in his Metrica, Book I, chapter 8, he finds 26 5/6, which squared is off by 1/36. His rule is now called Heron’s method: average your guess with the number divided by the guess, and repeat. Each step roughly doubles the number of correct digits.
The same rule is Newton’s method applied to x * x - n. I read Heron’s Greek in Schöne’s 1903 edition and Heath’s 1921 English, reworked his example, and coded it in Python with tests (heron.py on GitHub). It is part of Ancient algorithms, implemented.
The problem Heron needed to solve
Metrica means “measurements”. It is a surveyor’s handbook for areas and volumes. In I.8 Heron gives the rule now called Heron’s formula: a triangle with sides a, b, c and half-perimeter s has area sqrt(s(s - a)(s - b)(s - c)).
His example is the triangle with sides 7, 8 and 9. The half-perimeter is 12, so the area is the square root of 12 × 5 × 4 × 3 = 720. 720 is not a square number and its root is irrational, so Heron approximates it.
Who wrote it, and when
- Heron of Alexandria. His date is uncertain. Heath (1921, vol. 2, pp. 298 to 306) reviews the evidence and reports the then-current conclusion, which Heiberg accepted, that Heron lived in the 3rd century CE. In 1938 Otto Neugebauer matched a lunar eclipse that Heron uses as an example in his Dioptra to the eclipse of 13 March 62 CE, which put Heron in the 1st century CE, and MacTutor gives his life as c. 10 to c. 75 CE. Ramon Masià (2015) argues that the eclipse may have been picked as a worked example, so it may not date Heron (as summarized in the zbMATH review). The 1st-century date in this series is the usual one, and Masià’s objection leaves it open. I have read neither paper in full, only their records and reviews.
- The Metrica. Until 1896 it was known only from an allusion in Eutocius’s commentary on Archimedes’s Measurement of a Circle, which says Heron shows in the Metrica how to approximate the square root of a non-square number (Heath 1921, vol. 2, p. 317). That year R. Schöne found an 11th- or 12th-century manuscript of it at Constantinople, and his son H. Schöne edited it, with a German translation, in Heronis Alexandrini opera, vol. 3 (Teubner, 1903) (Heath, p. 308). The passage is on pp. 18.22 to 20.5, from folio 70 verso of the manuscript.
- The English is Heath’s, A History of Greek Mathematics, vol. 2 (Oxford, 1921), p. 324, in the public domain.
The source text
Schöne’s Greek, Metrica I.8:1
ἐπεὶ οὖν αἱ ψκ ῥητὴν τὴν πλευρὰν οὐκ ἔχουσι, ληψόμεθα μετὰ διαφόρου ἐλαχίστου τὴν πλευρὰν οὕτως· ἐπεὶ ὁ συνεγγίζων τῷ ψκ τετράγωνός ἐστιν ὁ ψκθ καὶ πλευρὰν ἔχει τὸν κζ, μέρισον τὰς ψκ εἰς τὸν κζ· γίγνεται κϛ καὶ τρίτα δύο· πρόσθες τὰς κζ· γίγνεται νγ τρίτα δύο. τούτων τὸ ἥμισυ· γίγνεται κϛ ∠ γʹ. ἔσται ἄρα τοῦ ψκ ἡ πλευρὰ ἔγγιστα τὰ κϛ ∠ γʹ. τὰ γὰρ κϛ ∠ γʹ ἐφ᾽ ἑαυτὰ γίγνεται ψκ λϛʹ· ὥστε τὸ διάφορον μονάδος ἐστὶ μόριον λϛʹ. ἐὰν δὲ βουλώμεθα ἐν ἐλάσσονι μορίῳ τοῦ λϛʹ τὴν διαφορὰν γίγνεσθαι, ἀντὶ τοῦ ψκθ τάξομεν τὰ νῦν εὑρεθέντα ψκ καὶ λϛʹ, καὶ ταὐτὰ ποιήσαντες εὑρήσομεν πολλῷ ἐλάττονα ⟨τοῦ⟩ λϛʹ τὴν διαφορὰν γιγνομένην.
Heath’s translation:
Since 720 has not its side rational, we can obtain its side within a very small difference as follows. Since the next succeeding square number is 729, which has 27 for its side, divide 720 by 27. This gives 26⅔. Add 27 to this, making 53⅔, and take half of this or 26½⅓. The side of 720 will therefore be very nearly 26½⅓. In fact, if we multiply 26½⅓ by itself, the product is 720 1/36, so that the difference (in the square) is 1/36.
If we desire to make the difference still smaller than 1/36, we shall take 720 1/36 instead of 729 [or rather we should take 26½⅓ instead of 27], and by proceeding in the same way we shall find that the resulting difference is much less than 1/36.
T. L. Heath, A History of Greek Mathematics (1921), vol. 2, p. 324
The square brackets are Heath’s.
Word by word
| Greek | Transliteration | Meaning |
|---|---|---|
| πλευρά | pleura | side; the side of a square number is its square root |
| ῥητή | rhētē | rational, “expressible” |
| μετὰ διαφόρου ἐλαχίστου | meta diaphorou elachistou | with the least difference |
| ὁ συνεγγίζων τετράγωνος | ho synengizōn tetragōnos | the square that comes close |
| μέρισον | merison | divide |
| πρόσθες | prosthes | add |
| τὸ ἥμισυ | to hēmisy | the half |
| ἔγγιστα | engista | very nearly |
| ἐφ᾽ ἑαυτά | eph’ heauta | by itself, that is, squared |
| μόριον λϛʹ | morion 36 | a 36th part |
| ἀντὶ τοῦ ψκθ τάξομεν | anti tou 729 taxomen | in place of 729 we shall set |
My translation
Since 720 does not have a rational side, we will take the side with the least difference, like this. The square that comes close to 720 is 729, and its side is 27. Divide 720 by 27: that makes 26 and 2 thirds. Add the 27: that makes 53 and 2 thirds. Half of that makes 26 1/2 1/3. So the side of 720 is very nearly 26 1/2 1/3, since 26 1/2 1/3 times itself makes 720 1/36, and the difference is a 36th part of a unit. If we want the difference to come out in a part smaller than the 36th, then in place of 729 we will set the 720 and 1/36 just found, and doing the same again we will find the difference comes out much less than the 36th.
What the text leaves open
- Which square to start from. Heron’s word, synengizōn, means “coming close”. Heath translates “the next succeeding square number”, and Schöne’s German has nächstkommende Quadratzahl. For 720 the 2 readings agree: 729 is the next square and the nearest one, 9 away against 44 for 676. Heath’s general statement uses the nearest square, above or below. The code takes the nearest.
- What the second step replaces. Heron says to set the square 720 1/36 in place of 729. Heath’s bracket gives the side, 26 1/2 1/3, in place of 27. These are the same instruction: the new square is the square of the new side. The code carries the side.
- How to write the result. Heron writes 26 2/3 as “26 and 2 thirds” and 26 5/6 as 26 1/2 1/3, a sum of unit fractions. He does not carry out the second step. Its result is 26 1609/1932, and the greedy unit-fraction rule writes the fractional part as 1/2 1/4 1/13 1/170 1/97040 1/10358340720.
- When to stop. Heron stops when the difference in the square is small enough for the job. He gives no fixed number of steps.
- Why it works. The passage gives the recipe and the check. Heron does not explain why averaging improves the guess.
How Heron’s method works on the square root of 720
Step 1. The nearest square is 729 = 27 × 27, so start with 27. Checking: 27 × 27 - 720 = 9, too big by 9.
Step 2. Divide 720 by 27 to get 26 2/3. If 27 is a little too big, 720 / 27 is a little too small, and the root lies between them.
Step 3. Average them: (27 + 26 2/3) / 2 = 26 5/6. Heron’s 26 1/2 1/3 is the same number.
Step 4. Check: 26 5/6 × 26 5/6 = 25921/36 = 720 1/36. The difference in the square has dropped from 9 to 1/36.
Step 5. Heron’s second step, done in exact fractions by the code. 720 ÷ 26 5/6 = 4320/161 = 26 134/161. Average with 26 5/6 to get 51841/1932, about 26.8328157350. Its square is 720 + 1/3,732,624.
| Step | Value | Decimal | Square less 720 | k, error below 10-k |
|---|---|---|---|---|
| 0 | 27 | 27.000000000000 | 9 | 0 |
| 1 | 161/6 | 26.833333333333 | 1/36 | 3 |
| 2 | 51841/1932 | 26.832815734990 | 1/3,732,624 | 8 |
| 3 | 5374978561/200313624 | 26.832815729997 | 1/40,125,547,960,013,376 | 18 |
| 4 | (20-digit fraction) | 26.832815729997 | about 2 × 10-37 | 38 |
The square root of 720 is 26.8328157299974763… After each step the error is below 10-k for k = 0, 3, 8, 18, 38, so each step a little more than doubles k. That k can be 1 more than the number of decimals that agree: 161/6 is 26.8333…, which agrees with the root to 2 decimals.
Why Heron’s method works
Averaging. If x is bigger than the root of n, then n / x is smaller, because their product is n. The root lies between the 2, and their average is a better guess than either one when they are close.
Quadratic convergence. Let the next guess be x' = (x + n / x) / 2. Then
Heron’s “difference in the square” gets squared each step and divided by 4x². For 720: 9 squared is 81, and 81 / (4 × 729) = 1/36. Then (1/36)² / (4 × (161/6)²) = 1/3,732,624. The same algebra in terms of the error e = x - sqrt(n) gives
An error of 10-3 becomes an error of about 10-6 / (2x). When x is at least 1/2, the exponent k of the error at least doubles. The tests check this on random numbers up to 108. The right side of both equations is never negative, so after the first step the guess is always at or above the root.
Newton’s method. Newton’s method finds a zero of a function f by replacing x with x - f(x) / f'(x). Take f(x) = x² - n, so f'(x) = 2x:
That is Heron’s rule. Newton’s method converges quadratically near a simple root for the same reason: the error after a step is proportional to the square of the error before it.
The Python
Heron’s method in exact fractions, with his check:
from fractions import Fraction
from math import isqrt
def nearest_square_side(n: int) -> int:
"""Side of the square number nearest to n."""
below = isqrt(n)
above = below + 1
return below if n - below * below < above * above - n else above
def heron_sqrt(n: int, steps: int) -> list[Fraction]:
"""Heron's approximations to the side of n."""
x = Fraction(nearest_square_side(n))
approximations = [x]
for _ in range(steps):
x = (x + n / x) / 2
approximations.append(x)
return approximations
for x in heron_sqrt(720, 2):
print(x, x * x - 720)
# 27 9
# 161/6 1/36
# 51841/1932 1/3732624
In the repo version, the averaging loop is heron_steps from the YBC 7289 module, reused. The module adds Heron’s unit-fraction notation, a measure of decimal accuracy (the largest k with the error below 10-k) that it shares with the Madhava module, and Newton’s step for comparison. The tests check:
- 720 as the squared area of the 7, 8, 9 triangle;
- each number in Heron’s passage: 27, 26 2/3, 53 2/3, 26 1/2 1/3, 720 1/36;
- the second step from 720 1/36, and that its difference is less than 1/36;
- the error exponents 0, 3, 8, 18, 38;
- with Hypothesis, on random numbers and guesses: Heron’s step equals Newton’s step, the difference in the square follows the formula above exactly, and the error exponent at least doubles.
Against modern square roots
- Floating point.
math.sqrt(720)returns 26.832815729997478. It calls the C library’ssqrt, which current processors compute with 1 instruction. IEEE 754 requires the square root to be correctly rounded, so the result is the nearest double to the true root. - Many digits. For 1,000 digits there is no instruction. Heron’s step is a standard way to get there: start from a float with about 16 correct digits, and 6 steps pass 1,000, since each step doubles them.
- Exact integers.
math.isqrt(n)returns the largest integer whose square is at mostn, for integers of any size. The code uses it to find Heron’s starting square.
Parallels and lineage
- YBC 7289, an Old Babylonian school tablet (c. 1900 to 1600 BCE, the date CDLI gives), writes the square root of 2 as 1;24,51,10 in base 60, without saying how it was found. Heron’s rule takes 3 steps from 1, or 2 from 3/2: 3/2, 17/12 and 577/408. 577/408 written in base 60 and cut off after 3 places is 1;24,51,10.
- BM 96957 + VAT 6598, an Old Babylonian problem text, estimates the diagonal of a gate in problem 18 with 1 step of the same averaging rule, as the YBC 7289 post shows from the translation in Fowler and Robson (1998, pp. 371 to 372).
- Archimedes, Measurement of a Circle, proposition 3 (3rd century BCE), uses 1351/780 as an upper bound for the square root of 3 without saying where it came from (Heath 1921, vol. 2, p. 51). 2 steps of Heron’s rule from 5/3 give exactly 1351/780. Heath reports that before the Metrica was found, writers had proposed a rule of this kind to explain Archimedes’s numbers.
- Later use. Heath (vol. 2, p. 324, note 2) traces the method to Barlaam and Nicolas Rhabdas in the 14th century, and an equivalent to Luca Pacioli (Heath spells him Paciuolo) around 1500.
What is debated
- A Babylonian origin. “The Babylonian method” is a modern name for the rule. What the sources support: 1 step of the rule appears in an Old Babylonian problem text, BM 96957 + VAT 6598; the value on YBC 7289 can be reached by 3 steps from 1, and agreement of digits does not show which method the scribe used; scholars have proposed at least 4 routes to that value. The Metrica is the earliest text I have read that states the rule as a procedure to repeat. The sources I read document no route from Old Babylonian practice to Heron, so I treat the 2 as parallels.
- Heron’s date. Heath (1921) reported the then-current conclusion, the 3rd century CE. The 1st-century date rests on Neugebauer’s 1938 identification of 1 eclipse, which Masià (2015) reviews.
- Archimedes. That Heron’s rule reproduces 1351/780 shows a route to the number. It does not show that Archimedes took that route.
Key takeaways
- Heron’s method replaces a guess
xfor the root ofnwith(x + n / x) / 2. Heron works it in the Metrica, I.8, on the square root of 720: 27, 26 5/6, then a second step from 720 1/36. - The difference in the square is squared at each step, so the error exponent roughly doubles: 10-3, 10-8, 10-18, 10-38 for 720.
- It is Newton’s method on
x² - n. - The Metrica was rediscovered in 1896. Heron’s date, and whether the rule is older, are open.
- The code and tests are in ancient-algorithms.
Sources
- Heron of Alexandria, Metrica, in Heronis Alexandrini opera quae supersunt omnia, vol. 3, ed. and trans. H. Schöne, Teubner, 1903, pp. 18 to 21.
- T. L. Heath, A History of Greek Mathematics, vol. 2, Oxford, 1921. Heron’s date pp. 298 to 306, the discovery of the Metrica p. 299, the manuscript p. 308, the Eutocius allusion p. 317, the square root of 720 p. 324, Archimedes’s bounds for the root of 3 p. 51.
- O. Neugebauer, Über eine Methode zur Distanzbestimmung Alexandria-Rom bei Heron, Det Kongelige Danske Videnskabernes Selskab, Historisk-filologiske Meddelelser 26(2), Copenhagen, 1938.
- R. Masià, “On dating Hero of Alexandria”, Archive for History of Exact Sciences 69(3), 2015, pp. 231 to 255. Summary in its zbMATH review, shown on the MaRDI portal.
- J. J. O’Connor and E. F. Robertson, “Heron of Alexandria”, MacTutor History of Mathematics.
- D. Fowler and E. Robson, “Square root approximations in Old Babylonian mathematics: YBC 7289 in context”, Historia Mathematica 25, 1998, pp. 366 to 378.
Footnotes
-
Greek numbers are written with letters: ψ is 700, κ is 20, θ is 9, ζ is 7, ϛ is 6, ν is 50, γ is 3, λ is 30, so ψκ is 720 and κζ is 27. A stroke marks a unit fraction, so γʹ is 1/3 and λϛʹ is 1/36. Schöne prints a separate sign for 1/2, shown here as ∠. ↩
Common questions
What is Heron's method?↓
A way to compute the square root of n. Start from a guess x, for example the side of the nearest square number. Replace x with the average of x and n / x. Repeat. Heron of Alexandria gives it in the Metrica, I.8, c. 1st century CE, for the square root of 720: 27, then 26 5/6, then 51841/1932.
How do you find the square root of a number with Heron's method?↓
Take the nearest square and its side as the first guess: for 720 that is 729 and 27. Divide the number by the guess, 720 / 27 = 26 2/3. Average the 2: (27 + 26 2/3) / 2 = 26 5/6. Square it to check: 720 1/36, off by 1/36. Repeat with 26 5/6 as the new guess for more accuracy.
Is Heron's method the same as Newton's method?↓
Yes, for square roots. Newton's method replaces x with x - f(x) / f'(x). With f(x) = x^2 - n that is x - (x^2 - n) / (2x), which simplifies to (x + n / x) / 2, Heron's rule.
Why does Heron's method converge so fast?↓
If x is off from the square root by e, the next value is off by e^2 / (2x). Squaring a small error roughly doubles its number of zeros after the decimal point. For 720 the error falls below 10^-3, 10^-8, 10^-18 and 10^-38 in the first 4 steps.
What is the fastest square root algorithm?↓
It depends on the size of the number. For 1 floating-point number, the processor's square root instruction, which IEEE 754 requires to return the correctly rounded result; in Python that is math.sqrt. For hundreds or thousands of digits, Newton-type iterations like Heron's are a standard choice, because each step doubles the correct digits. For exact integer roots, Python has math.isqrt.