Time Complexity of Euclidean Algorithm

27 Jan, 2022
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 stepsThen, 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):
Base Case:Let’s assume a = 2 and b = 1. Then, gcd(2, 1) will reduce to gcd(1, 0) in 1 step, i.e., N = 1.This means 2 should be at least f3 and 1 should be at least f2 and f3 = 2 and f2 = 1.This implies, a is at least f(N + 2) and b is at least f(N + 1). It can be concluded that the statement holds true for the Base Case. 
Let’s assume a = 2 and b = 1. Then, gcd(2, 1) will reduce to gcd(1, 0) in 1 step, i.e., N = 1.
This means 2 should be at least f3 and 1 should be at least f2 and f3 = 2 and f2 = 1.
This implies, a is at least f(N + 2) and b is at least f(N + 1).
 
It can be concluded that the statement holds true for the Base Case. 
Inductive Step: Assume that the statement holds true for the (N – 1)th Step. So, Below are the steps to prove it for the Nth Step:
gcd(b, a%b) ——> (N – 1) stepsThen,  b >= f(N – 1 + 2) i.e., b >= f(N + 1)a%b >= f(N – 1 + 1) i.e., a%b >= fN
It can also be written as:
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. 
Now, (a/b) would always be greater than 1 ( as a >= b). So, from the above result, it is concluded that:
a >= b + (a%b)This implies, a >= f(N + 1) + fN
It is known that each number is the sum of the two preceding terms in a Fibonacci series. This implies f(N + 2) = f(N + 1) + fN.
a >= f(N + 2) and b >= f(N + 1)
Since the above statement holds true for the inductive step as well. This proves that the statement is correct.
Before proceeding further, Look at the Binet Formula:
Binet Formula:
fN = {((1 + √5)/2)N – ((1 – √5)/2)N}/√5          orfN ≈ ∅N
where, ∅ is known as the golden ratio(∅≈1.618), and fN is the Nth Fibonacci Number.
Now, it is already stated that the time complexity will be proportional to N i.e., the number of steps required to reduce b to 0.
So, to prove the time complexity, it is known that:
fN ≈ ∅NN ≈ 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 ≈ log∅min(a, b)
=> O(N) = O(N+1) =  log(min(a, b))
gus4734
harshdangi
HCF
time complexity
Algorithms
Analysis
Mathematical
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Hashing | A Complete Tutorial
Find if there is a path between two vertices in an undirected graph
How to Start Learning DSA?
Complete Roadmap To Learn DSA From Scratch
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Practice Questions on Time Complexity Analysis
Time Complexity and Space Complexity
Time Complexity of building a heap
Analysis of different sorting techniques
Time complexities of different data structures