0 votes
gcd(n,m) {
  r = n%m
  if r == 0 return m;
  // else
  return gcd(m, r)
}

in Asymptotic Analysis by AlgoMeister (1.6k points)
edited by

3 Answers

+1 vote
 
Best answer

Below is my attempt at it approaching the algorithm using the Euclidean algorithm. If there's a weak link to this proof, it's probably proving the GCD algorithm is the Euclidean algorithm, or at least behaves similarly. I apologize if the image below taken from pdf is either too large or too small to read. 

by (248 points)
selected by
+1 vote

So for the simple observation. Assume gcd(a,b), I find that the digit of the number being divided will be decreased within two steps at most. So the digit of the number is represented by log10a, then the total step T(a,b) is 2log10a which is O(loga). 

by
Maybe easier to express this in terms of log base 2.  Also, what is the recurrence relation?
0 votes

Hi all,

After research I found my answer as following:

this problem is also called the Euclidian analysis. The worst case in this problem is that the two number n and m are the consecutive Fibonacci numbers Fn+1 and Fbecause it will continue calculating gcd(Fn+1, Fn) = gcd(Fn, Fn-1)

assume that T(a,b) is the steps we need to calculate gcd(a,b)

We know that Fn = O(Øn). and limn->infi(Fn+1/Fn) = Ø   // Ø is the golden ratio

 So T(Fn+1, Fn) = n; 

T(a,b) = OT(a, Fn) = OT(Fn+1, Fn)

So T(a,b) = O(logØb)

Another way to solve this problem is to use the "decreasing factor" introduced by Dr. Kiyoko F Aoki Kinoshita

decreasing factor is defined by d = a/a%b

from the analyse above we know that the main role played among T(a,b) is b

Thus T(a,b) = O(logd​b)

actually there are lots of other methods to resolve this, just list two for reference.

Hope this do any help

Ken

by
edited by anonymous
I would like a simple proof of the time complexity first.  Discussion about Fibonacci numbers is relevant, but not central or required for that simple proof.
The Book: Analysis and Design of Algorithms | Presentations on Slideshare | Lecture Notes, etc
...