retagged by
26,653 views
64 votes
64 votes

In the following C function, let $n \geq m$.

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

How many recursive calls are made by this function?

  1. $\Theta(\log_2n)$

  2. $\Omega(n)$

  3. $\Theta(\log_2\log_2n)$

  4. $\Theta(\sqrt{n})$

retagged by

8 Answers

1 votes
1 votes
Euclidian GCD worst case is when we have Fiboonacci pairs involved

As Arjun said, we must know this before.

Now think about the best case and average case time complexity of this algorithm

Option B : logn
0 votes
0 votes
If we consider worst case then it will be option a ... as an example take n=11 m=8 .. it will be Ceil(Logn) ..
0 votes
0 votes
  • If you stare at the algorithm for sometime, you will see that the remainder is 'cut' into half in every 2 steps.
  • And since it cannot go less than 1, there can be atmost 2.[log2n]2.[log2n] steps/recursions.
  • Each step/recursion requires constant time, θ(1)θ(1)
  • so this can be atmost 2.[log2n].θ(1)2.[log2n].θ(1) time
  • and that's θ(logθ(log n)
0 votes
0 votes
Euclidean algorithm - Codility

Go through it
Answer:

Related questions

26 votes
26 votes
1 answer
1
Kathleen asked Sep 21, 2014
10,388 views
Consider the following C function:int f(int n) { static int r = 0; if (n <= 0) return 1; if (n 3) { r = n; return f(n-2) + 2; } return f(n-1) + r; }What is the value of ...
64 votes
64 votes
15 answers
2
Arjun asked Jul 6, 2016
37,160 views
Consider the following segment of C-code:int j, n; j = 1; while (j <= n) j = j * 2;The number of comparisons made in the execution of the loop for any $n 0$ is:$\lceil \...
50 votes
50 votes
8 answers
4
Kathleen asked Sep 21, 2014
32,011 views
What is the $\text{time complexity}$ of the following recursive function?int DoSomething (int n) { if (n <= 2) return 1; else return (DoSomething (floor (sqrt(n))) + n); ...