Revision notes

Chapter 11 The World of Algorithms: Foundation

The key points, rules to remember, tables and diagrams from all 5 lessons, on one page for quick revision. To keep a copy, use your browser's Print and choose "Save as PDF".

Lesson 1

What an algorithm is: column addition step by step

An algorithm is a precise, step-by-step procedure for solving a whole class of problems. It is built from basic steps we already know how to do, such as adding two single digits.

One column of the addition algorithm

  1. Add the two digits and the carry
  2. Sum less than 10? Write the sum; carry becomes 0
  3. Sum 10 or more? Write the units digit; carry becomes 1
  4. Move one column left Repeat until no columns remain, then write any final carry
Counting dots (1 per second) against adding columns (15 seconds per column)
Numbers being addedDots to count (about)Time countingColumnsTime by columns
Two 2-digit numbers, e.g. 33 + 27601 minute230 seconds
Two 3-digit numbers, e.g. 334 + 27260010 minutes345 seconds
Two 4-digit numbers, e.g. 3347 + 27296000over 1.5 hours41 minute
  • An algorithm is a precise sequence of basic steps that solves a whole class of problems.
  • Column addition works because it adds units to units, tens to tens and so on, carrying each new ten to the next column.
  • The carry is never more than 1, since 9 + 9 + 1 = 19.
  • Counting effort grows with the size of the numbers; column addition grows only with the number of digits.
Read the full lesson

Lesson 2

Listing the divisors of a number: names, lists and tracing

Tracing divisors(20)
jDoes j divide 20?list-of-divisors after this step
(start)-[ ]
1Yes[1]
2Yes[1, 2]
3No[1, 2]
4Yes[1, 2, 4]
5Yes[1, 2, 4, 5]
6 to 9No[1, 2, 4, 5]
10Yes[1, 2, 4, 5, 10]
11 to 19No[1, 2, 4, 5, 10]
20Yes[1, 2, 4, 5, 10, 20]

What divisors(n) is made of

  • Basic step
    • Does j divide n?
  • Repetition
    • For each j in 1, 2, ..., n
  • Condition
    • Add j only if j divides n
  • Names and data
    • j: the number being tried
    • list-of-divisors: a list that grows

divisors(n): start with an empty list; for each j from 1 to n, if j divides n, add j to the list. The list comes out in increasing order.

  • divisors(n) tries every j from 1 to n and keeps the ones that divide n in a list.
  • Names such as j and list-of-divisors make an algorithm easy to state and to trace.
  • Algorithms combine basic steps, repetition ('for each') and conditions ('if').
  • One algorithm can use another: prime(n) checks whether divisors(n) has exactly 2 entries.
Read the full lesson

Lesson 3

The greatest common divisor: a first algorithm and improvements

Three improvements

  1. Two separate scans 1 to m, then 1 to n: m + n checks
  2. One combined scan Each j from 1 to max(m, n) is tested against both numbers: max(m, n) checks
  3. Scan only to min(m, n) A common divisor cannot exceed the smaller number; keep j if it divides both
  4. Drop the list Remember only the latest common divisor found

gcd without lists: start with 1; for each k from 2 to min(m, n), if k divides both m and n, remember k. The last number remembered is the gcd.

most-recent-common-divisor as k runs from 2 to min(8, 20) = 8
kDivides 8?Divides 20?most-recent-common-divisor
(start)--1
2YesYes2
3NoNo2
4YesYes4
5NoYes4
6NoNo4
7NoNo4
8YesNo4
  • First gcd algorithm: list divisors of both numbers, list the common ones, report the last (largest).
  • Lists in increasing order are a simple data structure that makes the last entry the largest.
  • Improvements: one combined scan, scan only up to min(m, n), then keep just the latest common divisor.
  • All versions are correct, but their work still grows with the size of the numbers.
Read the full lesson

Lesson 4

Euclid's subtraction algorithm for the gcd

If m ≥ n, then gcd(m, n) = gcd(n, m − n). Also, gcd(m, 0) = m.

Euclid's subtraction algorithm repeats until n = 0

  1. Is m < n? If so, swap the two numbers
  2. Is n = 0? If so, stop: the answer is m
  3. Reduce Replace (m, n) by (n, m − n)

Then back to step 1: Is m < n?

Each row is one pass through the steps
Current problemWhat happensNext problem
gcd(84, 36)Reduce: 84 − 36 = 48gcd(36, 48)
gcd(36, 48)36 < 48, swapgcd(48, 36)
gcd(48, 36)Reduce: 48 − 36 = 12gcd(36, 12)
gcd(36, 12)Reduce: 36 − 12 = 24gcd(12, 24)
gcd(12, 24)12 < 24, swapgcd(24, 12)
gcd(24, 12)Reduce: 24 − 12 = 12gcd(12, 12)
gcd(12, 12)Reduce: 12 − 12 = 0gcd(12, 0)
gcd(12, 0)n = 0, stopAnswer 12
  • If d divides m and n, it divides m − n; if d divides n and m − n, it divides m. So gcd(m, n) = gcd(n, m − n).
  • Euclid's algorithm: swap if m < n, stop with answer m when n = 0, otherwise reduce to gcd(n, m − n).
  • The method reduces a problem to a smaller problem of the same kind.
  • Repeated subtraction can be slow: gcd(2k + 1, 2) needs about k reductions.
Read the full lesson

Lesson 5

Āryabhaṭa's division method and what makes a good algorithm

If m ≥ n > 0, then gcd(m, n) = gcd(n, m mod n). Keep dividing the last divisor by the last remainder; when the remainder is 0, the last divisor is the gcd.

Each divisor becomes the next dividend; each remainder becomes the next divisor
DividendDivisorQuotientRemainderProblem becomes
252198154gcd(198, 54)
19854336gcd(54, 36)
5436118gcd(36, 18)
361820gcd(18, 0)

An algorithm

  • Basic steps
    • Things we can already do, e.g. add two digits, test whether j divides n
  • Repetition
    • Do a step for each j from 1 to n
    • Repeat until a stopping rule is met
  • Conditions
    • Do a step only if something is true
  • Correctness
    • An argument that it always gives the right answer
  • Efficiency
    • How the number of steps grows with the size of the input
  • m mod n is the remainder when m is divided by n; it replaces many subtractions at once.
  • gcd(m, n) = gcd(n, m mod n); stop when the remainder is 0, and the last divisor is the gcd.
  • This division method, found in Āryabhaṭa's work, needs a number of steps that grows with the digits, not the value.
  • The word 'algorithm' comes from al-Khwārizmī, who wrote about Indian methods of calculation.
Read the full lesson

Privacy · Terms · Refunds · Delivery