Time Complexity of Euclidean Algorithm
Last Updated :
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 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):
- 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) steps
Then,
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
or
fN ≈ ∅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 ≈ ∅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 ≈ log∅min(a, b)
=> O(N) = O(N+1) = log(min(a, b))
Similar Reads
Euclidean algorithms (Basic and Extended)
The Euclidean algorithm is a way to find the greatest common divisor of two positive integers. GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.Examples:input: a = 12, b = 20Output: 4Explanatio
9 min read
Euclid's Algorithm when % and / operations are costly
Euclid's algorithm is used to find GCD of two numbers. There are mainly two versions of algorithm. Version 1 (Using subtraction) C++ // Recursive function to return gcd of a and b int gcd(int a, int b) { if (a == b) return a; return (a > b) ? gcd(a - b, b) : gcd(a, b - a); } C // Recursive functi
9 min read
Common Divisor Reduction Algorithm
Given two integers x and y. In one step, we have to subtract the greatest common divisor of x and y from x and y each till x ⥠1 and y ⥠1. We have to find the minimum number of repetitions of this step. Examples: Input: x = 36, y = 16Output: 4Explanation: GCD of 36 and 16 is 4, so replace 36 and 16
11 min read
How to solve RSA Algorithm Problems?
RSA algorithm is an asymmetric cryptography algorithm which means, there should be two keys involve while communicating, i.e., public key and private key. There are simple steps to solve problems on the RSA Algorithm. Example-1: Step-1: Choose two prime number p and q Lets take p = 3 and q = 11 Step
8 min read
Boothâs Multiplication Algorithm
Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. Boothâs algorithm is of interest in the study of computer arch
15 min read
Maths for Data Structure and Algorithms (DSA) | A Complete Guide
Maths is a fundamental component of learning Data Structure and Algorithms, just like in programming. Maths is primarily used to evaluate the effectiveness of different algorithms. However, there are situations when the answer requires some mathematical understanding or the problem has mathematical
15+ min read
Pollard's Rho Algorithm for Prime Factorization
Given a positive integer n, and that it is composite, find a divisor of it.Example:Input: n = 12;Output: 2 [OR 3 OR 4]Input: n = 187;Output: 11 [OR 17]Brute approach: Test all integers less than n until a divisor is found. Improvisation: Test all integers less than ?nA large enough number will still
14 min read
Stein's Algorithm for finding GCD
Stein's algorithm or binary GCD algorithm is an algorithm that computes the greatest common divisor of two non-negative integers. Steinâs algorithm replaces division with arithmetic shifts, comparisons, and subtraction.Examples: Input: a = 17, b = 34 Output : 17Input: a = 50, b = 49Output: 1Algorith
14 min read
Trial division Algorithm for Prime Factorization
In this article, the trial division method to check whether a number is a prime or not is discussed. Given a number N, the task is to check whether the number is prime or not. Examples: Input: N = 433 Output: Prime Explanation: The only factors of 433 are 1 and 433. Therefore, it is a prime. Input:
7 min read
Barrett Reduction Algorithm (Optimized Variant)
The Barrett Reduction Algorithm computes the remainder of a big integer x divided by another large integer mod. The procedure precomputes mu, the inverse of mod with regard to the next power of 2. All mod calculations utilize this value. Approach: The Barrett Reduction Algorithm approximates x divid
8 min read