Learnify Academy is a tuition centre in Bahrain. Classes are for students in Bahrain only.Tuition classes in Bahrain only

Algorithms · 3 marks

How would our original algorithm change if we computed the divisors of n by examining the numbers from 1 to n in reverse order, from n down to 1?

Answer: The divisor lists come out in decreasing order, so the list of common divisors is also decreasing. The gcd is then the leftmost (first) element, so Step 5 must say “report the leftmost element”. (We could even stop at the first common divisor found.)

Step-by-step solution

Idea: The original gcd algorithm reports the rightmost common divisor because the lists were in increasing order. Reversing the scan reverses the order of the lists, so the greatest common divisor moves to the other end.

  1. Scanning j = n, n − 1, …, 1 finds the divisors from largest to smallest. For example divisors(18) becomes [18, 9, 6, 3, 2, 1], a decreasing list.½ mark
  2. The gcd algorithm uses this same divisors procedure for both numbers, so divisors-of-m and divisors-of-n are both decreasing. Step 4 goes through divisors-of-m in order, so common-divisors is also built in decreasing order.½ mark
  3. Now the greatest common divisor is the leftmost (first) element. Step 5 must change to: “Report the leftmost element of common-divisors as gcd(m, n)”. If Step 5 is left unchanged, it reports the rightmost element, which is always 1: wrong unless the gcd really is 1.1 mark
  4. Example gcd(375, 825): divisors-of-m = [375, 125, 75, 25, 15, 5, 3, 1]; checking each in divisors(825) gives common-divisors = [75, 25, 15, 5, 3, 1]. Leftmost = 75 ✓.½ mark
  5. Bonus: the first common divisor found is already the gcd, so we may stop there and skip the rest of the list.½ mark
  6. Note: if only the list for the second number were reversed, nothing would change, because common-divisors follows the order of divisors-of-m and the membership check does not depend on order.
The lists become decreasing, so common-divisors is decreasing and the gcd is its leftmost element: Step 5 must report the leftmost (first) element instead of the rightmost, and the search can stop at the first common divisor.

Answer to write in the exam

Reverse scan → divisors in decreasing order, e.g. divisors(18) = [18, 9, 6, 3, 2, 1]

Both lists decreasing → common-divisors decreasing

Greatest common divisor = leftmost element (rightmost would be 1)

Step 5 becomes: report the leftmost element of common-divisors as gcd(m, n)

e.g. gcd(375, 825): common-divisors = [75, 25, 15, 5, 3, 1] → 75

∴ Report the leftmost element; we can stop at the first common divisor found.

Common mistakes that cost marks

  • Saying “nothing changes” while still reporting the rightmost element, which would now always be 1.
  • Thinking the set of divisors changes. Only their order changes; the same divisors are found.
  • Forgetting that the algorithm can now stop early, which is the real gain from scanning in reverse.

How this can come in the exam

Assertion–Reason (1 mark)

Assertion (A): If divisors are listed from n down to 1, the gcd is the first element of the list of common divisors.
Reason (R): The list of common divisors is then in decreasing order.

  1. Both A and R are true, and R is the correct explanation of A.
  2. Both A and R are true, but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.
Show answer

(A) Both A and R are true, and R is the correct explanation of A.
In a decreasing list the first element is the largest, so R explains A.

Try one yourself

With divisors listed in decreasing order, find gcd(20, 30) by the original algorithm.

Show answer

divisors(20) = [20, 10, 5, 4, 2, 1]; divisors(30) = [30, 15, 10, 6, 5, 3, 2, 1]. Common divisors = [10, 5, 2, 1]. Leftmost: gcd(20, 30) = 10.

More questions like this

All Algorithms questions · All maths questions