# Ancient algorithms, implemented | Prasad Subrahmanya

> Ancient algorithms from India, Babylon, Greece, China, the Maya and al-Khwarizmi, each with its source text, a derivation, tested Python and the modern method.

Source: https://prasad.tech/ancient-algorithms/

---

Algorithms from ancient mathematics, read from dated sources and implemented in Python, starting with ancient Indian mathematics.

Every post follows the same steps:

-   the source text, translated, with a word-by-word gloss where the source has words;
-   a worked example from the source or its oldest commentary;
-   a derivation of why the method works;
-   Python with tests that check the source's own examples;
-   a comparison with the modern method, and what historians still argue about.

The code lives in 1 public repo, with a module and tests per algorithm: [ancient-algorithms on GitHub](https://github.com/prasadus92/ancient-algorithms).

## Ancient Indian mathematics

6 algorithms

Aryabhata, Aryabhatiya, Ganitapada 32 and 33, c. 499 CE

### [The kuttaka and the extended Euclidean algorithm](https://prasad.tech/blog/kuttaka-method-extended-euclidean-algorithm/)

Solving ax + c = by in whole numbers, from the Sanskrit verses to Python, with the modular inverse RSA needs.

Published

16th-century Kerala texts that credit Madhava (c. 1340 to 1425)

### [Madhava's series for pi, with his correction terms](https://prasad.tech/blog/madhava-leibniz-series-pi-correction-terms/)

The series 1 - 1/3 + 1/5 - ..., the 3 end corrections that speed it up, and a convergence benchmark against later formulas.

Published

Pingala, Chandahshastra (date debated), with Halayudha's 10th-century commentary

### [Pingala's enumeration of meters](https://prasad.tech/blog/pingala-binary-meru-prastara/)

Listing every pattern of long and short syllables, the binary encoding behind it, and the triangle of counts that Halayudha's commentary describes.

Published

Panini, Ashtadhyayi (date debated)

### [Panini's grammar as a rewrite system](https://prasad.tech/blog/panini-ashtadhyayi-rewrite-system/)

A small Python model of rule ordering and conflict resolution, compared with Backus-Naur form.

Published

Bhaskara II, Bijaganita, 1150

### [The chakravala method for Pell's equation](https://prasad.tech/blog/chakravala-method-pells-equation/)

The cyclic method for x^2 - Ny^2 = 1, implemented, with its step count against continued fractions.

Published

Sankaravarman, Sadratnamala (1819), and earlier Kerala texts

### [Katapayadi, a letter-to-digit code](https://prasad.tech/blog/katapayadi-system-decoder/)

How syllables encode numbers, with an encoder and a decoder.

Published

## Babylon

2 algorithms

Old Babylonian tablet, c. 1900 to 1600 BCE

### [The square root of 2 on YBC 7289](https://prasad.tech/blog/babylonian-square-root-ybc-7289/)

Base-60 arithmetic, the value 1;24,51,10 on the tablet, and what the evidence shows about how it was computed.

Published

Old Babylonian tablet, Larsa, c. 1822 to 1762 BCE

### [Plimpton 322](https://prasad.tech/blog/plimpton-322-pythagorean-triples/)

Rebuilding the table of numbers from its columns, and the competing readings of what it was for.

Published

## Greece

3 algorithms

Euclid, Elements, Book VII, propositions 1 and 2, c. 300 BCE

### [Euclid's algorithm](https://prasad.tech/blog/euclidean-algorithm-euclid-elements/)

Repeated subtraction as Euclid states it, and its step count against the modern version.

Published

Described by Nicomachus, Introduction to Arithmetic, c. 100 CE

### [The sieve of Eratosthenes](https://prasad.tech/blog/sieve-of-eratosthenes-python/)

The sieve as the text gives it, then the segmented and wheel versions used today.

Published

Heron of Alexandria, Metrica, c. 1st century CE (date debated)

### [Heron's method for square roots](https://prasad.tech/blog/herons-method-square-root/)

The averaging rule worked on the square root of 720, and why it doubles the correct digits each step.

Published

## China

2 algorithms

Sunzi Suanjing, 3rd to 5th century CE

### [Sunzi's remainder problem](https://prasad.tech/blog/chinese-remainder-theorem-sunzi-suanjing/)

A number leaving 2 with 3, 3 with 5 and 2 with 7, and the recipe that gives 23.

Published

Qin Jiushao, Mathematical Treatise in Nine Sections, 1247

### [Qin Jiushao's general method for remainders](https://prasad.tech/blog/qin-jiushao-dayan-method/)

The general method, compared step by step with the kuttaka.

Published

## The Maya

1 algorithm

Dresden Codex and monument inscriptions

### [Maya numerals and the Long Count](https://prasad.tech/blog/maya-numerals-long-count-converter/)

Base-20 place value with zero, converting Long Count dates to the Gregorian calendar, and the correlation constant debate.

Published

## Al-Khwarizmi

2 algorithms

Al-Khwarizmi, The Compendious Book on Calculation by Completion and Balancing, c. 820 CE

### [Completing the square](https://prasad.tech/blog/al-khwarizmi-completing-the-square/)

His geometric method for quadratics, translated into code, and how the word algebra came from the title.

Published

Latin translations of his arithmetic, 12th century

### [Where the word algorithm comes from](https://prasad.tech/blog/algorithm-word-origin-al-khwarizmi/)

Place-value arithmetic with Indian numerals, as the Latin text teaches it.

Published
