Skip to chapter text
Ganita ManjariCh 11 The World of Algorithms

The World of Algorithms: Class 9 Ganita Manjari · Chapter 11

Ganita Manjari Part 2 · Companion notes, simulations and practice · Official NCERT chapter PDF ↗

Published · Updated

  • 65 min
  • 22 practice questions
  • 20 MCQs

What you will learn in The World of Algorithms

  • Explain what an algorithm is, and recognise its basic steps, repeated steps and conditional steps.
  • Write column addition as a precise algorithm with a carry, execute it, and justify it using place value.
  • Compare the work of two methods by asking how it grows as the numbers get larger.
  • Build gcd algorithms from the definition using lists of divisors, and improve them step by step.
  • Describe a list as a data structure and use the fact that a list is sorted.
  • Prove that gcd(m, n) = gcd(n, m − n) and gcd(m, n) = gcd(n, m mod n), and use them in Euclid’s algorithm.
  • Find the gcd of large numbers by Āryabhaṭa’s division method and explain why it needs few steps.

Original companion text aligned to NCERT Mathematics Class 9, Chapter 11, pp. 37–50. Not affiliated with or endorsed by NCERT.

Ask a map app for the quickest way to the railway station and it suggests a route within a second. A music app picks the next song, and a shopping site guesses what you might buy. People shrug and say, “the algorithm decided”. So what is this thing called an algorithm, and where does mathematics come into it?

Think about how tea is made at home. Boil water, add tea leaves, then milk, then sugar “to taste”. That is a list of steps, but “to taste” leaves the cook to decide. A mathematical procedure leaves nothing to taste. Every step says exactly what to do, so anyone who follows it reaches the same answer. In this chapter we study two such procedures closely: adding numbers and finding a greatest common divisor. For each one we ask three questions. What are the exact steps? Why do they give the right answer? How much work do they take?

11.1 Adding Numbers

Suppose nobody had ever shown you column addition, and someone asked for 586 + 749. You could draw 586 dots, draw 749 more, and count the whole crowd: all 1335 of them. That is really how small children add 6 + 8, by counting everything. The method never fails, but for big numbers it would take ages.

You already know a far better way, and Fig. 11.1 puts it next to counting for a small sum. On the left, every dot has to be counted. On the right, the numbers are stacked by place value, and two single-digit sums finish the job. When a column reaches 10 or more, a carry is handed to the next column.

3/3The column method needs just two one-digit sums.

Fig. 11.1Two ways to add 23 and 14.NCERT p. 37

Left panel: 23 blue dots in rows of ten and 14 orange dots below them. Right panel: 23 written above 14 in columns. In the first step only the dots and the column sum are set out. In the second step every dot is counted, and a label says this takes 37 counts. In the third step the column method finds 3 + 4 = 7 in the units column and 2 + 1 = 3 in the tens column, giving 37 with only two small additions.

11.1.1 Adding Numbers Digit by Digit

Column addition deserves a name of its own: it is an algorithm (कलन-विधि). What makes it one is not cleverness but two other qualities. It is general: the same instructions handle 23 + 14 and 586 + 749 alike. And it is exact: no instruction needs judgement or taste.

Definition
Algorithm

A finite list of exact steps that, followed in order, solves every problem of a given kind.

ExampleColumn addition is an algorithm for adding any two whole numbers.

Let us write the instructions down so carefully that someone who has never met column addition, or a machine, could follow them. We call the result ADD. The word carry names a number that we keep track of while we work; you will soon see why it is always 0 or 1.

When we follow ADD's steps for two particular numbers, we are said to execute (or run) it. Fig. 11.2 executes ADD on 586 + 749. Step 2 handles one column at a time, and Step 3 keeps sending us one column to the left until none are left.

5/5No columns left and carry is 1, so write 1 at the far left: 1335.

Fig. 11.2ADD executed on 586+749. Each column is one basic step.NCERT Algorithm to add two numbers · p. 37

The numbers 586 and 749 are written in columns with a shaded box marking the current column. Step 1: the numbers are lined up and carry is 0. Step 2: in the units column 6 + 9 + 0 = 15, so 5 is written and a carry of 1 appears above the tens column. Step 3: in the tens column 8 + 4 + 1 = 13, so 3 is written and a carry of 1 appears above the hundreds. Step 4: in the hundreds column 5 + 7 + 1 = 13, so 3 is written with carry 1. Step 5: no columns are left, so the carry 1 is written at the far left, giving 1335.

ExampleNCERT 11.1.1 · p. 38

Executing ADD, column by column

Execute ADD on 7058 + 2967. For each column, show the total s, the digit written and the new value of carry.

Try it yourself first. Then reveal the solution one step at a time.

ADD never says how to work out 8 + 7. It simply trusts you to know single-digit sums by heart, the way a recipe trusts you to know how to boil water. Actions that an algorithm uses without spelling them out are its basic steps. For ADD, one column is a basic step: add two digits and a carry, write a digit, update the carry. Any algorithm has to stop explaining somewhere, and its basic steps mark that point.

Practice · 7 questions

Practice 11.1: questions with answers

  1. Short answerEasyNCERT Exercise Set 11.1, Q1

    Act as a machine that knows nothing except Algorithm ADD. Add 6847 and 5976, writing down s and carry for each column. Then check your answer by subtraction.

    Show solution

    Answer12823

    1. Units: 7+6+0=13, write 3, carry 1.
    2. Tens: 4+7+1=12, write 2, carry 1.
    3. Hundreds: 8+9+1=18, write 8, carry 1.
    4. Thousands: 6+5+1=12, write 2, carry 1.
    5. No columns left and carry is 1: write 1 at the far left.

    The result 12823 is correct; you can check it as 12823−5976=6847.

  2. Short answerMediumNCERT Exercise Set 11.1, Q2

    Now try ADD on 40382 + 976, where the numbers have different lengths.

    1. In the thousands column there is a 0 on top and no digit at all below it. Taken word for word, can Step 2 be carried out there?

      Show solution

      AnswerNo: Step 2 needs two digits.

      Step 2 says “add the two digits in the current column”. Here there is only one digit, so a reader who follows the words exactly gets stuck. The algorithm, as written, only handles numbers of equal length.

    2. Change Step 1 so that ADD handles numbers of different lengths. Then find the sum.

      Show solution

      Answer41358

      Add to Step 1: “If one number has fewer digits, write 0s in its empty places on the left.” So 976 becomes 00976, which has the same value.

      1. Units: 2+6=8, carry 0.
      2. Tens: 8+7=15, write 5, carry 1.
      3. Hundreds: 3+9+1=13, write 3, carry 1.
      4. Thousands: 0+0+1=1, carry 0.
      5. Ten-thousands: 4+0+0=4. The sum is 41358.
  3. Short answerEasyNCERT Exercise Set 11.1, Q3

    Riya writes 41 under 352 with the first digits lined up, 4 under 3 and 1 under 5, and then runs ADD. What answer does she get, and what went wrong?

    Show solution

    Answer762; the true sum is 393.

    Each column must hold digits of the same place value, so that ones are added to ones and tens to tens. Lined up on the left, the 4 of 41 sits in the hundreds column and the 1 in the tens column. ADD then really adds 410, giving 352+410=762 instead of 352+41=393.

  4. Short answerMediumNCERT Exercise Set 11.1, Q4

    Could ADD ever need a carry of 2? Find the largest total any column can have, and use it to decide.

    Show solution

    AnswerEach total is at most 19.

    In the units column carry is 0, so s≤9+9+0=18 and the new carry is at most 1.

    If carry is at most 1 coming into a column, then s≤9+9+1=19. That is less than 20, so s−10≤9 is a single digit and the new carry is again at most 1. Column after column, the carry stays 0 or 1.

  5. Short answerMediumNCERT Exercise Set 11.1, Q5

    A printing error deletes Step 4 from ADD.

    1. Give a sum for which ADD still gives the right answer.

      Show solution

      AnswerFor example 314 + 452.

      In 314 + 452 the hundreds column gives 3+4=7 with carry 0, so nothing is lost: the answer 766 is right.

    2. Give a sum for which it fails, and the wrong answer it gives.

      Show solution

      AnswerFor example 814 + 452 gives 266.

      In 814 + 452 the hundreds column gives 8+4=12: write 2, carry 1. Without Step 4 that 1 is never written, so the result reads 266 instead of 1266.

    3. Exactly when is Step 4 needed?

      Show solution

      AnswerWhen the leftmost column totals 10 or more.

      Step 4 writes something only when carry is 1 after the leftmost column, that is, when that column's total is 10 or more.

  6. Quick answerEasy

    In ADD, a column has total s=10. What happens?

    Show answer

    Answer (A) write 0 and set carry to 1

    10 is “10 or more”, so we write 10−10=0 and carry 1. A rule that said “more than 10” would leave this case out, which is why exact wording matters.
  7. Quick answerMedium

    How many times is Step 2 of ADD carried out when it adds two 6-digit numbers?

    Show answer

    Answer 6

    Step 2 runs once per column. Step 4 may add a seventh digit, but it does not run Step 2 again.

Why the column method is so much faster

Both methods give the same answer, so the real question is how much work each one costs. Add one digit to each number and the numbers become roughly ten times larger, and so does the pile of dots. ADD just gains a single extra column.

Let us put rough times on this. Suppose you can count 2 dots a second, and you spend a careful 10 seconds on each column of ADD. Table 11.1 compares the two methods as the numbers grow.

Table 11.1Counting time grows about ten times for each extra digit; the time for ADD grows by one column (2 dots a second, 10 s a column).
SumDots to countTime by countingColumnsTime by ADD
48 + 368442 s220 s
486 + 365851about 7 min330 s
4862 + 36578519about 71 min440 s
48620 + 3657185191about 12 hours550 s

Read the table down its columns. The counting times explode, from under a minute to half a day, while the times for ADD creep up by 10 seconds a row. This is the whole point of a good algorithm: its work stays modest even when the numbers get large. From now on we judge every algorithm the same way, by asking how its work grows as its input grows.

Why the column method gives the right answer

Speed is worthless if the answer is wrong, so we also owe a reason why ADD always agrees with counting. Picture 47 as 4 rods of ten cubes plus 7 loose cubes, and 38 as 3 rods plus 8 loose cubes, as in Fig. 11.3. Tipping everything into one heap and counting it gives the true total; that is what 47 + 38 means. ADD reaches the same total by counting loose cubes first and rods afterwards.

4/44+3+1=8 tens and 5 ones: the sum is 85.

Fig. 11.3The carry is a bundle of ten ones that becomes one ten.NCERT p. 39

Rods stand for tens and small cubes for ones. In the first step 47 is shown as 4 rods and 7 cubes, and 38 as 3 rods and 8 cubes. In the second step the ones are counted together: 7 + 8 = 15 cubes. In the third step ten of the cubes are bundled into one new rod, the carry, leaving 5 cubes. In the fourth step the rods are counted, 4 + 3 + 1 = 8 tens, so the total is 8 tens and 5 ones, which is 85.

Seven loose cubes and eight loose cubes make fifteen: enough to snap ten of them into a new rod, with five left loose. Snapping that rod together is exactly what the carry records. A hundred is a bundle of ten rods, so the same snapping-together can happen one column further left. Grouping cubes never adds or removes a cube, so the rods and loose cubes we end with count exactly the cubes we started with.

Quick check
1 of 4
Which of these best describes an algorithm?
Show answer

Answer (D) exact steps that solve every problem of one kind

An algorithm such as ADD works for every pair of whole numbers, and each step is exact. People executed algorithms by hand long before computers existed.

11.2 Greatest Common Divisor

Now a second familiar problem. A tailor has two ribbons, 84 cm and 126 cm long. She wants to cut both into pieces of one equal length with nothing left over. What is the longest piece she can cut? The length must divide 84 and divide 126, and we want the largest such number. It is the greatest common divisor of 84 and 126, written gcd⁡(84,126). You may know it as the highest common factor, or HCF (महत्तम समापवर्तक).

Definition
Greatest common divisor (gcd)

The largest whole number that divides each of two given numbers exactly. It is also called the highest common factor (HCF).

Examplegcd⁡(12,18)=6: 6 divides both numbers, and no larger number does.

You could find this with prime factorisation, as in earlier classes. Instead, let us pretend we know nothing except what the words greatest common divisor say, and build an algorithm straight from them. Read the name from the back. Divisor: find the numbers that divide. Common: keep the ones shared by both numbers. Greatest: choose the largest. For two numbers m and n that gives a three-part plan.

  1. Find all the divisors of m and write them down.
  2. Find all the divisors of n and write them down.
  3. Compare the two collections and pick out the largest number that appears in both.

Each part is a smaller job than the whole problem, and smaller jobs are easier to turn into exact steps. We take them one at a time.

11.2.1 Computing the Divisors of a Number

A divisor (भाजक) of n is a whole number that divides n with remainder 0. Finding all of them is like taking a roll call. Call out 1, 2, 3, and so on up to n, and every number that divides n exactly answers “present”. Nobody beyond n can answer, because a divisor of n is never larger than n.

The answers must be written down somewhere. We use a list: an ordered row of values inside square brackets, such as [2,5,9]. Before the roll call starts, nobody has answered, so the list begins empty, written [  ]. Each new answer is appended, that is, written at the end.

Try reading Step 2 aloud without the letter j: “for each number from 1 to n, if that number divides n, append that number…”. It soon gets muddled. Short labels such as j and found keep the steps crisp. A label for a value that changes while an algorithm runs is called a variable. Step 2 also shows two new features: a step can be repeated (once for each j), and it can be conditional (append j only if it divides n).

5/5Only 20 itself succeeds. The list is complete.

Fig. 11.4DIVISORS(20): every candidate is tested; the divisors join found in increasing order.NCERT p. 40

The numbers 1 to 20 sit in two rows of boxes, with a pointer marking the current value of j, and the list found is written below. Step 1: found is the empty list. Step 2: j = 1 and j = 2 both divide 20, so found becomes [1, 2]. Step 3: j = 3 fails while 4 and 5 divide 20, giving [1, 2, 4, 5]. Step 4: of 6 to 10 only 10 divides 20. Step 5: of 11 to 20 only 20 divides 20, so the final list is [1, 2, 4, 5, 10, 20].

The finished list [1,2,4,5,10,20] comes out in increasing order. That is no accident: the roll call runs upwards, so each divisor joins the list after every smaller one. Keep this fact in mind, because we use it soon. Here the basic step is a divisibility test, which we treat as something we can always do, for instance by long division.

ExampleNCERT 11.2.1 · p. 41

Divisors of 63

Execute DIVISORS(63). How many tests does it make, and how many divisors does it find?

Try it yourself first. Then reveal the solution one step at a time.

11.2.2 Finding the Greatest Common Divisor

Two lists are on the table. Which number is in both, and biggest? For 36 and 60 the answer, 12, almost jumps out, as Fig. 11.5 shows. For 1680 and 2520, which have 40 and 48 divisors, nothing jumps out, and a tired eye easily skips a match. (Their gcd is 840; a much faster method later in the chapter finds it in two divisions.)

So we hand the comparison to one more small algorithm. Picture a shopkeeper checking a delivery: she reads down her order list and ticks each item that also appears on the delivery note. In the same way, we read the first divisor list from left to right and copy every entry that also appears in the second list into a new list, common. Since 1 divides everything, common always gets at least one entry.

Now a pleasant surprise. Both divisor lists were sorted, and we read the first one from left to right, so common is sorted too. Its biggest entry sits at the right-hand end. The gcd is therefore simply the last entry of common, and no further search is needed.

4/4The last entry of common is the largest: gcd⁡(36,60)=12.

Fig. 11.5The list of common divisors is in increasing order, so its last entry is the gcd.NCERT p. 42

Two rows of boxes show the divisors of 36 (nine boxes) and of 60 (twelve boxes). Step 1 shows only the two lists. Step 2 highlights the entries of the first list that also appear in the second and joins each to its match: 1, 2, 3, 4, 6 and 12. Step 3 copies them, in order, into a third row called common. Step 4 rings the last box of common, 12, and labels it as the gcd of 36 and 60.

11.2.3 First Algorithm for gcd

Snap the three parts together and we have a complete gcd algorithm. Notice that it does not repeat the instructions of DIVISORS. It simply calls on DIVISORS twice, the way a biryani recipe might say “cook the rice” without explaining how. Reusing a trusted algorithm inside a bigger one is one of the most useful habits in mathematics and computing.

ExampleNCERT 11.2.3 · p. 43

The tailor’s ribbons

Use GCD-LIST to find the longest equal pieces that can be cut from ribbons of 84 cm and 126 cm with nothing left over.

Try it yourself first. Then reveal the solution one step at a time.

Quick check
1 of 4
DIVISORS(n) tests every j from 1 to n. Why is its output list in increasing order?
Show answer

Answer (B) it tests the candidates from smallest to largest

Each divisor is appended when it is found, and the candidates are tried in increasing order. There is no separate sorting step, and 36 shows that the number of divisors can be odd.

11.3 Data Structures

Think of the ticket checker on a long-distance train. Her reservation chart is printed coach by coach and berth by berth, so she can find any passenger in seconds. If the names were printed in the order the tickets were booked, the same information would cost her far more effort. How information is arranged decides how quickly we can use it.

Algorithms face the same issue. GCD-LIST keeps working values that are not the answer itself: the lists A, B and common. Each is a whole collection of numbers, held in increasing order. Any such arrangement of working values is called a data structure, and the sorted list is one of the simplest. Its sortedness paid off at the end, when the gcd could be read straight from the right-hand end of common.

Giving each working value a short name, as we did with A, B and common, keeps the steps readable. Your phone's contact list is another data structure you use daily: sorted by name, it lets you find a friend without reading every entry. Higher classes study many more.

Definition
Data structure

A way of organising the values an algorithm works with, such as a list kept in increasing order, so that its steps can use them easily.

ExampleBecause common is sorted, GCD-LIST reads the gcd off as its last entry.

Practice · 3 questions

Practice 11.2: questions with answers

  1. Short answerMediumNCERT Exercise Set 11.2, Q1

    A class teacher keeps two lists of roll numbers, each in increasing order: P for students who handed in homework on Monday, and Q for Tuesday.

    1. Write an algorithm that produces the roll numbers that appear in P but are missing from Q.

      Show solution
      1. Make an empty list onlyP.
      2. For each value x in P, from left to right: if x is not in Q, append x to onlyP.
      3. Report onlyP.
    2. Execute it for P = [2, 5, 8, 11, 14] and Q = [1, 5, 9, 14, 20].

      Show solution

      Answer[2, 8, 11]

      2 is not in Q (append), 5 is (skip), 8 is not (append), 11 is not (append), 14 is (skip). Result: [2, 8, 11].

    3. Who handed in on Tuesday only? Adapt your algorithm and run it on the same lists.

      Show solution

      AnswerSwap the roles of P and Q: [1, 9, 20].

      Walk along Q instead and test membership in P. 1, 9 and 20 are not in P, while 5 and 14 are, so the result is [1, 9, 20].

    4. Both lists are sorted. How could you avoid searching all of Q for every x?

      Show solution

      Keep a pointer in each list and compare the two values pointed at. If the P value is smaller, it cannot appear later in Q, so append it and move along P. If the two are equal, move both pointers. If the Q value is smaller, move along Q. When Q runs out, append the rest of P. Each list is read only once.

  2. Short answerMediumNCERT Exercise Set 11.2, Q2

    Two buses leave a depot together at 6 a.m., one every 12 minutes and one every 18 minutes. They next leave together after lcm⁡(12,18) minutes, the smallest whole number that both 12 and 18 divide.

    1. Write an algorithm that finds lcm⁡(m,n) for any two whole numbers.

      Show solution
      1. Let L be the larger of m and n, and S the smaller.
      2. For k=1,2,3,… in turn: if S divides k×L, report k×L and stop.

      It must stop, at the latest when k=S, because S divides S×L.

    2. Execute it for the buses. When do they next leave together?

      Show solution

      AnswerAfter 36 minutes, at 6:36 a.m.

      L=18 and S=12. For k=1: 12 does not divide 18. For k=2: 12 divides 36. Report 36, so the buses next leave together after 36 minutes, at 6:36 a.m.

  3. Short answerHardNCERT Exercise Set 11.2, Q3

    When 36 is written as a product of two whole numbers, its divisors turn up two at a time: 1×36, 2×18, 3×12, 4×9 and 6×6.

    1. Suppose DIVISORS appends both numbers of a pair whenever it finds the smaller one. Up to which j must it test for n=36? For a general n?

      Show solution

      AnswerUp to 6; in general, while j×j≤n.

      If both numbers of a pair were larger than 6, their product would be larger than 36. So the smaller number of each pair is at most 6, and testing j=1 to 6 finds every pair. In general we test only the j with j×j≤n: about n values instead of n.

    2. This gives the list [1, 36, 2, 18, 3, 12, 4, 9, 6] for 36. Would GCD-LIST still work with lists in this order? What would you change?

      Show solution

      AnswerNot always; report the largest entry.

      The lists are no longer sorted, so the last entry of common need not be the largest. For m=36 and n=18: A = [1, 36, 2, 18, 3, 12, 4, 9, 6] and B = [1, 18, 2, 9, 3, 6], so common = [1, 2, 18, 3, 9, 6]. Its last entry is 6, but gcd⁡(36,18)=18.

      Fix: change Step 3 to report the largest entry of common (or sort the lists first).

11.3.1 Improving the Algorithm

GCD-LIST is correct, but wasteful. A good habit is to look at an algorithm and ask: which work could never change the answer? Each time we spot such waste we can remove it. Table 11.2 records four rounds of trimming for m=36 and n=60. It scores each version by the candidates it looks at and the values it keeps in memory.

Table 11.2Trimming GCD-LIST for m=36 and n=60: candidates looked at and values kept in memory.
VersionWhat changesLooked atKept
1. GCD-LISTTwo separate scans, then the lists are compared9627
2. One scanEach j up to the larger number is tested against both m and n6027
3. Common list onlyj goes straight into common; A and B are never built606
4. Stop at minThe scan stops at the smaller number, since nothing larger can divide it366
5. GCD-SCANOnly the latest common divisor, best, is kept361

The last change deserves a closer look. The scan climbs upwards, so every common divisor it meets is bigger than all the ones before, and the older ones are dead weight. One variable, best, is enough. It begins at 1, which divides every number, and is overwritten whenever a bigger common divisor turns up. Fig. 11.6 shows the shrinking work as bars.

5/5Still 36 candidates, and only one number, best, is stored.

Fig. 11.6Each refinement looks at fewer candidates or stores less (m=36, n=60).NCERT p. 44

Five labelled bars, one per version of the algorithm for m = 36 and n = 60, appear one at a time. Step 1: GCD-LIST with two separate scans looks at 96 candidates. Step 2: one scan up to the larger number looks at 60. Step 3: building the common list directly still looks at 60 but stores fewer numbers. Step 4: stopping at the smaller number cuts this to 36. Step 5: keeping only the best divisor so far still looks at 36 candidates but stores just one number.

Practice · 2 questions

Practice 11.3: questions with answers

  1. Short answerMediumNCERT Exercise Set 11.3, Q1

    A friend rewrites DIVISORS so that j counts down: n, n−1, …, 1.

    1. In what order would it list the divisors of 20?

      Show solution

      Answer[20, 10, 5, 4, 2, 1]

      The largest divisor is found first, so the list is in decreasing order: [20, 10, 5, 4, 2, 1].

    2. Which step of GCD-LIST must change, and how?

      Show solution

      AnswerStep 3: report the first entry.

      Now common is in decreasing order, so its largest entry is the first one. Step 3 must report the leftmost entry of common instead of the rightmost.

  2. Short answerMediumNCERT Exercise Set 11.3, Q2

    Run the scan of GCD-SCAN backwards: let k go from min⁡(m,n) down to 1.

    1. Explain why you can stop at the first k that divides both numbers.

      Show solution

      Every k above it has already been tested and failed. So the first common divisor met on the way down is the largest one, which is the gcd. Nothing below it can beat it.

    2. How many values of k does the backward scan look at for m=48, n=80? How many does the forward scan look at?

      Show solution

      AnswerBackward 33, forward 47.

      gcd⁡(48,80)=16. Backwards, k runs 48, 47, …, 16 and stops: 48−16+1=33 values. Forwards, k runs from 2 to 48: 47 values.

    3. For which pairs does the backward scan save nothing?

      Show solution

      AnswerPairs with gcd 1.

      If gcd⁡(m,n)=1, no k above 1 works, so the backward scan goes all the way down to 1, looking at every value from min⁡(m,n) down.

11.3.2 Analysing these Algorithms

We now have five versions of one algorithm. Are they all right, and are any of them fast? These are the two questions to ask about every algorithm.

Correctness. Think of the versions as links in a chain. The first link, GCD-LIST, can be trusted because it is the definition of the gcd turned into steps. Each later link differs from the one before only by throwing away work that could not affect the result: candidates too large to divide both numbers, or lists that were never read again. A chain of such links does not break, so GCD-SCAN at the end is as correct as GCD-LIST at the start.

ExampleNCERT Example · p. 45

Executing GCD-SCAN

Execute GCD-SCAN for m=20 and n=8. Show the value of best after each k.

Try it yourself first. Then reveal the solution one step at a time.

Efficiency. Speed is another matter. GCD-SCAN still visits every candidate up to the smaller number. Hand it a pair of 9-digit numbers and it may look at hundreds of millions of candidates; a computer copes, but a person with pencil and paper never would. In the language of Table 11.1, GCD-SCAN behaves like counting dots, not like ADD. That raises a challenge: is there a gcd algorithm whose work follows the length of the numbers rather than their size?

Quick check
1 of 4
How many values of k does GCD-SCAN look at for m=150 and n=84?
Show answer

Answer 83

k runs from 2 to min⁡(150,84)=84, which is 84−2+1=83 values.

11.3.3 Euclid’s Subtraction Algorithm

The answer is more than 2300 years old. Around 300 BCE, Euclid of Alexandria gathered the mathematics of his time into the Elements, a work studied for over two thousand years. This method appears near the start of its Book VII. It was probably not his own invention, since earlier Greek mathematicians seem to have used it, but his account is the one that has come down to us.

Making the problem smaller

Try a small experiment first. The common divisors of 30 and 18 are 1, 2, 3 and 6. Now replace 30 by 30−18=12: the common divisors of 18 and 12 are again 1, 2, 3 and 6. Nothing was lost and nothing was gained, yet the numbers got smaller. Euclid's idea is that this always happens.

PropertyEuclid’s reduction

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

Fig. 11.7 shows the idea on a number line. A hop length that lands exactly on n and on m also covers the stretch between them, of length m−n, in whole hops. Euclid himself reasoned with lengths in much this way.

3/3The stretch m−n=12 is exactly two hops: 6 divides 12.

Fig. 11.7If hops of length d land on n and on m, they cover m−n in whole hops too.NCERT Fig. 11.1 · p. 46

A number line runs from 0 to 30 with marks every 6 units. Step 1: three hops of length d = 6 from 0 land exactly on n = 18. Step 2: two more hops land exactly on m = 30. Step 3: the stretch from 18 to 30, of length m − n = 12, is highlighted and is covered by exactly two of the hops, so 6 divides m − n as well.

Each subtraction makes the total m+n smaller, so the process cannot go on for ever: sooner or later the smaller number hits 0. What is gcd⁡(m,0)? Every whole number d divides 0, because 0=0×d. So the common divisors of m and 0 are just the divisors of m, and the greatest of these is m itself. Hence gcd⁡(m,0)=m: when a 0 appears, the other number is the answer. For example, (9,9) becomes (9,0), and the answer is 9.

ExampleNCERT 11.3.3 · p. 47

Euclid by subtraction

Use EUCLID-SUB to find gcd⁡(91,35). How many subtractions does it make?

Try it yourself first. Then reveal the solution one step at a time.

There is a neat picture of EUCLID-SUB. Draw an m by n rectangle. Each subtraction cuts an n by n square off the end, leaving a smaller rectangle whose sides are the new pair. When a cut leaves nothing over, the side of the last square is the gcd. Fig. 11.8 does this for a 42 by 24 rectangle.

4/4Three subtractions use up the rest exactly. 5 cuts in all: the gcd is 6.

Fig. 11.8Each subtraction cuts off a square; the smallest square that fits exactly has side gcd⁡(42,24)=6.NCERT p. 47

A rectangle 42 units wide and 24 units tall. Step 1 shows the whole rectangle and the pair (42, 24). Step 2 cuts off a 24 by 24 square on the left, leaving an 18 by 24 strip: the pair (24, 18). Step 3 cuts an 18 by 18 square off the strip, leaving an 18 by 6 piece: the pair (18, 6). Step 4 cuts the piece into three 6 by 6 squares with nothing left over, so the gcd is 6.

EUCLID-SUB is a big step forward, but give it one large number and one small one and it crawls. For gcd⁡(1000,3) it chips away 3 at a time: (1000,3), (997,3), (994,3), … all the way down to (3,1), and then needs three more steps. That is 336 subtractions. A 7-digit number paired with 3 would need over a million. The work still follows the size of the numbers, not their length.

Quick check
1 of 4
Put the pairs in the order EUCLID-SUB reaches them, starting from (40,15) (pairs shown after any swap).
  1. (40,15)
  2. (25,15)
  3. (15,10)
  4. (5,0)
  5. (5,5)
  6. (10,5)
Show answer
Answer
  1. (40,15)
  2. (25,15)
  3. (15,10)
  4. (10,5)
  5. (5,5)
  6. (5,0)
40−15=25, 25−15=10, 15−10=5, 10−5=5, 5−5=0. Five subtractions, and the answer is 5.

11.3.4 Āryabhaṭa’s Division Algorithm

Look again at gcd⁡(1000,3). Taking 3 away from 1000 again and again, 333 times, leaves 1. One division does exactly that in a single stroke: 1000=333×3+1. The leftover 1 is the remainder.

Indian mathematicians worked in exactly this way. In the Āryabhaṭīya, completed in 499 CE, Āryabhaṭa describes a method, now known as kuṭṭaka (“the pulveriser”), for solving certain equations in whole numbers. Its first stage divides two numbers into each other again and again, each time keeping only the remainder. That repeated division is precisely what finds a gcd, and with Indian place-value numerals it became a compact long-division method.

Definition
m mod n

m mod n is the remainder left over after dividing m by n. It is always less than n.

Example23 mod 4=3, since 23=5×4+3; and 50 mod 7=1.

That suggests a one-line upgrade to EUCLID-SUB. Wherever it would subtract n from m over and over, let it divide once and keep the remainder, so that the pair (m,n) jumps straight to (n,m mod n). The theorem below shows that the jump keeps the gcd the same.

PropertyReduction by remainder

If n>0, then gcd⁡(m,n)=gcd⁡(n,m mod n).

Step 1 now matters only at the very start, because a remainder is always smaller than the divisor. Run the slow case again: gcd⁡(1000,3) becomes gcd⁡(3,1), then gcd⁡(1,0), and the answer 1 arrives after 2 divisions instead of 336 subtractions. In fact gcd⁡(m,3) never needs more than 3 divisions, however large m is.

The long-division layout

By hand, EUCLID-DIV is a chain of divisions. Each divisor becomes the next number to be divided, and each remainder becomes the next divisor. The chain stops at remainder 0, and the last divisor, which is the last non-zero remainder, is the gcd. Indian texts set this out compactly, writing each new division just below and to the right of the one before. Fig. 11.9 follows the chain for gcd⁡(1155,546).

5/5The last non-zero remainder is the gcd: gcd⁡(1155,546)=21.

Fig. 11.9Each divisor moves left and each remainder moves into the divisor’s place, until the remainder is 0.NCERT long division layout · p. 49

A table with columns divide, by, quotient and remainder. Step 1: 1155 divided by 546 gives quotient 2 and remainder 63. Step 2: arrows carry 546 to the divide column and 63 to the by column of the next row: 546 divided by 63 gives 8, remainder 42. Step 3: 63 divided by 42 gives 1, remainder 21. Step 4: 42 divided by 21 gives 2, remainder 0. Step 5: the last non-zero remainder, 21, is circled as the gcd.

The same pair takes 13 subtractions with EUCLID-SUB but only 4 divisions here.

ExampleNCERT 11.3.4 · p. 49

Euclid with division

Find gcd⁡(2585,44) with EUCLID-DIV, and compare the work with EUCLID-SUB.

Try it yourself first. Then reveal the solution one step at a time.

InteractiveAlgorithm stepper

An algorithm stepper. Buttons choose one of five algorithms from this chapter: ADD, DIVISORS, GCD-SCAN, EUCLID-SUB and EUCLID-DIV. Number boxes take your own inputs. The Step button carries out one basic step and writes it in a numbered log, Run to end finishes the algorithm (long runs are summarised), and Reset starts again. A panel shows the current state: the columns, digits and carry for ADD, the value of j and the list found for DIVISORS, k and best for GCD-SCAN, and the current pair for the two Euclid algorithms. A counter shows how many basic steps have been used, and for the gcd algorithms a comparison line gives the number of steps each of the three gcd methods needs for the same pair.

Fig. 11.10Step through ADD, DIVISORS, GCD-SCAN, EUCLID-SUB and EUCLID-DIV with your own numbers, and compare how many steps each one takes.NCERT p. 49

Why is EUCLID-DIV so quick in general? A remainder is always smaller than its divisor. With a little more care one can show something stronger: after any two divisions, the larger number of the pair has dropped below half its old value. Each digit of a number can survive only about 313 halvings, so the count of divisions grows with the number of digits. A complete proof waits for a later class; for now, test the claim in the stepper.

Quick check
1 of 4
47 mod 6=
Show answer

Answer 5

47=7×6+5, so the remainder is 5.
Timeline

Algorithms through time

  1. c. 300 BCE

    Euclid’s Elements

    The gcd found by repeated subtraction.

  2. 499 CE

    Āryabhaṭa’s Āryabhaṭīya

    Repeated division with remainders, in the kuṭṭaka method.

  3. c. 820 CE

    al-Khwārizmī, Baghdad

    A book on Indian numerals and how to compute with them.

  4. 12th century

    Latin translation

    Indian arithmetic, called algorismus, spreads through Europe.

  5. 1844

    Gabriel Lamé

    Proves that Euclid’s division algorithm needs few steps.

Recap

The World of Algorithms: key points and summary

  1. An algorithm is a finite list of exact steps that solves every problem of a given kind. It is built from basic steps that we can already do (Section 11.1.1).
  2. ADD adds two numbers column by column from the right, with a carry that is always 0 or 1 (Algorithm ADD).
  3. ADD is correct because it adds ones, tens and hundreds separately; a carry is a bundle of ten (why ADD works).
  4. Counting dots takes about ten times longer for each extra digit; ADD needs just one more column (why ADD is fast).
  5. Steps may repeat (ADD runs Step 2 once per column) and may be conditional (GCD-SCAN changes best only when k divides both numbers) (Section 11.2.1).
  6. DIVISORS(n) tests every candidate from 1 to n and returns the divisors in increasing order (Section 11.2.1).
  7. GCD-LIST keeps the divisors common to both lists; the last entry of that sorted list is the gcd (Section 11.2.3).
  8. A data structure, such as a sorted list, organises the values an algorithm works with (Section 11.3).
  9. Scanning once, stopping at min⁡(m,n) and keeping only best make the work smaller, but it still grows with the size of the numbers (Section 11.3.1, Section 11.3.2).
  10. Euclid: gcd⁡(m,n)=gcd⁡(n,m−n) for m≥n, and gcd⁡(m,0)=m (Section 11.3.3).
  11. Āryabhaṭa’s division method uses gcd⁡(n,m mod n); its number of steps grows only with the number of digits (Section 11.3.4).
  12. For every algorithm we ask two questions: is it correct, and how does its work grow with the size of its input (Section 11.3.2)?
  13. The word algorithm comes from al-Khwārizmī, whose book carried Indian arithmetic to the wider world (history).
Practice · 10 questions

Practice questions with answers (NCERT “End-of-chapter exercises”)

  1. NumericalMediumNCERT End-of-chapter Exercises Q1

    Use EUCLID-DIV to find each gcd. Show every division.

    1. gcd⁡(1365,1001)

      Show solution

      Answer91

      Each row divides m by n; the next row divides n by the remainder.

      mnremainderquotient
      136510013641
      10013642732
      364273911
      2739103

      The last non-zero remainder is 91, so the gcd is 91.

    2. gcd⁡(43200,75600)

      Show solution

      Answer10800

      Step 1 swaps the numbers, since 43200<75600.

      Each row divides m by n; the next row divides n by the remainder.

      mnremainderquotient
      7560043200324001
      4320032400108001
      324001080003

      The last non-zero remainder is 10800, so the gcd is 10800.

    3. gcd⁡(5162157,956241)

      Show solution

      Answer1287

      Each row divides m by n; the next row divides n by the remainder.

      mnremainderquotient
      51621579562413809525
      9562413809521943372
      3809521943371866151
      19433718661577221
      1866157722128724
      7722128706

      The last non-zero remainder is 1287, so the gcd is 1287. Six divisions were enough for 7-digit numbers.

    4. gcd⁡(2114112,945945)

      Show solution

      Answer3003

      Each row divides m by n; the next row divides n by the remainder.

      mnremainderquotient
      21141129459452222222
      945945222222570574
      22222257057510513
      570575105160061
      51051600630038
      6006300302

      The last non-zero remainder is 3003, so the gcd is 3003.

  2. ProofMediumNCERT End-of-chapter Exercises Q2

    Let m≥n>0, and write m=qn+r, where r=m mod n. Prove that the common divisors of m and n are exactly the same as the common divisors of n and r.

    Show solution

    Every common divisor of m and n is a common divisor of n and r. If d divides m and n, write m=ad and n=bd. Then r=m−qn=ad−qbd=(a−qb)d, so d divides r (and it divides n already).

    Every common divisor of n and r is a common divisor of m and n. If d divides n and r, write n=bd and r=cd. Then m=qn+r=qbd+cd=(qb+c)d, so d divides m (and n).

    Both directions hold, so the two pairs have the same common divisors. This is why EUCLID-DIV may replace (m,n) by (n,m mod n).

  3. Long answerMediumNCERT End-of-chapter Exercises Q3

    Is 221 prime? Is 223? Checking by hand is tedious, so let an algorithm do it.

    1. Write an algorithm PRIME(n) that reports “yes” for a prime n and “no” otherwise. (A prime has no divisors except 1 and itself.)

      Show solution
      1. Let D be the list DIVISORS(n).
      2. If D has exactly 2 entries, report “yes”; otherwise report “no”.

      This handles n=1 correctly: DIVISORS(1) = [1] has one entry, and 1 is not prime. For 221, D = [1, 13, 17, 221], so the answer is “no”; for 223, D = [1, 223], so it is “yes”.

    2. How many tests does your algorithm make for n=223? Suggest a faster version.

      Show solution

      Answer223 tests; stop once j×j>n.

      DIVISORS(223) tests every j from 1 to 223: 223 tests.

      Faster: test j=2,3,… while j×j≤n, and report “no” at the first j that divides n. If a number has a divisor other than 1 and itself, it has one with j×j≤n (Practice 11.2, Q3). For 223 that means j=2 to 14, only 13 tests, and none succeeds, so 223 is prime.

  4. Long answerHardNCERT End-of-chapter Exercises Q4

    The primes that divide 84 are 2, 3 and 7. Write an algorithm PRIME-DIVISORS(n) that produces such a list for any n, using DIVISORS and PRIME as building blocks. Then execute it for n=90.

    Show solution

    Answer[2, 3, 5]

    1. Let D be the list DIVISORS(n), and make an empty list P.
    2. For each x in D: if PRIME(x) reports “yes”, append x to P.
    3. Report P.

    For 90, D = [1, 2, 3, 5, 6, 9, 10, 15, 18, 30, 45, 90]. Only 2, 3 and 5 are prime, so the result is [2, 3, 5].

  5. Long answerHardNCERT End-of-chapter Exercises Q5

    Prime factors offer yet another road to the gcd.

    1. 360=23×32×5. Suggest a way to store a prime factorisation as a list.

      Show solution

      Answer[(2, 3), (3, 2), (5, 1)]

      Use a list of pairs (prime, power), with the primes in increasing order: [(2, 3), (3, 2), (5, 1)].

    2. Describe an algorithm FACTORISE(n) that builds this list.

      Show solution
      1. Make an empty list F, and set p to 2.
      2. While n>1: count how many times p divides n, dividing n by p each time. If the count is more than 0, append (p,count) to F. Then increase p by 1.
      3. Report F.

      A non-prime p never divides what is left, because its prime factors have already been divided out.

    3. Use the lists for 360 and 756=22×33×7 to find their gcd. State your rule.

      Show solution

      Answer36

      Rule: for each prime in both lists, take the smaller power; leave out primes in only one list. From [(2, 3), (3, 2), (5, 1)] and [(2, 2), (3, 3), (7, 1)] we get 22×32=36. Check with EUCLID-DIV: 756=2×360+36 and 360=10×36.

  6. Short answerMedium

    A shop has 1428 red beads and 1092 blue beads. It wants to make identical gift packets, each with the same numbers of red and blue beads, using every bead. What is the largest number of packets it can make, and what is in each one?

    Show solution

    Answer84 packets of 17 red and 13 blue.

    mnremainderquotient
    142810923361
    1092336843
    3368404

    So gcd⁡(1428,1092)=84 packets. Each packet holds 1428÷84=17 red and 1092÷84=13 blue beads.

  7. Quick answerMedium

    Which pair makes EUCLID-SUB do the most subtractions?

    Show answer

    Answer (D) (100,3)

    The counts are 2, 4, 6 and 36. From 100, subtracting 3 alone takes 33 steps just to get down to 1.
  8. Quick answerEasy

    Which statement about an algorithm is false?

    Show answer

    Answer (B) it may give different answers to the same input

    Exact steps followed in order always lead to the same result for the same input.
  9. Quick answerEasy

    For every whole number m>0, gcd⁡(m,0)= .

    Show answer

    Answer m

    Every number divides 0, so the common divisors are the divisors of m, and the largest is m.
  10. Quick answerMedium

    If gcd⁡(m,n)=1, the backward scan of Practice 11.3, Q2 looks at every value from min⁡(m,n) down to 1.

    Show answer

    Answer True

    No value above 1 divides both numbers, so the scan only stops when it reaches 1.

Find your NCERT exercise question

Our practice questions are written for ncert.ai and follow the NCERT exercises. Find the one that matches the question in your textbook; its answer is with it.

In your NCERT textbookPractice question here
NCERT Exercise Set 11.1, Q1Practice 11.1 · Q1
NCERT Exercise Set 11.1, Q2Practice 11.1 · Q2
NCERT Exercise Set 11.1, Q3Practice 11.1 · Q3
NCERT Exercise Set 11.1, Q4Practice 11.1 · Q4
NCERT Exercise Set 11.1, Q5Practice 11.1 · Q5
NCERT Exercise Set 11.2, Q1Practice 11.2 · Q1
NCERT Exercise Set 11.2, Q2Practice 11.2 · Q2
NCERT Exercise Set 11.2, Q3Practice 11.2 · Q3
NCERT Exercise Set 11.3, Q1Practice 11.3 · Q1
NCERT Exercise Set 11.3, Q2Practice 11.3 · Q2
NCERT End-of-chapter Exercises Q1End-of-chapter exercises · Q1
NCERT End-of-chapter Exercises Q2End-of-chapter exercises · Q2
NCERT End-of-chapter Exercises Q3End-of-chapter exercises · Q3
NCERT End-of-chapter Exercises Q4End-of-chapter exercises · Q4
NCERT End-of-chapter Exercises Q5End-of-chapter exercises · Q5

Quick check: MCQs on The World of Algorithms

  1. Why the column method gives the right answer4 questions
  2. 11.2.3 First Algorithm for gcd4 questions
  3. 11.3.2 Analysing these Algorithms4 questions
  4. Making the problem smaller4 questions
  5. The long-division layout4 questions
Glossary · 10

Key terms (English–Hindi)

algorithmकलन-विधिkalan-vidhi
A finite list of exact steps that, followed in order, solves every problem of a given kind.First seen in the chapter →
basic stepमूल चरणmool charan
An action an algorithm uses without explaining it, because we can already do it, such as adding two digits.First seen in the chapter →
data structureडेटा संरचनाdata sanrachna
A way of organising the values an algorithm works with, such as a sorted list.First seen in the chapter →
divisorभाजकbhajak
A whole number that divides a given number with remainder 0.First seen in the chapter →
efficiencyदक्षताdakshata
How much work an algorithm needs, judged by how its number of basic steps grows with the size of the input.First seen in the chapter →
executeनिष्पादित करनाnishpadit karna
To carry out the steps of an algorithm one by one for a particular input.First seen in the chapter →
greatest common divisorमहत्तम समापवर्तकmahattam samapavartak
The largest whole number that divides each of two given numbers exactly.First seen in the chapter →
listसूचीsoochi
An ordered row of values in square brackets, such as [1,3,9]; new values are appended at the end.First seen in the chapter →
modशेषफलsheshphal
m mod n is what is left over after taking as many whole lots of n out of m as possible.
variableचरchar
A name for a value that an algorithm keeps track of and may change, such as carry or best.First seen in the chapter →