NCERT Solutions Class 9 Maths Exercise 11.2 — The World of Algorithms
NCERT Class 9 Mathematics — Ganita Manjari Part II, The World of Algorithms, Exercise Set 11.2. All 3 questions solved.
Section 11.2 builds the first algorithm for the greatest common divisor in two stages: list the divisors of each number, then compare the two lists. This exercise asks you to write algorithms of the same kind yourself. Three tools from the chapter are used throughout.
- A list is written in square brackets, for instance $[1, 2, 3, 6, 9, 18]$, and a list starts empty, $[\;]$, and grows by adding items at the end.
- Naming the values you track, such as
list-of-divisorsor $j$, is what lets a step say “do this to $j$” briefly. - Executing an algorithm means running through its steps for a small case, to see that it does what it should.
Key insight. An algorithm is a recipe made only of steps we already know how to do: test whether one number divides another, add an item to a list, compare two numbers. Writing one means breaking the job into those small steps and saying in what order, and how many times, to repeat them. Then you run it on a small example to check it.
Question 1
Suppose List 1 and List 2 are two lists of numbers in increasing order.
(i) Write an algorithm to find elements in List 1 that are not present in List 2.
Solution. Start with the plain idea straight from the question: go through List 1 and, for each element, check whether it is in List 2. This is the same pattern the chapter used to build the list of common divisors.
Algorithm to find the elements of List 1 not present in List 2.
- Start with an empty list
only-in-1. - For each number $x$ in List 1: – If $x$ does not appear in List 2, add $x$ to
only-in-1. - Report the list
only-in-1.
Executing it on List 1 $= [1, 3, 5, 7, 9, 11]$ and List 2 $= [3, 4, 5, 9, 10]$: $1$ is not in List 2, so add it, giving $[1]$; $3$ and $5$ are in List 2, so leave the list alone; $7$ is not in List 2, giving $[1, 7]$; $9$ is in it; $11$ is not, giving $[1, 7, 11]$. The answer is $[1, 7, 11]$, which is correct.
A faster algorithm, using the “increasing order”. The question stresses that both lists are in increasing order, which hints at a quicker method. Step 2 above re-reads List 2 from the start for every $x$. But because List 2 is sorted, once we have gone past a value in it we never need to look at that value again. Keep a marker in each list, and move only the marker that is behind.
- Start with an empty list
only-in-1. Place a marker $a$ on the first element of List 1 and a marker $b$ on the first element of List 2. - Repeat while $a$ has not run off the end of List 1: – If $b$ has run off the end of List 2, or the element at $a$ is smaller than the element at $b$: that element is not in List 2 (everything remaining in List 2 is at least as large), so add it to
only-in-1and move $a$ one place on. – Otherwise, if the two elements are equal, it is in both lists: move both markers on. – Otherwise, the element at $a$ is larger, so the element at $b$ can never match anything later in List 1: move $b$ one place on. - Report
only-in-1.
On the same lists: $1 < 3$, so $1$ goes in the answer; $3 = 3$, move both; $5 > 4$, move $b$ past $4$; $5 = 5$, move both; $7 < 9$, so $7$ goes in the answer; $9 = 9$, move both; $11 > 10$, move $b$, and now List 2 has run out, so $11$ goes in the answer. The result is again $[1, 7, 11]$, found with at most as many steps as the two lists together hold, not their product.
Algorithm. Start with an empty list. For each number $x$ in List 1, if $x$ is not in List 2, add $x$ to the list. Report the list.
Check: List 1 $= [1,3,5,7,9,11]$, List 2 $= [3,4,5,9,10]$ gives $[1, 7, 11]$.
(The faster version walks both sorted lists once with two markers, moving only the marker that is behind.)
(ii) Write an algorithm to find elements in List 2 that are not present in List 1.
Solution. This is the same problem with the two lists swapped. Nothing in the algorithm depends on which list is which, so the same steps work with the roles exchanged.
Algorithm to find the elements of List 2 not present in List 1.
- Start with an empty list
only-in-2. - For each number $y$ in List 2: – If $y$ does not appear in List 1, add $y$ to
only-in-2. - Report the list
only-in-2.
On the same example, $3, 5, 9$ are in List 1, while $4$ and $10$ are not, so the answer is $[4, 10]$. The two-marker version swaps in the same way, and gives $[4, 10]$ as well.
Together the two answers describe how the lists differ: List 1 has $1, 7, 11$ that List 2 lacks, and List 2 has $4, 10$ that List 1 lacks, while $3, 5, 9$ are shared.
Algorithm. Start with an empty list. For each number $y$ in List 2, if $y$ is not in List 1, add $y$ to the list. Report the list.
Check: the same lists give $[4, 10]$.
Question 2
Describe an algorithm to compute the least common multiple (lcm) of two numbers.
Solution. Do for the lcm what the chapter did for the gcd: go straight to the definition. The lcm of $m$ and $n$ is the smallest number that both $m$ and $n$ divide. Every such number is a multiple of $m$, so there is no need to look at numbers that are not multiples of $m$ at all.
The multiples of the larger number go up in bigger jumps, so there are fewer to test. Call the larger of the two numbers $M$ and the smaller $N$.
Algorithm to find lcm$(m, n)$.
- Let $M$ be the larger and $N$ the smaller of $m$ and $n$.
- For each $k$ in the sequence $1, 2, 3, \dots$, in order: – Compute $c = M \times k$. – If $N$ divides $c$, report $c$ as lcm$(m, n)$ and stop.
Why it works. The first multiple of $M$ that $N$ divides is divisible by both, so it is a common multiple. Any smaller common multiple would also be a multiple of $M$, and the steps would have stopped there earlier. So what is reported is the least common multiple.
Why it stops. $M \times N$ is divisible by $N$, so at the latest the scan stops at $k = N$. The amount of work is therefore at most $N$ steps.
Executing it for lcm$(12, 18)$: $M = 18$, $N = 12$. For $k = 1$, $c = 18$, and $12$ does not divide $18$. For $k = 2$, $c = 36$, and $12$ divides $36$. So the lcm is $36$.
Executing it for lcm$(375, 825)$ — the numbers the chapter used for the gcd: $M = 825$, $N = 375$. The values of $c$ are $825, 1650, 2475, 3300, 4125$, and the first one divisible by $375$ is $4125 = 375 \times 11$, found at $k = 5$. So lcm$(375, 825) = 4125$.
A second algorithm, using the gcd. Since the product of two numbers equals their gcd times their lcm, once the chapter’s gcd algorithm is available the lcm follows in one step: compute $g = $ gcd$(m, n)$ and report $m \times n \div g$. For $375$ and $825$: $g = 75$ and $375 \times 825 \div 75 = 4125$, the same answer.
Algorithm. Let $M$ be the larger and $N$ the smaller number. For $k = 1, 2, 3, \dots$, compute $M \times k$; the first time $N$ divides it, report it as the lcm and stop.
Check: lcm$(12, 18) = 36$ and lcm$(375, 825) = 4125$. (Alternatively, lcm $= m \times n \div$ gcd$(m, n)$.)
Animation of this question’s idea. Use the controls to change it. Open full size
Question 3
Divisors occur in pairs. For instance, the divisors of $18$ are $(1, 18)$, $(2, 9)$ and $(3, 6)$.
This question carries an asterisk in the book: it is enrichment, and is not assessed.
(i) If we write out divisors in pairs, how many numbers do we have to examine between $1$ and $n$ to find all the divisors of $n$?
Solution. In every pair $(d, n/d)$ the two members multiply to $n$, so they cannot both be larger than $\sqrt{n}$: if they were, their product would be more than $n$. Hence the smaller member of each pair is at most $\sqrt{n}$. Finding it gives its partner for free, since the partner is $n/d$.
So it is enough to examine $j = 1, 2, 3, \dots$ up to $\sqrt{n}$, and for each $j$ that divides $n$ record both $j$ and $n/j$.
For $n = 18$ we have $\sqrt{18} \approx 4.24$, so examine $1, 2, 3, 4$ only:
- $1$ divides $18$: pair $(1, 18)$.
- $2$ divides $18$: pair $(2, 9)$.
- $3$ divides $18$: pair $(3, 6)$.
- $4$ does not divide $18$.
That is all six divisors from $4$ checks, instead of $18$. For the chapter’s example $n = 375$ it is $19$ checks instead of $375$.
If $n$ is a perfect square, for instance $n = 36$, the pair $(6, 6)$ is one divisor, not two, so it should be recorded only once.
Only the numbers from $1$ up to $\sqrt{n}$, that is, about $\sqrt{n}$ numbers (precisely the whole-number part of $\sqrt{n}$).
Every divisor pair $(d, n/d)$ has its smaller member at most $\sqrt{n}$, and finding it gives the partner $n/d$. For $n = 18$ only $1, 2, 3, 4$ need checking; for $n = 375$ only $19$ numbers instead of $375$.
(ii) If we list out the divisors in pairs, will our gcd algorithm still work in the manner we have described?
Solution. The set of divisors found is exactly the same, so comparing two lists and collecting common divisors is still correct. What changes is the order of the list, and the chapter’s algorithm relied on it.
Written in pairs, the divisors of $12$ come out in the order the pairs are found:
$$[\,1, 12, 2, 6, 3, 4\,]$$
This list is not in increasing order: small and large divisors alternate. But the book’s Step 5 says “report the rightmost element of the list common-divisors as gcd$(m, n)$”, and the chapter justified it by “since each list of divisors is in increasing order, the list of common divisors is also in increasing order”.
Test it on gcd$(12, 24)$, whose answer is $12$. The pair-ordered list for $24$ is $[1, 24, 2, 12, 3, 8, 4, 6]$, and every divisor of $12$ occurs in it. So the list of common divisors, in the order of $12$’s list, is $[1, 12, 2, 6, 3, 4]$, whose rightmost element is $4$. The rightmost element is not the gcd, which is $12$.
So the algorithm does not work in the manner described. It can be repaired in either of two ways:
- Restore the order. List the small members $j$ in the order they are found, and the large members $n/j$ in the reverse of the order they are found, so that the final list is increasing. Then Step 5 is correct again.
- Change Step 5 to “report the largest element of the list common-divisors”. This works for any order, at the cost of a comparison across the whole list.
Not as described. The list of divisors found is the same, but when it is written pair by pair it is no longer in increasing order, for example $[1, 12, 2, 6, 3, 4]$ for $12$, and the “rightmost element” rule then fails: for gcd$(12, 24)$ it gives $4$, not $12$.
The fix is to put the list back in increasing order (large members in reverse), or to report the largest common divisor instead of the rightmost.
Common mistakes
- Question 1, giving only the plain method and never running it. The chapter’s habit is to execute an algorithm on a small case; an algorithm that is not tested may not work for the lists the question has in mind.
- Question 1(ii), writing a different algorithm from scratch. Part (ii) is part (i) with the lists swapped. Keeping the structure the same is the point.
- Question 2, testing numbers that are not multiples. Scanning every number $1, 2, 3, \dots$ and checking both divisibilities works but is wasteful; only multiples of one of the numbers can be the lcm.
- Question 2, reporting the product $m \times n$. The product is a common multiple, not usually the least: for $12$ and $18$ it is $216$, six times the true answer.
- Question 3(i), saying “about half of $n$”. Half of $n$ is the largest proper divisor, not the number of places to look. The scan stops at $\sqrt{n}$.
- Question 3(i), double-counting the square root. For $n = 36$ the pair $(6, 6)$ is one divisor, not two.
- Question 3(ii), saying “yes” because the same divisors are found. The divisors are the same, but the step that reads off the answer depended on their order.
Practise next
- Exercise Set 11.3, Question 2 — asks what happens when the last gcd algorithm of the chapter scans from $\min(m, n)$ downwards, where the order of the scan is again what matters.
- The End-of-Chapter Exercises, Question 3 — uses the list of divisors to decide whether a number is prime, building directly on the divisor algorithm of this section.
Shiwam's Classes: CBSE 10CBSE Class 10 Maths and Science, every chapter
Subscribe
Doubts are answered by Shiwam, usually within a day. Ask about this question specifically — a general question about the chapter is better asked in class.