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

Faster, still right

Refinements preserve correctness

Stuck? Ask Guru

NCERT: 11.3.2 Analysing these Algorithms

Think

Is it still correct?

We have improved the gcd algorithm three times: one combined scan, scanning only to min(m, n), and no lists. Each version does less work.

Do we need a fresh proof that the last version gives the gcd?

What this lesson covers

The idea

If a refinement improves efficiency without changing what is computed, the improved algorithm is still correct.

Is it still correct?

We have improved the gcd algorithm three times: one combined scan, scanning only to min(m, n), and no lists. Each version does less work.

Do we need a fresh proof that the last version gives the gcd?

  • No: each change saves work but computes the same thing
  • Yes, each version needs a completely new proof
  • No, because it is the fastest

Same answer?

The first algorithm, built straight from the definition, gives gcd(375, 825) = 75. Run the final version (no lists, scan to min(m, n)) and compare.

Same result, less work

If a refinement improves efficiency without changing what is computed, the improved algorithm is still correct.

The first algorithm comes directly from the definition of gcd, so it is easy to justify. Each refinement only changes how the work is done: • one scan instead of two: the same divisors are found; • stopping at min(m, n): no common divisor is skipped; • keeping only the latest: the last common divisor is the same. So every version computes the gcd: 75 for 375 and 825.

Notes

A refinement that only makes the work faster, without changing what is computed, keeps the algorithm correct.

Check yourself

Why is stopping the scan at min(m, n) still correct?

Which change would NOT preserve correctness?

  • Every common divisor is at most min(m, n), so none is missed — correct. Yes. The result does not change.
  • Because it is faster. Being faster does not make it correct.
  • Because the gcd always equals min(m, n). gcd(375, 825) = 75, not 375.
  • Scanning only up to 10 — correct. Right. A common divisor larger than 10 (like 75) would be missed.
  • Scanning both numbers in one pass. This finds the same divisors.
  • Keeping only the most recent common divisor. The last common divisor found is still the gcd.