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? $\Theta(\log_2n)$ $\Omega(n)$ $\Theta(\log_2\log_2n)$ $\Theta(\sqrt{n})$ Algorithms gatecse-2007 algorithms recursion time-complexity normal + – Kathleen asked Sep 21, 2014 • retagged Jun 30, 2017 by Silpa Kathleen 26.7k views answer comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments sunil sarode commented Jun 18, 2017 reply Follow Share https://stackoverflow.com/questions/3980416/time-complexity-of-euclids-algorithm 1 votes 1 votes Prakhar Garg commented Mar 18, 2019 reply Follow Share For m=2 and for all n=2^i , running time is ϴ(1) which will contradicts every option... 1 votes 1 votes rohith1001 commented Dec 20, 2019 reply Follow Share https://stackoverflow.com/a/30687762/5982032 0 votes 0 votes Please log in or register to add a comment.
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 shashankrustagi answered Dec 8, 2020 shashankrustagi comment Share Follow See all 0 reply Please log in or register to add a comment.
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) .. Puja Mishra answered Nov 11, 2017 Puja Mishra comment Share Follow See all 2 Comments See all 2 2 Comments reply gourav94240 commented Dec 11, 2019 reply Follow Share For calculating GCD(n,m) EveryTime we are just dividing by m . So T(n) =T(m/n)+O(1) which 2nd case of masters method solving it complexity comes to be O(logn). 2 votes 2 votes svas7246 commented Aug 27, 2020 reply Follow Share could you please elaborate it 0 votes 0 votes Please log in or register to add a comment.
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) Punit Sharma answered Aug 18, 2018 Punit Sharma comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes Euclidean algorithm - Codility Go through it IamDRD answered Sep 21, 2019 IamDRD comment Share Follow See all 0 reply Please log in or register to add a comment.