Algorithms

NCERT Solutions Class 9 Maths Chapter 11 End-of-Chapter Exercises — The World of Algorithms

NCERT Class 9 Mathematics — Ganita Manjari Part II, The World of Algorithms, End-of-Chapter Exercises. All 5 questions solved.

These five questions use Euclid’s algorithm in its improved, Āryabhaṭa form, and then ask you to write algorithms of your own for primality and prime factorisation. The improved algorithm for gcd$(m, n)$ is:

  1. If $m < n$, reverse the numbers and compute gcd$(n, m)$.
  2. If $n = 0$, report the answer as $m$.
  3. Otherwise, reduce the problem to computing gcd$(n, m \bmod n)$.

Here $m \bmod n$ is the remainder on dividing $m$ by $n$.

Key insight. Euclid’s algorithm works because common divisors never change when the larger number is replaced by its remainder (Question 2 proves it). That is why the numbers shrink fast and the gcd is left untouched, and why four gcds that would need thousands of trial divisions take only a handful of divisions here. For the algorithms you write yourself in Questions 3 to 5, the best strategy is the chapter’s: build each new algorithm out of one you already have.

Question 1

Compute the following using the improved version of Euclid’s algorithm.

Each line below applies Step 3 once. A line that reverses the two numbers, because $m < n$, is shown as such.

(i) gcd$(375, 825)$

$375 < 825$, so reverse to gcd$(825, 375)$. Then $825 \bmod 375 = 75$, since $825 = 2 \times 375 + 75$, giving gcd$(375, 75)$. Next $375 = 5 \times 75 + 0$, so $375 \bmod 75 = 0$ and we reach gcd$(75, 0)$. By Step 2 the answer is $75$.

This agrees with the chapter, which found gcd$(375, 825) = 75$ from the lists of divisors, and shows the saving: three reductions instead of hundreds of tests.

$$\gcd(375, 825) = 75$$

(ii) gcd$(51000, 81000)$

$51000 < 81000$, so reverse to gcd$(81000, 51000)$. Then:

$$\begin{aligned} 81000 &= 1 \times 51000 + 30000 \\ 51000 &= 1 \times 30000 + 21000 \\ 30000 &= 1 \times 21000 + 9000 \\ 21000 &= 2 \times 9000 + 3000 \\ 9000 &= 3 \times 3000 + 0 \end{aligned}$$

The chain of problems is gcd$(81000, 51000)$, gcd$(51000, 30000)$, gcd$(30000, 21000)$, gcd$(21000, 9000)$, gcd$(9000, 3000)$ and gcd$(3000, 0)$. The remainder first reaches $0$ when dividing by $3000$, so the answer is $3000$.

As a check, $51000 = 2^3 \times 3 \times 5^3 \times 17$ and $81000 = 2^3 \times 3^4 \times 5^3$. The prime factors they share, each to the smaller power, are $2^3 \times 3 \times 5^3 = 3000$.

$$\gcd(51000, 81000) = 3000$$

(iii) gcd$(1789287, 237656)$

Here $m > n$ already, so no reversal is needed at the start. Dividing repeatedly:

$$\begin{aligned} 1789287 &= 7 \times 237656 + 125695 \\ 237656 &= 1 \times 125695 + 111961 \\ 125695 &= 1 \times 111961 + 13734 \\ 111961 &= 8 \times 13734 + 2089 \\ 13734 &= 6 \times 2089 + 1200 \\ 2089 &= 1 \times 1200 + 889 \\ 1200 &= 1 \times 889 + 311 \\ 889 &= 2 \times 311 + 267 \\ 311 &= 1 \times 267 + 44 \\ 267 &= 6 \times 44 + 3 \\ 44 &= 14 \times 3 + 2 \\ 3 &= 1 \times 2 + 1 \\ 2 &= 2 \times 1 + 0 \end{aligned}$$

That is thirteen divisions to reach gcd$(1, 0)$, so the answer is $1$: the two numbers have no common divisor other than $1$ (they are coprime). A prime factorisation agrees: $1789287 = 3 \times 19 \times 31391$ and $237656 = 2^3 \times 61 \times 487$ share no prime.

$$\gcd(1789287, 237656) = 1$$

(iv) gcd$(2587392, 157656)$

Again $m > n$. Dividing repeatedly:

$$\begin{aligned} 2587392 &= 16 \times 157656 + 64896 \\ 157656 &= 2 \times 64896 + 27864 \\ 64896 &= 2 \times 27864 + 9168 \\ 27864 &= 3 \times 9168 + 360 \\ 9168 &= 25 \times 360 + 168 \\ 360 &= 2 \times 168 + 24 \\ 168 &= 7 \times 24 + 0 \end{aligned}$$

The remainder first reaches $0$ when dividing by $24$, so the answer is $24$.

Check by prime factorisation: $2587392 = 2^8 \times 3^2 \times 1123$ and $157656 = 2^3 \times 3 \times 6569$, whose common part is $2^3 \times 3 = 24$.

$$\gcd(2587392, 157656) = 24$$

Animation of this question’s idea. Use the controls to change it. Open full size

Question 2

Assume that $m \geq n$. Verify that $d$ divides $m$ and $n$ if and only if $d$ divides both $n$ and $m \bmod n$.

Solution. This is the fact that justifies the improved algorithm. Divide $m$ by $n$ and write

$$m = qn + r, \qquad r = m \bmod n,$$

where $q$ is a natural number (because $m \geq n$) and $0 \leq r < n$. “If and only if” needs both directions.

($\Rightarrow$) Suppose $d$ divides $m$ and $n$. Write $m = ad$ and $n = bd$ for some whole numbers $a$ and $b$. Then

$$r = m – qn = ad – qbd = (a – qb)\,d,$$

so $d$ divides $r$. Thus $d$ divides both $n$ and $m \bmod n$.

($\Leftarrow$) Suppose $d$ divides $n$ and $r = m \bmod n$. Write $n = xd$ and $r = yd$. Then

$$m = qn + r = qxd + yd = (qx + y)\,d,$$

so $d$ divides $m$. Thus $d$ divides both $m$ and $n$.

If $r = 0$ both arguments still hold, since every $d$ divides $0$ and the statement then says that the divisors of $m$ and $n$ are just the divisors of $n$, which is right because $n$ divides $m$.

What this gives. Both directions together say that the pair $(m, n)$ and the pair $(n, m \bmod n)$ have exactly the same common divisors. In particular they have the same greatest one:

$$\gcd(m, n) = \gcd(n, m \bmod n).$$

This is the same argument the chapter made for $m – n$, with $q$ copies of $n$ subtracted at once instead of one.

Write $m = qn + r$ with $r = m \bmod n$.

If $d \mid m$ and $d \mid n$, then $r = m – qn$ is divisible by $d$. Conversely, if $d \mid n$ and $d \mid r$, then $m = qn + r$ is divisible by $d$.

So $(m, n)$ and $(n, m \bmod n)$ have the same common divisors, and hence $\gcd(m, n) = \gcd(n, m \bmod n)$.

Question 3

Write an algorithm prime$(n)$ to check if $n$ is prime.

(Hint: A prime number $p$ has exactly two distinct factors, $1$ and $p$. Can you make use of divisors$(n)$ to write out prime$(n)$?)

Solution. Follow the hint. A prime has exactly two divisors, so $n$ is prime precisely when the list divisors$(n)$ has exactly two elements. All we need is to compute the list with the chapter’s algorithm and count it.

Algorithm prime$(n)$.

  1. Compute divisors$(n)$, the list of divisors of $n$, using the earlier algorithm.
  2. Count the elements in the list.
  3. If the list has exactly two elements, report “$n$ is prime”. Otherwise, report “$n$ is not prime”.

Executing it.

  • $n = 13$: divisors$(13) = [1, 13]$, two elements, so $13$ is prime.
  • $n = 18$: divisors$(18) = [1, 2, 3, 6, 9, 18]$, six elements, so $18$ is not prime.
  • $n = 1$: divisors$(1) = [1]$, one element, so $1$ is not prime. This is right, because $1$ has only one divisor, and the “exactly two” rule handles it without a special case.
  • $n = 2$: divisors$(2) = [1, 2]$, so $2$ is prime.

Why it is correct. Every $n > 1$ has at least the two divisors $1$ and $n$. If there is no third divisor, there are exactly two and $n$ is prime; any further divisor makes the list longer, and $n$ is composite.

A shortcut. The test only needs to know whether a third divisor exists, so Step 1 could stop as soon as it finds any divisor of $n$ other than $1$ and $n$. By Question 3 of Exercise Set 11.2 it is also enough to look only at numbers up to $\sqrt{n}$.

prime$(n)$: compute divisors$(n)$; if the list has exactly two elements ($1$ and $n$), report “prime”, otherwise report “not prime”.

Check: divisors$(13) = [1, 13]$ so $13$ is prime; divisors$(18)$ has six elements so $18$ is not; divisors$(1) = [1]$ so $1$ is not prime.

Question 4

This question carries an asterisk in the book: it is enrichment, and is not assessed.

Write an algorithm primedivisors$(n)$ to compute the list of divisors of $n$ that are prime numbers.

(Hint: Compute divisors$(n)$ and then filter out the primes in this list.)

Solution. Follow the hint, and use Question 3 for the “filter” step. We need to run through the divisors of $n$ and keep those for which prime$(\,\cdot\,)$ says “prime”.

Algorithm primedivisors$(n)$.

  1. Compute divisors$(n)$.
  2. Start with an empty list prime-divisors.
  3. For each number $d$ in divisors$(n)$: – If prime$(d)$ reports “prime”, add $d$ to prime-divisors.
  4. Report prime-divisors.

Because divisors$(n)$ is in increasing order, prime-divisors comes out in increasing order too.

Executing it for $n = 180$. divisors$(180) = [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]$. Testing each: $1$ is not prime, $2$ is prime, $3$ is prime, $4$ ($= 2 \times 2$) is not, $5$ is prime, and every other entry is divisible by $2$, $3$ or $5$ and is larger than it, so none is prime. The result is $[2, 3, 5]$.

Two more checks: for $n = 375$ the divisors are $[1, 3, 5, 15, 25, 75, 125, 375]$, giving $[3, 5]$; for $n = 825$ the answer is $[3, 5, 11]$. For $n = 1$ the list is empty, since $1$ is not prime.

primedivisors$(n)$: compute divisors$(n)$; for each $d$ in it, if prime$(d)$ reports “prime”, add $d$ to an initially empty list; report that list.

Check: primedivisors$(180) = [2, 3, 5]$, primedivisors$(375) = [3, 5]$ and primedivisors$(825) = [3, 5, 11]$.

Question 5

This question carries an asterisk in the book: it is enrichment, and is not assessed.

We can also find the gcd of two numbers by computing the prime factorisation of both the numbers. Try to write an algorithm to compute the prime factorisation of a number.

Writing the algorithm

Solution. The prime factorisation of $n$ is the list of primes dividing $n$, each with the number of times it divides. Question 4 already gives the primes, so the only new job is to count how many times each one divides $n$, by dividing it out repeatedly.

Algorithm factorise$(n)$.

  1. Compute primedivisors$(n)$, and set remaining to $n$. Start with an empty list factorisation.
  2. For each prime $p$ in primedivisors$(n)$, in increasing order: – Set the exponent $e$ to $0$. – While $p$ divides remaining: replace remaining by remaining $\div\, p$ and increase $e$ by $1$. – Add the pair $(p, e)$ to factorisation.
  3. Report factorisation.

At the end remaining is $1$, because each prime has been divided out completely.

Executing it for $n = 180$. primedivisors$(180) = [2, 3, 5]$ and remaining $= 180$.

  • $p = 2$: $180 \to 90 \to 45$, and $2$ does not divide $45$. So $e = 2$ and we add $(2, 2)$.
  • $p = 3$: $45 \to 15 \to 5$, and $3$ does not divide $5$. So $e = 2$ and we add $(3, 2)$.
  • $p = 5$: $5 \to 1$. So $e = 1$ and we add $(5, 1)$.

The result is $[(2, 2), (3, 2), (5, 1)]$, which is $2^2 \times 3^2 \times 5^1 = 4 \times 9 \times 5 = 180$, as it should be.

A shorter algorithm skips primedivisors altogether: try $j = 2, 3, 4, \dots$ in turn and divide each out as far as possible. A composite $j$ never divides, because its prime factors were divided out before $j$ was reached. Both give the same answer.

(i) The prime factorisation of $180$ is $2^2 \times 3^2 \times 5^1$. How would you represent this?

Solution. The expression has two parts for each prime: the prime and its exponent. So represent it as a list of pairs (prime, exponent), kept in increasing order of the prime:

$$[(2, 2),\ (3, 2),\ (5, 1)].$$

This keeps all the information, since the number can be rebuilt by multiplying $p^e$ over the pairs, and the increasing order is the same convenience that the lists of divisors had. An equally good choice is two matching lists, primes $[2, 3, 5]$ and exponents $[2, 2, 1]$.

As a list of (prime, exponent) pairs in increasing order of prime:

$$[(2, 2),\ (3, 2),\ (5, 1)]$$

(ii) How would you compare the prime factorisations of two numbers?

Solution. Walk down the two lists together, in the way Question 1 of Exercise Set 11.2 compared two sorted lists. For each prime, look at its exponent in each number, using $0$ if the prime does not appear.

  • Equal numbers. The numbers are equal exactly when the two lists are identical.
  • The gcd. For each prime that appears in both lists, keep the smaller exponent; the gcd is the product of $p^{\min(e, f)}$. A prime in only one list contributes nothing.
  • The lcm (a bonus). Keep the larger exponent for every prime that appears in either list.
  • Divisibility. $m$ divides $n$ exactly when every prime of $m$ is in $n$ with an exponent at least as large.

Example. $180 = 2^2 \times 3^2 \times 5^1$ and $150 = 2^1 \times 3^1 \times 5^2$. The smaller exponents are $1, 1, 1$ for $2, 3, 5$, so

$$\gcd(180, 150) = 2 \times 3 \times 5 = 30.$$

Another: $375 = 3^1 \times 5^3$ shares the primes $3$ and $5$ with $180$, with smaller exponents $1$ and $1$, so $\gcd(180, 375) = 3 \times 5 = 15$. Both agree with Euclid’s algorithm.

This method is clear but slow for large numbers, since factorising is far harder than taking remainders. Question 1(iii) is the example: Euclid finds gcd $1$ in thirteen divisions, while factorising $1789287$ by trial division means testing divisors up to its prime factor $31391$.

Compare the two lists prime by prime. The numbers are equal if the lists are identical.

For the gcd, multiply $p^{\min(e, f)}$ over the primes in both lists (the smaller exponent); for the lcm, take the larger exponent over all primes.

Check: $\gcd(180, 150) = 2 \times 3 \times 5 = 30$ and $\gcd(180, 375) = 15$.

Common mistakes

  • Question 1, forgetting to reverse when $m < n$. In parts (i) and (ii) the first number is the smaller. The algorithm’s Step 1 reverses it; dividing the smaller by the larger gives quotient $0$, remainder $m$, which does the reversal for you but wastes a line.
  • Question 1, reporting the last remainder instead of the last non-zero one. The answer is the divisor at the line where the remainder first becomes $0$, for instance $3000$ in part (ii), not $0$.
  • Question 1(iii), expecting a bigger answer because the numbers are big. Large numbers are often coprime. A gcd of $1$ is a perfectly good result.
  • Question 2, proving one direction only. “If and only if” is two statements. The forward direction is the subtraction $m – qn$; the backward direction is the addition $qn + r$.
  • Question 3, calling $1$ prime. divisors$(1) = [1]$ has one element, so the “exactly two” rule correctly rejects it.
  • Question 3, counting a repeated divisor twice. The list of divisors holds each divisor once. For $n = 36$ the divisor $6$ appears once, so the list has $9$ elements.
  • Question 5, dividing by composite numbers. Only primes go into the factorisation. If you divide $180$ by $4$ you will not find the exponent of $2$.
  • Question 5(ii), taking the larger exponent for the gcd. The gcd keeps what the two numbers have in common, so it takes the smaller exponent; the larger exponent belongs to the lcm.

Practise next

  • Exercise Set 11.3, Question 2 — scans downward for the gcd; compare how many steps it takes for gcd$(375, 825)$ with the three reductions in Question 1(i) above.
  • Exercise Set 11.2, Question 2 — the lcm algorithm, where the gcd identity $m \times n = \gcd(m, n) \times \text{lcm}(m, n)$ can be tested using the gcds of this exercise.
Keep track of what you have finished — create a free account.

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.

Ask your doubt

Stuck on this question? Ask Shiwam directly.

A free account lets you post a doubt on any question, keep track of the exercises you have finished, and come back to the answer later.

Create a free accountI already have one