The World of Algorithms

Chapter 117 topics, part by part5 min read

What an algorithm is, column addition as an algorithm and why it is fast, finding the gcd from lists of divisors, data structures, improving an algorithm step by step, Euclid’s subtraction method and Āryabhaṭa’s division method, and how Al-Khwārizmī gave us the word ‘algorithm’ – with every exercise solved.

🧮
Part

What an Algorithm Is

2 topics to learn here

What an Algorithm Is · Topic 1 of 2

Addition as an algorithm

Columns, carries and speed.

  1. 1

    Counting dots works for 5 + 7 but not for 473 + 695.

  2. 2

    An algorithm is a precise, step-by-step procedure to get an answer.

  3. 3

    Addition algorithm: align digits from the right; add each column with the carry; write the units digit and carry 1 when the sum is 10 or more; at the end, write any leftover carry.

  4. 4

    It relies on a basic step we can already do: adding single digits.

  5. 5

    Why it works: we add units, tens, hundreds separately and pass each new ten to the next group.

  6. 6

    It is fast: one more digit means one more column, not ten times as many dots. At 1 dot per second, 3347 + 2729 takes over 1 h 40 min to count but about a minute column by column.

❓ Can you answer?

Basic step assumed in column addition?

What an Algorithm Is · Topic 2 of 2

Listing divisors

Check 1 to n, keep divisors.

  1. 1

    Start with an empty list-of-divisors; for each j from 1 up to n, add j to the end whenever it divides n.

  2. 2

    Names like j and list-of-divisors make the steps easy to describe.

  3. 3

    Running the steps is executing the algorithm: divisors(18) = [1, 2, 3, 6, 9, 18] – in increasing order.

  4. 4

    Basic step assumed: telling whether one number divides another.

❓ Can you answer?

divisors(20)?

➗
Part

Algorithms for gcd

4 topics to learn here

Algorithms for gcd · Topic 1 of 4

gcd from lists; data structures

Common list, take the last.

  1. 1

    gcd(m, n): list the divisors of each; keep those in both lists (common-divisors); the rightmost is the gcd.

  2. 2

    divisors(375) and divisors(825) share [1, 3, 5, 15, 25, 75], so gcd = 75. gcd(54000, 81000) = 27000.

  3. 3

    A list is a data structure: a way of organising intermediate values so the algorithm works well.

❓ Can you answer?

Why is the gcd the rightmost common divisor?

Algorithms for gcd · Topic 2 of 4

Improving the algorithm

One scan, up to min, no lists.

  1. 1

    Scan once up to max(m, n) for both numbers instead of twice (m + n checks).

  2. 2

    Collect common divisors directly – and only up to min(m, n).

  3. 3

    Drop lists: just remember the most-recent-common-divisor; the last one found is the gcd.

  4. 4

    Still, the work grows with the value: a 4-digit number needs about 10 times the checks of a 3-digit one.

  5. 5

    Scanning backwards from min(m, n), the first common divisor found is the gcd – you can stop early.

💡 UnderstandProgrammers improve code exactly like this – same output, far fewer steps.

❓ Can you answer?

What single value replaces the list of common divisors?

Algorithms for gcd · Topic 3 of 4

Euclid’s subtraction algorithm

Subtract the smaller.

  1. 1

    If d divides m and n, it divides m − n; and if d divides n and m − n, it divides m. So both pairs share the same common divisors.

  2. 2

    Picture: if blocks of size d tile m and n, they also tile m − n.

  3. 3

    Steps: if m < n, swap; if n = 0, the answer is m; otherwise find gcd(n, m − n).

  4. 4

    gcd(375, 825) needs 7 reductions to reach gcd(75, 0) = 75.

  5. 5

    But gcd(99, 2) takes about 49 subtractions – work still grows with the value.

  6. 6

    Euclid wrote it in his Elements; earlier Greeks probably knew it.

m = 7 tiles of dn = 4 tiles of dm − n = 3 tiles of dwhatever tiles m and n also tiles m − n, so gcd(m, n) = gcd(n, m − n)
❓ Can you answer?

gcd(14, 6) by subtraction?

Algorithms for gcd · Topic 4 of 4

Āryabhaṭa’s division algorithm

Use the remainder instead.

  1. 1

    Repeatedly subtracting 2 from 99 just leaves the remainder of 99 ÷ 2.

  2. 2

    So reduce gcd(m, n) to gcd(n, m mod n); m mod n is the remainder (17 mod 5 = 2).

  3. 3

    gcd(99, 2) → gcd(2, 1) → gcd(1, 0) = 1: only two steps.

  4. 4

    The long-division layout appears in Āryabhaṭa’s Āryabhaṭīya (499 CE).

  5. 5

    The number of steps grows with the number of digits, not the value – a huge saving.

💡 UnderstandPhones use the same remainder idea (with huge numbers) every time they encrypt a message.

gcd(825, 375): 825 = 2 × 375 + 75gcd(375, 75): 375 = 5 × 75 + 0gcd(75, 0) → answer 75Āryabhaṭa’s division: 3 lines instead of Euclid’s 7 subtraction steps
499 CEĀryabhaṭīya’s division method
🧮 Worked example

Find gcd(60, 16) by Āryabhaṭa’s division method.

  1. 60 = 3 × 16 + 12 → gcd(16, 12)
  2. 16 = 1 × 12 + 4 → gcd(12, 4)
  3. 12 = 3 × 4 + 0 → gcd(4, 0)

Answer: gcd = 4

🧠 Memory trickDivide, keep the remainder, shift left; the last non-zero remainder is the gcd.

❓ Can you answer?

gcd(48, 18) by division?

📜
Part

The Word Itself

1 topic to learn here

The Word Itself · Topic 1 of 1

Where the word ‘algorithm’ comes from

Al-Khwārizmī and Indian numerals.

  1. 1

    Al-Khwārizmī (780–850 CE), at Baghdad’s House of Wisdom, learnt Sanskrit and wrote (c. 820 CE) on the Indian place-value system and its four operations.

  2. 2

    Its Latin translation, Liber Algorismi de numero Indorum, spread Indian arithmetic in Europe.

  3. 3

    ‘Algorismus’ – from Algoritmi, the Latin form of his name – first meant an Indian method of calculation, later any step-by-step procedure: algorithm.

❓ Can you answer?

What did ‘algorismus’ first mean?

🔤 Key words

Each science word with its meaning and its Hindi name. Cover the meaning and try to say it first.

  • Algorithmकलनविधि (एल्गोरिद्म)

    A precise step-by-step procedure that solves a problem.

  • Executeनिष्पादन

    Run through an algorithm’s steps.

  • Basic stepमूल चरण

    A step we assume we can do directly, e.g. adding two digits.

  • Carryहासिल

    The 1 passed to the next column when a column sum is 10 or more.

  • gcd / hcfमहत्तम समापवर्तक

    Greatest common divisor (highest common factor).

  • Listसूची

    An ordered collection of values, written [1, 2, 3].

  • Data structureडेटा संरचना

    A way of organising information for an algorithm.

  • m mod nशेषफल

    Remainder when m is divided by n.

  • Efficiencyदक्षता

    How the work grows as the input grows.

🛂 One-look revision

One card per idea. Read the key point, then try to recall the whole topic.

  • AlgorithmPrecise steps + basic stepsColumn addition
  • ExecuteRun the stepsdivisors(18)
  • gcd by listsRightmost common divisorgcd(375, 825) = 75
  • Data structureOrganised intermediate valuesIncreasing lists
  • ImprovementScan to min(m, n); keep only latestSame answer, less work
  • Euclidgcd(n, m − n)Slow for gcd(99, 2)
  • Āryabhaṭagcd(n, m mod n)gcd(99, 2) in 2 steps
  • Word originAl-KhwārizmīIndian numerals

❔ Questions and answers

Tap a question to see the answer. Try answering in your head first.

Exercise Set 11.1 and Think and Reflect

QAdd two 4-digit numbers strictly by the steps.
A
e.g. 4738 + 2596: 8 + 6 = 14 → 4, carry 1; 3 + 9 + 1 = 13 → 3, carry 1; 7 + 5 + 1 = 13 → 3, carry 1; 4 + 2 + 1 = 7 → 7; answer 7334. The steps work.
QA 5-digit number plus a 3-digit number?
A
Once the shorter number runs out, treat its missing digits as 0 – the written steps should say so explicitly.
QWhy align columns from the right?
A
So units add to units, tens to tens and so on – place values must match.
QWhy can’t the carry exceed 1?
A
The largest column sum is 9 + 9 + 1 = 19, so the carry is at most 1.
QWhat if Step 5 (write the final carry) is dropped?
A
It still works when the last column has no carry (12 + 34 = 46) but fails otherwise (95 + 17 gives 12 instead of 112).
QAdding decimal fractions?
A
Align the decimal points (pad with zeros), add from the right, and place the point in the answer under the others.
QDivisors of 15, 135 and 775 – how does the work grow?
A
[1, 3, 5, 15]; [1, 3, 5, 9, 15, 27, 45, 135]; [1, 5, 25, 31, 155, 775]. The checks grow in proportion to the number itself.

Exercise Sets 11.2 and 11.3

QElements of List 1 not in List 2 (and vice versa).
A
Start an empty result list; for each x in List 1, if x does not appear in List 2, add x to the result. Swap the lists for the reverse.
QAn algorithm for the lcm of m and n.
A
For k = 1, 2, 3, …, check m × k; the first multiple of m that n also divides is the lcm. (Or lcm = m × n ÷ gcd(m, n).)
QDivisors in pairs: how far must we check? Does the gcd method still work?
A
Only up to √n – each small divisor j gives its partner n ÷ j. The divisors then come out in pairs, not in increasing order, so the lists must be sorted before taking the rightmost common divisor.
QOriginal algorithm scanning from n down to 1?
A
The lists come out in decreasing order, so the gcd is the leftmost common divisor.
QLast algorithm scanning from min(m, n) down to 1?
A
The first common divisor found is the gcd, so we can stop at once.

End-of-Chapter Exercises

Qgcd(375, 825) and gcd(51000, 81000) by the improved Euclid method
A
825 → 375 → 75 → 0: 75. 81000, 51000 → 30000 → 21000 → 9000 → 3000 → 0: 3000.
Qgcd(1789287, 237656)
A
Remainders 125695, 111961, 13734, 2089, 1200, 889, 311, 267, 44, 3, 2, 1, 0: 1 (co-prime).
Qgcd(2587392, 157656)
A
Remainders 64896, 27864, 9168, 360, 168, 24, 0: 24.
QShow d divides m and n ⇔ d divides n and m mod n.
A
m = qn + r. If d | m and d | n then d | m − qn = r. If d | n and d | r then d | qn + r = m.
QAlgorithm prime(n).
A
Compute divisors(n); n is prime exactly when the list is [1, n] with n > 1 (two divisors).
QAlgorithm primedivisors(n).
A
Start an empty list; for each d in divisors(n), if prime(d), add d. E.g. primedivisors(180) = [2, 3, 5].
QPrime factorisation: represent 180 = 2² × 3² × 5¹; compare two factorisations.
A
Store pairs (prime, power): [(2, 2), (3, 2), (5, 1)]. For the gcd, take each prime common to both with the smaller power.

Back to the full listAll Class 9 Mathematics