GRADE 9 · Mathematics · CHAPTER 11 · CBSE

The World of Algorithms

Write and trace precise step-by-step procedures, find an HCF with Euclid's algorithm, and make an algorithm faster by counting and cutting its steps.

Original learning companion

Original learning companion for selected outcomes of this chapter: what makes a procedure an algorithm (precise steps, decisions and repeats), Euclid's division algorithm for the HCF, and improving and analysing an algorithm by counting its steps and comparing the subtraction and division forms of Euclid's algorithm. It does not reproduce the textbook, its activities or its exercise solutions.

Watch: The World of Algorithms in three lessons

Silent animation. The same steps are explained in the lessons below.

Concept animations

Algorithms · Lesson 1
Euclid's algorithm for the HCF · Lesson 2, 3
Comparing algorithms by steps · Lesson 3

Silent animations of one idea each. An animation is shared by every lesson that teaches the idea.

Solution animations

Every problem of this chapter, solved step by step. Try the problem first, then watch.

Lesson 1 · What makes an algorithm (9)
Lesson 2 · Euclid's algorithm for the HCF (9)
Lesson 3 · Improving and analysing an algorithm (9)

Read all lessons

These explanations remain available without JavaScript. The separate learner and solution pages can be printed on A4.

LESSON 1

What makes an algorithm

Describe an algorithm as precise steps from input to output, and trace one with decisions and repeats.

Algorithms
Algorithms

You already follow fixed methods by hand, such as column addition or long division, which give the same answer whoever uses them.

  1. A recipe for a task

    An algorithm takes an input, follows a list of steps and gives an output. Long division is one: input two numbers, output a quotient and a remainder.

    Input: two numbersLong divisionstepsOutput: quotientand remainder
  2. Each step must be exact

    'Add a little' is vague, but 'add 5' is exact. With exact steps, anyone who follows the algorithm on the same input gets the same output, and it must stop after finitely many steps.

    VagueAdd a littleExactAdd 5Same input gives same outputStops after finitely manysteps
  3. Decisions and repeats

    Steps can choose (IF the number is even THEN halve it) or repeat (REPEAT for each value in the list). To find the largest value: keep a 'best so far', compare each new value with it, and keep the larger.

    Take the nextvalueCompare with bestso farKeep the larger
  4. Trace and check

    Trace the largest-value algorithm on 14, 9, 23, 8. Best so far: 14, still 14, then 23, still 23. The list ends, so the output is 23, which is correct.

    Value readBest so far14149142323823

Worked example: Algorithm for the sum of the digits of 4,728. Step 1: total = 0. Step 2: add the last digit to the total and remove it from the number. Step 3: repeat Step 2 until the number is 0. Trace: 8; 8 + 2 = 10; 10 + 7 = 17; 17 + 4 = 21. Output: 21.

Watch for this: An algorithm is not only something a computer runs. The column method for adding numbers, done with pencil and paper, is an algorithm too.

Discuss: Write an algorithm a friend could follow to find the smallest mark in a list. Swap algorithms, trace each other's on the same list, and check whether both give the right answer.

LESSON 2

Euclid's algorithm for the HCF

Use repeated division with remainder to find the highest common factor of two numbers.

The highest common factor (HCF) of two numbers is the largest number that divides both; the HCF of 12 and 18 is 6.

  1. Division with remainder

    For whole numbers a and b > 0 we can always write a = bq + r with 0 ≤ r < b. For example, 85 = 12 × 7 + 1.

    85 = 12 × 7 + 1a = 85, b = 12, q = 7, r = 10 ≤ 1 < 12
  2. Common factors carry over

    Any number that divides both a and b also divides r = a − bq, and the other way round. So the pair (b, r) has the same HCF as (a, b), with smaller numbers.

  3. Repeat until the remainder is 0

    HCF(252, 105): 252 = 105 × 2 + 42; 105 = 42 × 2 + 21; 42 = 21 × 2 + 0. The last divisor, 21, is the HCF.

    DivisionQuotientRemainder252 ÷ 105242105 ÷ 4222142 ÷ 2120
  4. Check the answer

    252 = 21 × 12 and 105 = 21 × 5. Since 12 and 5 have no common factor other than 1, no number larger than 21 divides both.

    252 = 21 × 12105 = 21 × 512 and 5 share only 1, so HCF = 21

Worked example: Ribbons of 96 cm and 60 cm are cut into equal pieces, as long as possible, with nothing left over. 96 = 60 × 1 + 36; 60 = 36 × 1 + 24; 36 = 24 × 1 + 12; 24 = 12 × 2 + 0. Each piece is 12 cm: 8 pieces from the first ribbon and 5 from the second.

Watch for this: The HCF is the last non-zero remainder, not a quotient. For 252 and 105 every quotient is 2, but the HCF is 21, the divisor that leaves remainder 0.

Discuss: Count the divisions Euclid's algorithm needs for HCF(89, 55) and for HCF(100, 50). Suggest what kind of number pair makes the algorithm take many steps.

LESSON 3

Improving and analysing an algorithm

Count the steps an algorithm takes, cut out wasted work, and compare the subtraction and division forms of Euclid's gcd algorithm.

Comparing algorithms by steps
Comparing algorithms by steps

Euclid's algorithm replaces a pair of numbers by a smaller pair with the same HCF and stops when the remainder is 0.

  1. Count the checks

    A simple gcd method tests every number from 1 upwards to see if it divides both numbers. For gcd(84, 120), testing every number up to 120 means 120 checks.

    0102030405060708090100110120120 checks
  2. Cut wasted work

    A common divisor can never be larger than the smaller number, so stop at 84: 84 checks, same answer. But the work still grows with the size of the numbers: a smaller number ten times as big needs about ten times the checks.

    120Test 1 to 12084Stop at 84Stopping at 84 saves 36 checks for gcd(84, 120)
  3. Euclid by subtraction

    For m ≥ n, gcd(m, n) = gcd(n, m − n). 120 − 84 = 36, 84 − 36 = 48, 48 − 36 = 12, 36 − 12 = 24, 24 − 12 = 12, 12 − 12 = 0. Six subtractions reach the pair (12, 0), so the gcd is 12.

    120 − 84 = 36, 84 − 36 = 4848 − 36 = 12, 36 − 12 = 2424 − 12 = 12, 12 − 12 = 0Pair (12, 0): gcd = 12
  4. Euclid by division, compared

    120 = 84 × 1 + 36, 84 = 36 × 2 + 12, 36 = 12 × 3 + 0: only 3 divisions. One division does a whole run of subtractions at once. Check: both forms give 12, and 120 = 12 × 10, 84 = 12 × 7.

    6Subtractions3DivisionsBoth reach gcd(84, 120) = 12

Worked example: gcd(1,000, 3) by subtraction: taking away 3 again and again, 333 subtractions bring 1,000 down to 1; then 3 − 1 = 2, 2 − 1 = 1 and 1 − 1 = 0 take 3 more, so 336 subtractions in all. By division: 1,000 = 3 × 333 + 1 and 3 = 1 × 3 + 0, only 2 divisions. Both give gcd 1.

Watch for this: A faster algorithm does not give a different answer. The subtraction and division forms of Euclid's algorithm always reach the same gcd; they differ only in how many steps they take.

Discuss: Choose two pairs of 3-digit numbers: one pair close together and one where one number is much larger. Count the subtractions and the divisions Euclid's algorithm needs for each pair, and discuss when the subtraction form is slow.

Continue on paper

Sources and coverage

Original learning companion for selected outcomes of this chapter: what makes a procedure an algorithm (precise steps, decisions and repeats), Euclid's division algorithm for the HCF, and improving and analysing an algorithm by counting its steps and comparing the subtraction and division forms of Euclid's algorithm. It does not reproduce the textbook, its activities or its exercise solutions.

Original HelioLearn teaching and questions, aligned to the listed CBSE topics. It is not an official publication of the board or a textbook solution bank. Independent teacher review is pending.