Around 820 CE the Persian scholar Al-Khwārizmī (780–850 CE), at Baghdad's House of Wisdom, wrote a book explaining the Indian place-value numerals and the Indian methods for the four operations. Its 12th-century Latin translation, Liber Algorismi de numero Indorum, spread Indian arithmetic in Europe. From Algoritmi, the Latin form of his name, came algorismus, then algorithm: first meaning an Indian method of calculation, now any well-defined step-by-step procedure.
Building new algorithms from old ones:
- prime(n): n is prime exactly when divisors(n) has two entries, 1 and n.
- primedivisors(n): compute divisors(n), keep only those that are prime. For 18: [2, 3].
- Prime factorisation: 180 = 2² × 3² × 5¹ can be stored as pairs (2, 2), (3, 2), (5, 1).
Speed-up: divisors come in pairs, e.g. 18 = 1 × 18 = 2 × 9 = 3 × 6. Checking j up to √n finds every pair.
Every algorithm has: basic steps, repeated steps (for each j …), conditional steps (only if j divides n), a reason it is correct, and a count of how many steps it takes.
Tap the diagram to enlarge it
- Algorithm ← Al-Khwārizmī
- prime(n): exactly two divisors
- Pairs: test up to √n