Algorithms | Analysis of Algorithms | Question 14

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

filter_none

edit
close

play_arrow

link
brightness_4
code

int gcd(n,m)
{
  if (n%m ==0) return m;  
  n = n%m;
  return gcd(m, n);
}

chevron_right


How many recursive calls are made by this function?
(A) \theta(logn)
(B) \Omega(n)
(C) \theta(loglogn)
(D) \theta(sqrt(n))
(A) A
(B) B
(C) C
(D) D


Answer: (A)

Explanation: Above code is implementation of the Euclidean algorithm for finding Greatest Common Divisor (GCD).
Please see http://mathworld.wolfram.com/EuclideanAlgorithm.html for time complexity.

Quiz of this Question



My Personal Notes arrow_drop_up