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

Count the steps

Analysing efficiency

Stuck? Ask Guru

NCERT: Chapter Summary

Think

Same answer, different work

This chapter has many algorithms that all find gcd(375, 825) = 75: two separate scans, one combined scan, a scan to the smaller number, most-recent-common-divisor, and (coming up next) Euclid's subtraction algorithm and Āryabhaṭa's division algorithm.

They all give 75. How do we decide which is best?

What this lesson covers

The idea

We can count how many steps an algorithm takes in terms of the size of its input, and use this to decide how efficient it is.

Same answer, different work

This chapter has many algorithms that all find gcd(375, 825) = 75: two separate scans, one combined scan, a scan to the smaller number, most-recent-common-divisor, and (coming up next) Euclid's subtraction algorithm and Āryabhaṭa's division algorithm.

They all give 75. How do we decide which is best?

  • Count how many steps each takes, in terms of the size of its input
  • They are all equally good
  • Pick the one with the shortest description

Count the reductions

A preview of the next lessons: run Euclid's subtraction algorithm and Āryabhaṭa's division algorithm side by side on gcd(375, 825), and count the reductions.

Measure the work

We can count how many steps an algorithm takes, in terms of the size of its input, and use this to decide how efficient it is.

Steps for gcd(375, 825): • two separate scans: 375 + 825 = 1200 checks • one combined scan: max(375, 825) = 825 checks • scan to the smaller number: min(375, 825) = 375 checks • most-recent-common-divisor (k = 2 … 375): 374 checks • Euclid's subtraction algorithm: 7 reductions • Āryabhaṭa's division algorithm: 2 reductions The scans grow with the value of the numbers; the division algorithm grows with the number of digits.

Notes

Analyse an algorithm by counting its steps in terms of the size of its input. For gcd: scans grow with the value; Āryabhaṭa's division algorithm grows with the number of digits.

Check yourself

For gcd(54000, 81000), how many checks does the scan to the smaller number make?

Answer: 54000

min(54000, 81000) = 54000 checks. The division algorithm needs just 2 reductions.

Euclid's subtraction algorithm needs 7 reductions for gcd(375, 825) but about 50 for gcd(99, 2). What does this show?

Which gcd algorithm in this chapter takes steps that grow with the number of digits?

  • Its steps can grow with the value of the numbers, not their digits — correct. Yes. 99 and 2 are small, but subtracting 2 each time takes many steps.
  • It is always faster for smaller numbers. gcd(99, 2) has smaller numbers but needs more steps.
  • It gives the wrong answer for 99 and 2. It gives the right answer, 1; it is just slow.
  • Āryabhaṭa's division algorithm — correct. Yes, as the book says.
  • Scanning up to min(m, n). Its work grows with the value: 10 times for each extra digit.
  • Two separate scans. That is the slowest: m + n checks.