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(\log_2n)$
$\Omega(n)$
$\Theta(\log_2\log_2n)$
$\Theta(\sqrt{n})$
https://stackoverflow.com/questions/3980416/time-complexity-of-euclids-algorithm
https://stackoverflow.com/a/30687762/5982032
Worst case will arise when both $n$ and $m$ are consecutive Fibonacci numbers.
$\text{gcd}(F_n, F_{n-1}) = \text{gcd}(F_{n-1},F_{n-2}) = \dots = \text{gcd}(F_1,F_0) = 1$
and $n^{th}$ Fibonacci number is $1.618^n$, where $1.618$ is the Golden ratio.
So, to find $\text{gcd} (n,m)$, number of recursive calls will be $\Theta (\log n) $.
Correct Answer: $A$
good to read http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibmaths.html
- fibonacci series, got lot of properties (last digit, last 2 digit periodic etc)
-cormen 3rd ed pg 59, ex pg 108 (3rd ed), ch27 pg 776 (3rd given TC for finding ith fib number), pg 993 (given gcd has worst case when n , m are consecutive fibonacci number)
Try to put input in worst case...
@ankit3009, take the worst case input like 97, 48 or whatever you wish.
And, do consider this fact (bolded):
Give a shot, and still if you can't get, revert back with issue, more specifically where you're stuck on.
This is how I approached the Problem.
64.3k questions
77.9k answers
244k comments
80.0k users