retagged by
2,199 views

1 Answer

Best answer
9 votes
9 votes

The key to understand this is to observe that "when $a$ is divided by $b$, remainder is always less than or equal to $a/2$". Why ? Because if remainder is more than $a/2$, and since divisor is always greater than remainder, then divisor is also more than $a/2$, and so sum of divisor and remainder becomes more than $a$, which can't be possible.

Now when we find $gcd(a,b)$, (suppose $a>b$, if not, swap $a$ and $b$), in the first step, $a$ is dividend and $b$ is divisor, we find some remainder $r_1$. Then in second step, $r_1$ becomes divisor and $b$ becomes dividend. Now again we divide $b$ by $r_1$ and get some remainder $r_2$, but due to above property, $r_2 \le b/2$.

So in two steps, remainder is at most $b/2$. We terminate the process once we reach remainder of $0$. In the worst case, every 2-step reduces remainder to $b/2$, and thus we need $\log_2b$ such 2-steps, or total $2\log_2b$ steps.

So $gcd(a,b)$ requires at most $2\log_2b$ recursive calls where $b$ is $\min(a,b)$.

It is worth noting that number of recursive calls depends only on smaller number (not larger number).

selected by

Related questions

3 votes
3 votes
3 answers
2
Diksha Aswal asked Jun 27, 2017
1,043 views
int fun(int n) { int s=0,i; if(n<=1) return 1; for(i=1; i*i<n; i++) s+=n; return fun(n/4)+fun(n/4)+s; } what will be the time complexity, returning value and no. of recur...
0 votes
0 votes
3 answers
3
gshivam63 asked May 19, 2016
2,897 views
int f(int x){if(x<1) return 1;else return f(x-1) +g(x/2);}int g(int x){if(x<2) return 1;else return f(x-1) +g(x/2);}a. LogarithmicB. QuadraticC. LinearD. Exponential
64 votes
64 votes
8 answers
4
Kathleen asked Sep 21, 2014
26,651 views
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?$\Theta(\...