Open In App

Time Complexity of Euclidean Algorithm

In this article, we will discuss the time complexity of the Euclidean Algorithm which is O(log(min(a, b)) and it is achieved.

Euclid’s Algorithm: It is an efficient method for finding the GCD(Greatest  Common Divisor) of two integers. The time complexity of this algorithm is O(log(min(a, b)). Recursively it can be expressed as:



gcd(a, b) = gcd(b, a%b)
where, a and b are two integers.

Proof: Suppose, a and b are two integers such that a  >b then according to Euclid’s Algorithm:



gcd(a, b) = gcd(b, a%b)

Use the above formula repetitively until reach a step where b is 0. At this step, the result will be the GCD of the two integers, which will be equal to a. So, after observing carefully, it can be said that the time complexity of this algorithm would be proportional to the number of steps required to reduce b to 0.

Let’s assume, the number of steps required to reduce b to 0 using this algorithm is N.

gcd(a, b) ------> N steps

Now, if the Euclidean Algorithm for two numbers a and b reduces in N steps then, a should be at least f(N + 2) and b should be at least f(N + 1).

gcd(a, b) ——> N steps
Then, a >= f(N + 2) and b >= f(N + 1)
where, fN is the Nth term in the Fibonacci series(0, 1, 1, 2, 3, …) and N >= 0.

To prove the above statement by using the Principle of Mathematical Induction(PMI):

gcd(b, a%b) ——> (N – 1) steps
Then,  
b >= f(N – 1 + 2) i.e., b >= f(N + 1)
a%b >= f(N – 1 + 1) i.e., a%b >= fN

a = floor(a/b)*b + a%b

floor(a/b)*b means highest multiple which is closest to b.

ex – floor(5/2)*2 = 4. If we then add 5%2=1, we will get a(=5) back.
 

a >= b + (a%b)
This implies, a >= f(N + 1) + fN

a >= f(N + 2) and b >= f(N + 1)

Binet Formula:

fN = {((1 + √5)/2)N – ((1 – √5)/2)N}/√5
          or
fN ≈ ∅N

fN ≈ ∅N
N ≈ log(fN)

Now, from the above statement, it is proved that using the Principle of  Mathematical Induction, it can be said that if the Euclidean algorithm for two numbers a and b reduces in N steps then, a should be at least f(N + 2) and b should be at least f(N + 1).

From the above two results, it can be concluded that:

=> fN+1 ≈ min(a, b)
=> N+1 ≈ logmin(a, b)

=> O(N) = O(N+1) =  log(min(a, b))

Article Tags :