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?
Step-by-step solution
Idea: Scanning downwards meets the common divisors from largest to smallest, so the first one met is the greatest. Stopping there usually saves many checks.
- The last algorithm scans k = 2, 3, …, min(m, n) and keeps the most recent common divisor; going upwards, the most recent one is the largest.
- Going backwards, k = min(m, n), min − 1, …, 1 meets the common divisors from largest to smallest. So the first k that divides both m and n is the gcd.1 mark
- If we kept the old rule (“update most-recent-common-divisor whenever k divides both”) and scanned all the way down, the last update would be at k = 1, since 1 divides every number. The algorithm would report 1, which is wrong. So the rule must change to: stop at the first common divisor and report it.1 mark
- New algorithm: for each k from min(m, n) down to 1: if k divides both m and n, report k as gcd(m, n) and stop. (It always stops, at k = 1 at the latest.)½ mark
- Effect on the work: gcd(6, 12) is found at once (k = 6, one check). gcd(375, 825) is found at k = 75 after 301 checks, instead of 374 checks going up. In the worst case, when the gcd is 1, we still check every number, so the work is still proportional to the size of the smaller number.½ mark
Answer to write in the exam
Scanning down from min(m, n), common divisors are met from largest to smallest.
First k dividing both m and n = gcd(m, n).
Old update rule kept → last update at k = 1 → answer 1 ✗
New rule: for k = min(m, n) down to 1, if k divides both m and n, report k and stop.
e.g. gcd(6, 12): k = 6 divides both → 6 (1 check)
∴ We stop at the first common divisor found; fewer checks unless the gcd is small.
Common mistakes that cost marks
- Keeping the update rule unchanged and scanning to 1, which always ends with the answer 1.
- Starting the backward scan at max(m, n) instead of min(m, n).
- Claiming the backward scan is always faster. When the gcd is 1 it still checks every number.
How this can come in the exam
Scanning backwards from min(m, n) and stopping at the first common divisor, how many values of k are checked to find gcd(40, 64)?
- 8
- 32
- 33
- 40
Show answer
(C) 33
k = 40, 39, …, 8: the first common divisor is 8. That is 40 − 8 + 1 = 33 checks.
Try one yourself
Use the backward scan to find gcd(27, 45). How many values of k are checked?
Show answer
k = 27, 26, …: none divides both until k = 9 (27 = 3 × 9, 45 = 5 × 9). gcd = 9, after 27 − 9 + 1 = 19 checks.
More questions like this
- Let us assume that m ≥ n. Then, gcd(m, n) is the same as gcd(n, m − n). Why is this the case?
- Euclid’s algorithm for gcd(m, n): 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 compute gcd(n, m − n). Let us apply this algorithm to gcd(375, 825), the problem that we saw before.
- Are we close to our original goal of finding an algorithm where the effort is proportional to the number of digits?
- Consider the earlier example gcd(99, 2) that took a long time to converge. Here is what happens when we execute the improved algorithm.
- In other words, we needed only two reduction steps to get to the final answer. Check that this would happen for any similar problem of the form gcd(2k + 1, 2).