‹ Class 9 · Ch 11
The World of Algorithms · Principle 15 of 29

Stop at the smaller number

Scan only up to the smaller number

Stuck? Ask Guru

NCERT: 11.3.1 Improving the Algorithm — Directly compute the list of common divisors

Think

How far do we need to go?

We only want the common divisors of 375 and 825. Could a number bigger than 375 divide both 375 and 825?

Could a common divisor of 375 and 825 be larger than 375?

What this lesson covers

The idea

A common divisor cannot exceed the smaller number, so it suffices to check j from 1 to min(m, n) and add those dividing both.

How far do we need to go?

We only want the common divisors of 375 and 825. Could a number bigger than 375 divide both 375 and 825?

Could a common divisor of 375 and 825 be larger than 375?

  • No: it must divide 375, so it is at most 375
  • Yes, up to 825
  • Only if it is 825

Scan to min(m, n)

Build the common divisors directly: for each j from 1 to min(375, 825) = 375, if j divides both numbers, add j to the list.

min, not max

A common divisor cannot be larger than the smaller number, so it is enough to check j from 1 to min(m, n) and add those dividing both.

For each j from 1 to min(m, n): if j divides both m and n, add j to the list of common divisors. For 375 and 825 this needs 375 checks (not 825) and gives [1, 3, 5, 15, 25, 75], so gcd = 75.

Notes

Check only j = 1 to min(m, n): if j divides both, it is a common divisor. No common divisor is larger than the smaller number.

Check yourself

Why can we stop at min(m, n)?

How many checks does this scan make for gcd(54000, 81000)?

Answer: 54000

min(54000, 81000) = 54000 checks.

  • A common divisor divides the smaller number, so it cannot be bigger than it — correct. Yes.
  • Because the gcd is always the smaller number. Not true: gcd(375, 825) = 75, not 375.
  • To save time, even if we might miss one. No common divisor is missed.