• retagged by
2,533 views

1 Answer

Best answer
9 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
Position:
Show:

Related questions

4 4 votes
3 answers 3 answers
2.0k
2.0k views
Diksha Aswal asked Jun 27, 2017
2,025 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...
5 5 votes
1 1 answer
397
397 views
GO Classes asked Jul 1
397 views
Consider the following Python code:def virfib_sq(n): print(n) if n <= 1: return n return (virfib_sq(n - 1) + virfib_sq(n - 2)) 2 r4 = virfib_sq(4)What would be the outp...