See Knuth TAOCP, Volume 2 -- he gives the extensive coverage. Just FWIW, a couple of tidbits: it's not proportional to a%b. The worst case is when a and b are consecutive Fibonacci numbers.
–
Jerry CoffinOct 20 '10 at 17:10

7 Answers
7

One trick for analyzing the time complexity of Euclid's algorithm is to follow what happens over two iterations:

a', b' := a % b, b % (a % b)

Now a and b will both decrease, instead of only one, which makes the analysis easier. You can divide it into cases:

Tiny A: 2a <= b

Tiny B: 2b <= a

Small A: 2a > b but a < b

Small B: 2b > a but b < a

Equal: a == b

Now we'll show that every single case decreases the total a+b by at least a quarter:

Tiny A: b % (a % b) < a and 2a <= b, so b is decreased by at least half, so a+b decreased by at least 25%

Tiny B: a % b < b and 2b <= a, so a is decreased by at least half, so a+b decreased by at least 25%

Small A: b will become b-a, which is less than b/2, decreasing a+b by at least 25%.

Small B: a will become a-b, which is less than a/2, decreasing a+b by at least 25%.

Equal: a+b drops to 0, which is obviously decreasing a+b by at least 25%.

Therefore, by case analysis, every double-step decreases a+b by at least 25%. There's a maximum number of times this can happen before a+b is forced to drop below 1. The total number of steps (S) until we hit 0 must satisfy (4/3)^S <= A+B. Now just work it:

Therefore, because the number of iterations is linear in the input size and the total running time is linear in the number of iterations (assuming mod takes constant time), the total running time is linear in the input size.

It is known (see article) that it will never take more steps than five times the number of digits in the smaller number. So the max number of steps grows as the number of digits (ln b). The cost of each step also grows as the number of digits, so the complexity is bound by O(ln^2 b) where b is the smaller nubmer. That's an upper limit, and the actual time is usually less.

Here's intuitive understanding of runtime complexity of Euclid's algorithm. The formal proofs are covered in various texts such as Introduction to Algorithms and TAOCP Vol 2.

First think about what if we tried to take gcd of two Fibonacci numbers F(k+1) and F(k). You might quickly observe that Euclid's algorithm iterates on to F(k) and F(k-1). That is, with each iteration we move down one number in Fibonacci series. As Fibonacci numbers are O(Phi ^ k) where Phi is golden ratio, we can see that runtime of GCD was O(log n) where n=max(a, b) and log has base of Phi. Next, we can prove that this would be the worst case by observing that Fibonacci numbers consistently produces pairs where the remainders remains large enough in each iteration and never become zero until you have arrived at the start of the series.

We can make O(log n) where n=max(a, b) bound even more tighter. Assume that b >= a so we can write bound at O(log b). First, observe that GCD(ka, kb) = GCD(a, b). As biggest values of k is gcd(a,c), we can replace b with b/gcd(a,b) in our runtime leading to more tighter bound of O(log b/gcd(a,b)).