NCERT Solutions Class 9 Maths Exercise 11.3 — The World of Algorithms
NCERT Class 9 Mathematics — Ganita Manjari Part II, The World of Algorithms, Exercise Set 11.3. All 2 questions solved.
Section 11.3.1 takes the first gcd algorithm and improves it in three steps: scan both numbers together, check only up to $\min(m, n)$, and finally throw the lists away and keep a single running value, most-recent-common-divisor, updated each time a larger common divisor turns up. This exercise asks what happens if each scan is run backwards.
Key insight. An algorithm that works through candidates in a given order produces its output in that order, and the order also decides when you may stop. Going upwards, the largest common divisor is the last one met, so every candidate must be examined. Going downwards, the largest is the first one met, so the search can end the moment it is found. Same answer, very different amount of work.
Question 1
How would our original algorithm change if we computed the divisors of $n$ by examining the numbers from $1$ to $n$ in reverse order, from $n$ down to $1$?
Solution. The original algorithm is:
- Start with an empty list-of-divisors.
- For each number $j$ in the sequence $1, 2, 3, \dots, n$: if $j$ divides $n$, add $j$ to the list-of-divisors.
Reversing means only that Step 2 reads “for each number $j$ in the sequence $n, n-1, n-2, \dots, 1$”. The test, “does $j$ divide $n$?”, and the action, “add $j$ at the end of the list”, are untouched. So the algorithm changes by one line.
What changes is the output. The same divisors are found, since each of $1, \dots, n$ is still examined exactly once, but they are added in the opposite order, so the list now comes out in decreasing order. For $18$:
- Start: $[\;]$.
- $18$ divides $18$: $[18]$. $17, 16, 15, 14, 13, 12, 11, 10$ do not divide $18$.
- $9$ divides $18$: $[18, 9]$. $8$ and $7$ do not.
- $6$ divides $18$: $[18, 9, 6]$. $5$ and $4$ do not.
- $3$ divides $18$: $[18, 9, 6, 3]$.
- $2$ divides $18$: $[18, 9, 6, 3, 2]$.
- $1$ divides $18$: $[18, 9, 6, 3, 2, 1]$.
Compare the chapter’s list $[1, 2, 3, 6, 9, 18]$: it is the same list read backwards. The same holds for $15$, which gives $[15, 5, 3, 1]$, and $135$, which gives $[135, 45, 27, 15, 9, 5, 3, 1]$.
Two things follow:
- The amount of work is the same. Each of the $n$ numbers is tested once in either direction. Reversing the scan does not make divisors-finding cheaper.
- Anything that relied on the order must be adjusted. The gcd algorithm of Section 11.2.3 used “the largest common divisor is the rightmost element of the list”. With a decreasing list, the largest common divisor is the leftmost element, so its last step would read “report the first element of the list common-divisors”.
Only the order of the scan changes: for each $j$ in $n, n-1, \dots, 1$, if $j$ divides $n$, add $j$ to the list. The same divisors are found with the same number of tests, but the list comes out in decreasing order, for example $[18, 9, 6, 3, 2, 1]$.
So the gcd algorithm would report the first, not the last, element of the list of common divisors.
Question 2
What about the last algorithm described above? What happens when we look at common divisors starting from $\min(m, n)$ and work backwards to $1$?
Solution. The last algorithm keeps one number, most-recent-common-divisor, starts it at $1$, and for each $k$ from $2$ to $\min(m, n)$ updates it to $k$ whenever $k$ divides both $m$ and $n$. Because $k$ is increasing, each update replaces a smaller common divisor by a larger one, and when the scan ends the value held is the largest, the gcd. That is why it cannot stop early: until $k$ reaches $\min(m, n)$ a larger common divisor may still appear.
Now run the scan from $\min(m, n)$ downwards. The first common divisor we meet is, by the order of the scan, larger than every common divisor still to come. So it is already the greatest, and we may stop at once, with no “most recent” value to keep:
The last algorithm, run backwards.
- For each $j$ in the sequence $\min(m, n), \min(m, n) – 1, \dots, 2, 1$: – If $j$ divides both $m$ and $n$, report $j$ as gcd$(m, n)$ and stop.
It always stops, since $1$ divides everything, so the scan cannot run past $j = 1$.
What it gains. The book’s forward scan always tests every $k$ from $2$ to $\min(m, n)$, that is $\min(m, n) – 1$ values. The backward scan ends at the gcd, so the number of values it examines is $\min(m, n) – \text{gcd}(m, n) + 1$. That is far fewer when the gcd is large, and at worst one more than the forward scan:
| Pair | $\min(m,n)$ | gcd | Forward scan | Backward scan |
|---|---|---|---|---|
| $(6, 12)$ | $6$ | $6$ | $5$ | $1$ |
| $(375, 825)$ | $375$ | $75$ | $374$ | $301$ |
| $(54000, 81000)$ | $54000$ | $27000$ | $53999$ | $27001$ |
| $(99, 2)$ | $2$ | $1$ | $1$ | $2$ |
For $(6, 12)$ the book’s example scans $k = 2, 3, 4, 5, 6$ and updates three times (to $2$, $3$ and $6$); the backward scan tests $j = 6$, finds that $6$ divides both, and stops after one check.
What it does not gain. The worst case is the same. If the gcd is $1$, as for $(99, 2)$ or for two numbers with no common factor, the backward scan has to travel all the way down to $1$, which is $\min(m, n)$ checks, about the forward scan’s cost. The chapter’s complaint, that the effort grows with the value of the smaller number rather than the number of its digits, is therefore unchanged: going backwards helps only when the gcd is large.
The scan finds the gcd as soon as it reaches the first $j$ that divides both $m$ and $n$, because the order of the scan makes that $j$ the largest common divisor. So we stop there, and the running value most-recent-common-divisor is not needed at all.
For $(6, 12)$ it needs $1$ check instead of $5$; for $(375, 825)$ it needs $301$ checks instead of $374$. But when the gcd is $1$ it still examines all $\min(m, n)$ numbers, so the worst case is no better.
Animation of this question’s idea. Use the controls to change it. Open full size
Common mistakes
- Question 1, thinking reversing the scan finds fewer divisors or does less work. The same $n$ numbers are tested, so the same divisors are found with the same effort. Only the order of the output flips.
- Question 1, forgetting that the output order changed. Any later step that reads “the rightmost element” now needs to read “the leftmost”, or it quietly returns $1$.
- Question 2, stopping at the first common divisor in the forward direction. Going upwards, the first common divisor met is $1$, which is almost never the gcd. Stopping early is valid only in the downward scan.
- Question 2, claiming the backward scan is “always much faster”. It is faster only when the gcd is large. For coprime numbers it takes just as long as the forward scan.
- Question 2, starting the scan at $\max(m, n)$. A common divisor cannot exceed the smaller number, so the scan should begin at $\min(m, n)$.
Practise next
- Exercise Set 11.2, Question 3 — also asks how the order of a list of divisors affects the gcd algorithm, there for divisors written in pairs.
- The End-of-Chapter Exercises, Question 1 — computes four gcds with Euclid’s improved algorithm, which needs a handful of steps where the scans of this section need thousands.
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.