Where the word algorithm comes from: al-Khwarizmi's arithmetic, in Python
The word algorithm comes from al-Khwarizmi, whose 9th-century arithmetic reached Latin as Dixit Algorizmi. His rules for Indian numerals, tested in Python.
The word algorithm comes from the name of al-Khwarizmi, a mathematician who worked in Baghdad in the 9th century. His book on calculating with the 9 Indian digits and a zero reached Europe in a Latin version that opens "Dixit Algorizmi", "al-Khwarizmi said". Latin readers made his name into algorismus, the art of calculating with these numerals, and English reshaped algorism into algorithm.
The book itself is a set of procedures for adding, subtracting, halving, doubling, multiplying and dividing numbers written in place value. My source is the Latin text of the Cambridge manuscript, in Baldassarre Boncompagni's 1857 edition. I translated the rules and ran each one on lists of digits in Python, tested against Python's own integers. The code is on GitHub, and the series is Ancient algorithms, implemented.
The problem it solves
Roman numerals give each value its own sign, and so did the Greek system of writing numbers with letters of the alphabet, as Folkerts (2001) notes. The Indian system uses 9 digits whose value depends on their place, and a small circle for an empty place. The book teaches how to write any number with these 10 signs and how to compute with them.
Folkerts (2001) explains the working surface. The computer wrote on a board covered with dust, sand, wax or white powder, and erased and rewrote digits as the work went on. For addition, subtraction and multiplication, the upper number is overwritten until it holds the answer. That explains several choices in the rules below, such as starting from the left.
Who was al-Khwarizmi?
- Muhammad ibn Musa al-Khwarizmi (al-Khwārizmī; in Latin Algorizmi, Algoritmi, Alchoarismi) lived c. 780 to c. 850 and worked in Baghdad under the caliph al-Ma'mun, who reigned 813 to 833, among the scholars of the "House of Wisdom". Folkerts (2001) gives these dates and says he wrote on arithmetic, algebra, astronomy, geography and the calendar.
- The arithmetic was written c. 825, after his Algebra, which it cites. Folkerts calls it "the oldest known Arabic work" describing the Indian decimal place-value system and operations with it. Its Arabic title is not known for certain. From Arabic commentaries listed in the Fihrist, Folkerts suggests Kitāb fī 'l-jam' wa 'l-tafrīq, "Book on addition and subtraction".
- The Arabic original is lost. No Arabic manuscript is known, and the 12th-century Latin translation is lost too. What survives is a Latin reworking, called DA after its opening words Dixit Algorizmi, in 2 manuscripts:
- Cambridge, University Library Ii.6.5 (apparently 13th century, once at Bury St Edmunds). It is missing its last folios. Boncompagni printed it in 1857, and John Crossley and Alan Henry translated it into English in 1990.
- New York, Hispanic Society of America HC 397/726 (written in Spain, probably 13th century). It is complete, and Menso Folkerts edited it in 1997.
- The translator is unknown. Adelard of Bath, Robert of Chester and Gerard of Cremona have all been suggested. Folkerts finds the case for Gerard "distinctly better" than the others, and no case conclusive.
The Latin text, with my translation
The Latin below is Boncompagni's 1857 transcription of the Cambridge manuscript. He prints u for v, as the manuscript does, and spells the name algoritmi. I give my own translations. Crossley and Henry's English translation is the published one to compare.
The opening
Dixit algoritmi: laudes deo rectori nostro atque defensori dicamus dignas.
Algoritmi said: let us give due praise to God, our guide and defender.
The zero
The Indians, the text says, needed a sign to show that the place of units was empty when a 1 stood in the tens:
posuerunt in ea circulum paruulum in similitudine .o. litere, ut per hoc scirent quod differentia unitatum esset uacua
they put in it a small circle like the letter o, so that from this they would know that the place of units was empty
The word for a place is differentia. The places are counted from the right, so the first place holds the units.
Adding and subtracting
After the rules for subtraction, the text gives the order of work:
et incipe semper in augmentacione uel diminutione ab altiori differentia
| Latin | Meaning |
|---|---|
| incipe | begin |
| semper | always |
| in augmentacione | in increasing, in adding |
| uel diminutione | or decreasing, subtracting |
| ab altiori differentia | from the higher place |
and always begin, in adding or subtracting, from the highest place
When a place collects 10 or more, "make 1 of them and raise it to the higher place". When a place is too small to subtract from, "take 1 from the next higher place and make 10 of it". If nothing is left in a place, "put a circle, so that the place is not empty".
Halving and doubling
Cum uolueris mediare aliquem numerum, accipe a prima differentia, et media eam
When you want to halve a number, start from the first place and halve it.
If the units are odd, halve the even part, and the leftover 1 becomes "30 parts of 60", written under the units. In the higher places an odd 1 goes down to the place below as 5. Doubling runs the other way:
Et cum uolueris duplare, incipe a superiori differentia, et dupla
And when you want to double, begin from the highest place, and double.
Multiplying and dividing
For multiplication you must first "keep in memory the multiplication of the numbers from 1 to 9 with each other". The lower number is written with its units under the highest digit of the upper number. For division, the text calls the quotient quod debetur uni, "what is owed to 1", and a remainder "parts of 1".
What the text leaves open
- Which way addition runs. Folkerts notes that the addition section does not say whether to start at the right or the left. The sentence above, which closes the subtraction section in the Cambridge text, covers both addition and subtraction, and I follow it: highest place first.
- How to choose a quotient digit. The text says to find a digit that, times the top digit of the divisor, gives the number above it "or close to it, below it". It does not say what to do when that digit is too large for the rest of the divisor. My code lowers the digit until the subtraction fits, and a test covers a case where it has to: 1980 ÷ 199.
- Halving. The half of an odd unit is written as 30 sixtieths, while an odd 1 in a higher place becomes 5 in the place below. Folkerts points out that the text never justifies the 2 different treatments. Both are correct: 30/60 is a half, and a 1 in the tens is 10 units, whose half is 5.
- Examples. The Cambridge text gives no worked example for addition, halving or doubling, and states its second subtraction example, 1144 - 144, without working it.
Working the examples
The Cambridge text works 3 examples through. I ran each one in the code and got the same numbers at every step it states.
Subtraction: 6422 - 3211
Starting from the highest place: 6 - 3 = 3, then 4 - 2 = 2, then 2 - 1 = 1, then 2 - 1 = 1. The answer is 3211.
Multiplication: 2326 × 214
Write 2326, and under it 214 with the 4 under the first 2. Take that 2. Multiply it by 2, then 1, then 4, the digits of 214 from the left, adding each product into the place above that digit. The last product, 2 × 4 = 8, replaces the 2 itself. Then move 214 one place right and do the same with the 3, then the 2, then the 6.
The board after each digit of 2326:
| After the digit | Board reads |
|---|---|
| 2 (thousands) | 428326 |
| 3 (hundreds) | 492226 |
| 2 (tens) | 496486 |
| 6 (units) | 497764 |
The answer is 497764, the text's "quadringenta milia et nonaginta septem milia et septingenta lxiiii". The board never holds partial products in separate rows; the upper number turns into the product.
The text then checks the result by casting out nines. 2326 leaves 4 when divided by 9 and 214 leaves 7. 4 × 7 = 28 leaves 1. 497764 leaves 1 as well, so the check passes.
Division: 46468 ÷ 324
Write 46468, and 324 under it with the 3 under the 4. Then:
- 3 goes into 4 once. Write 1 above the dividend, in line with the divisor's units digit, and subtract 1 × 324 at that position: 46468 becomes 14068.
- Move 324 one place right. 3 goes into 14 four times. Subtract 4 × 324 at that position: 14068 becomes 1108.
- Move again. 3 goes into 11 three times. Subtract 3 × 324: 1108 becomes 136.
The text gives the answer as 143, "and 136 parts of 324 parts of one". Every intermediate number above is in the Latin. Its second example, 1800 ÷ 9, starts the 9 under the 8, because 9 is larger than 1, and gives 200.
Why it works
Place value means a number is a sum of digits times powers of 10. Every rule in the book is a way to keep that sum correct while changing the digits:
- Carrying replaces 10 in one place with 1 in the next, which leaves the sum unchanged. Starting from the highest place means a carry can land on a place already finished, and that place may then carry again. On a dust board that costs an erasure, and the text spells out the case of a 9 that becomes 10 when 1 is carried into it.
- Borrowing is the reverse. If the place above is 0, the text takes 1 from the next place up and leaves 9 behind: "and 9 will remain in the second".
- Multiplication is the distributive law. 2326 × 214 is 2000 × 214 + 300 × 214 + 20 × 214 + 6 × 214, and each digit's product lands at its place. Writing the last product over the digit is safe because that digit has been used for every product it is needed for.
- Casting out nines works because 10 leaves 1 when divided by 9, so every power of 10 does too, and a number leaves the same remainder with 9 as the sum of its digits. Remainders multiply: if
aleavesrandbleavess, thenableaves the remainder ofrs. A wrong product that differs from the right one by a multiple of 9 still passes. The test suite includes such a case, 497773.
The Python
This version works on lists of digits, units first, the way the text counts places from the right. It runs on its own.
def places(n: int) -> list[int]:
"""Digits of n, units first, as DA counts places from the right."""
return [int(d) for d in reversed(str(n))]
def number(digits: list[int]) -> int:
return int("".join(map(str, reversed(digits))))
def carry_up(digits: list[int], place: int) -> None:
"""Every 10 in a place becomes 1 in the place above, as often as needed."""
while digits[place] >= 10:
if place + 1 == len(digits):
digits.append(0)
digits[place + 1] += digits[place] // 10
digits[place] %= 10
place += 1
def add(upper: list[int], lower: list[int]) -> list[int]:
"""DA chapter 2: add place by place, starting from the highest place."""
total = upper + [0] * (len(lower) - len(upper))
for place in reversed(range(len(lower))):
total[place] += lower[place]
carry_up(total, place)
return total
def halve(digits: list[int]) -> tuple[list[int], int]:
"""DA chapter 3: start from the units; half of an odd unit is 30 sixtieths."""
half, sixtieths = [d // 2 for d in digits], 0
for place, d in enumerate(digits):
if d % 2 and place == 0:
sixtieths = 30
elif d % 2:
half[place - 1] += 5 # an odd 1 here is 10 of the place below
return half, sixtieths
def multiply(upper: list[int], lower: list[int]) -> list[int]:
"""DA chapter 4: each digit of the upper number, from the highest, times the lower number."""
work = upper + [0] * len(lower)
for place in reversed(range(len(upper))):
digit = work[place]
for shift in reversed(range(len(lower))):
if shift == 0:
work[place] = digit * lower[0] # the last product replaces the digit
else:
work[place + shift] += digit * lower[shift]
carry_up(work, place + shift)
return work
print(number(add(places(999), places(1)))) # 1000
print(halve(places(35))) # ([7, 1], 30): 17 and 30 sixtieths
print(number(multiply(places(2326), places(214)))) # 497764
The repo version adds subtraction with borrowing, doubling, the nines check, and division with a step-by-step record. The chapter numbers follow Folkerts. The tests check:
- the 3 worked examples: 6422 - 3211 = 3211, 2326 × 214 = 497764 with the nines check, and 46468 ÷ 324 = 143 remainder 136 with the 3 intermediate remainders 14068, 1108 and 136;
- 1800 ÷ 9 = 200, with the 9 starting under the 8;
- halving with 30 sixtieths, an odd digit sending 5 down, and doubling and addition that carry through a run of 9s;
- property tests with Hypothesis: addition, subtraction, halving, doubling, multiplication and division against Python's
+,-,*anddivmodon numbers up to 40 digits.
Against the modern method
School arithmetic today runs right to left. Each column is added once, the carry passes to the next column, and no written digit is changed. That suits pen and paper, where erasing is messy. DA runs left to right on a board where erasing is easy, and its multiplication overwrites the upper number in place instead of writing partial products in rows.
The 2 orders give the same digits. The repo has a modern right-to-left addition, schoolbook_add, and a property test checks that it agrees with DA's left-to-right addition on random numbers.
Python's own integers are a further step on. On 64-bit builds CPython stores an integer as a list of digits in base 230, adds them right to left with a carry, and switches to Karatsuba multiplication for large numbers, which needs fewer digit products than the schoolbook method. The idea underneath is the one the book teaches: a number is a list of digits in places, and arithmetic is done digit by digit with carries.
Where does the word algorithm come from?
The steps, with the sources for each:
- The name. Al-Khwarizmi's name appears in the Latin as Algorizmi or Algoritmi ("Dixit algoritmi" in Boncompagni's reading of the Cambridge manuscript), and the New York manuscript's heading reads Incipit arismethica Alchoarismi (Folkerts 2001).
- Algorismus. Folkerts says the later Latin textbooks on calculating with Indian numerals were called algorismus treatises, "a name that goes back to 'al-Khwārizmī,' though the historical connection was not understood". 2 of the best known are Alexander de Villa Dei's Carmen de algorismo and Johannes de Sacrobosco's Algorismus vulgaris, both from the first half of the 13th century.
- Algorism in English. Merriam-Webster traces algorism to Middle English augrim, algorisme, from Old French and Medieval Latin algorismus, from Arabic al-khuwārizmi, after al-Khwarizmi's name. It dates the English word to the 14th century and defines it as "the system of Arabic numerals" or "the art of calculating by means of nine figures and zero".
- Algorithm. Merriam-Webster gives algorithm as an "alteration of Middle English algorisme", with the same chain back to al-Khwarizmi.
Merriam-Webster does not say why -ism became -ithm, and I could not check the Oxford English Dictionary.
Parallels and lineage
- The Indian digits. Folkerts (2001) dates the oldest Indian example of decimal place value to 595 CE. The oldest known zero in an Indian inscription is a small circle at Gwalior, in the numbers 270 and 50 of a temple grant dated Vikrama year 933, which is 876 CE (Hultzsch 1892). Folkerts gives this date as 870. Al-Khwarizmi's book, c. 825, describes the system about 130 years before the oldest arithmetic in Arabic that survives, al-Uqlidisi's of the mid-10th century.
- The Latin redactions. 3 12th-century reworkings, the Liber Ysagogarum Alchorismi, the Liber Alchorismi and the Liber pulveris, treat the same topics in the same order and were more influential than DA, according to Folkerts.
- The algebra. The same author's Algebra gave Europe the word algebra and the method in the completing the square post.
- Other number systems. The Maya numerals are a separate place-value system with a zero, in base 20, developed with no known contact with India.
What is debated
- The Arabic title. It cannot be read from the Latin. Folkerts's suggestion, "Book on addition and subtraction", comes from the titles of Arabic commentaries.
- How much of DA is al-Khwarizmi's. Folkerts finds Arabic turns of phrase and technical terms that point to the Arabic original, alongside passages that look like a Latin reworker's additions, such as the discussion of the number 1. He judges the New York manuscript closer to the original translation than the Cambridge one.
- The translator. No manuscript names one, and Folkerts calls the suggestions so far deduction or speculation.
- The -ithm spelling. Merriam-Webster calls algorithm an alteration of algorisme without saying what changed it.
Key takeaways
- Algorithm comes from al-Khwarizmi's name, through Medieval Latin algorismus and Middle English algorisme. Merriam-Webster dates algorism to the 14th century.
- His arithmetic (c. 825) is lost in Arabic. It survives as a Latin reworking, Dixit Algorizmi, in 2 manuscripts, Cambridge Ii.6.5 and New York HC 397/726.
- The rules work on a dust board: add, subtract and double from the highest place; halve from the units, with half a unit written as 30 sixtieths; multiply by overwriting the upper number; divide by repeated subtraction of the shifted divisor.
- The Cambridge text's examples, 6422 - 3211, 2326 × 214 = 497764 and 46468 ÷ 324 = 143 remainder 136, come out the same in the code, step by step.
- The code checks every operation against Python's integers on random numbers up to 40 digits.
Sources
- B. Boncompagni (ed.), Trattati d'aritmetica I: Algoritmi de numero indorum. Rome, 1857. The Latin text of Cambridge, University Library Ii.6.5. Public domain.
- J. N. Crossley and A. S. Henry, "Thus spake al-Khwārizmī: a translation of the text of Cambridge University Library Ms. Ii.vi.5". Historia Mathematica 17(2), 103 to 131, 1990.
- M. Folkerts, Die älteste lateinische Schrift über das indische Rechnen nach al-Ḫwārizmī. Bayerische Akademie der Wissenschaften, Munich, 1997. Edition of the New York manuscript, with German translation.
- M. Folkerts, "Early texts on Hindu-Arabic calculation". Science in Context 14, 13 to 38, 2001.
- E. Hultzsch, "The two inscriptions of the Vaillabhattasvamin temple at Gwalior", Epigraphia Indica 1, 1892, from p. 154. The second inscription is dated Vikrama year 933, which he converts to 876 CE.
- J. J. O'Connor and E. F. Robertson, "A history of zero", MacTutor History of Mathematics. On the numbers 270 and 50 in that inscription.
- Merriam-Webster, "algorithm" and "algorism", etymologies. Read 15 November 2026.
Common questions
Where does the word algorithm come from?↓
From the name of Muhammad ibn Musa al-Khwarizmi (c. 780 to c. 850). A 12th-century Latin version of his book on Indian numerals opens 'Dixit Algorizmi', 'al-Khwarizmi said'. Medieval Latin turned the name into algorismus, the art of calculating with Indian numerals, which became Middle English algorisme and modern algorism. Merriam-Webster gives algorithm as an alteration of the Middle English algorisme.
What type of word is algorithm?↓
A noun, plural algorithms. Merriam-Webster defines it as a step-by-step procedure for solving a problem or accomplishing some end, including a procedure that solves a mathematical problem in a finite number of steps.
What is algorism?↓
Algorism is the older word: the system of Arabic numerals, or the art of calculating with 9 figures and zero. Merriam-Webster dates it to the 14th century and traces it through Old French and Medieval Latin algorismus to al-Khwarizmi's name.
Who was al-Khwarizmi?↓
Muhammad ibn Musa al-Khwarizmi worked in Baghdad under the caliph al-Ma'mun (reigned 813 to 833), among the scholars of the House of Wisdom. He wrote the Algebra (c. 820), a book on arithmetic with Indian numerals (c. 825), and works on astronomy, geography and the calendar.
Does al-Khwarizmi's arithmetic survive in Arabic?↓
No. No Arabic manuscript is known. The book survives in a Latin reworking of a lost 12th-century translation, in 2 manuscripts: Cambridge University Library Ii.6.5, which is missing its last folios, and New York, Hispanic Society of America HC 397/726, which is complete.