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

Do it again

Repeated steps

Stuck? Ask Guru

NCERT: Chapter Summary

Think

Written once, done many times

The divisor algorithm says: "For each number j in 1, 2, 3, …, n: if j divides n, add j to the list-of-divisors." It is written once but carried out many times.

Finding the divisors of 775 (a Think and Reflect number), how many times is this step carried out?

What this lesson covers

The idea

A step in an algorithm may be executed a number of times, once for each value in a range.

Written once, done many times

The divisor algorithm says: "For each number j in 1, 2, 3, …, n: if j divides n, add j to the list-of-divisors." It is written once but carried out many times.

Finding the divisors of 775 (a Think and Reflect number), how many times is this step carried out?

  • 775 times
  • Once
  • 6 times

Count the repeats

Run the divisor algorithm for 775 and watch the counter: the same step, once for each j from 1 to 775.

Repeated steps

A step in an algorithm may be executed a number of times, once for each value in a range.

"For each j in 1, 2, …, n, check if j is a divisor of n" is carried out n times: 775 times for n = 775. It finds divisors(775) = [1, 5, 25, 31, 155, 775]. Other repeated steps in this chapter: "Repeat Step 3 until there are no more digits on the left" (addition), and Euclid's reduction, repeated until n = 0.

Notes

A step can be repeated, once for each value in a range, such as "for each j in 1, 2, …, n". It is written once but carried out many times.

Check yourself

The addition algorithm adds 3347 + 2729. How many times is the column step carried out in all?

Answer: 4

There are 4 columns, so the column step is done 4 times.

Which of these is a repeated step?

  • For each x in divisors-of-m, check if x is in divisors-of-n — correct. Yes: once for each x in the list.
  • Start with an empty list. That is done once.
  • Report the rightmost element as gcd(m, n). That is done once, at the end.