Back to the 2019 paper

Module 1: Introduction and Complexity Analysis

20192m

In the following C++ function, let n>=mn >= m.

int gcd(int n, int m) {
  if (n % m == 0) return m;
  if (n < m) swap(n, m);
  while (m > 0) {
    n = n % m;
    swap(n, m);
  }
  return n;
}

What is the time complexity of the above function assuming n>mn > m?

  • (i) Θ(logn)\Theta(\log n)
  • (ii) Ω(n)\Omega(n)
  • (iii) Θ(loglogn)\Theta(\log \log n)
  • (iv) Θ(n)\Theta(\sqrt{n})

Similar questions