Javatpoint Logo
Javatpoint Logo

Euclidean algorithm

The Euclidean algorithm, often known as Euclid's algorithm, is an effective way to determine the greatest common divisor (GCD), or the biggest number that divides two integers (numbers) evenly and without leaving a remainder. It was initially described in the 300 BC book The Elements by the Greek mathematician Euclid, for whom it was called. One of the first algorithms still in use, an algorithm is a step-by-step process for carrying out a computation in accordance with predetermined principles. It is a component of several number-theoretic and cryptographic calculations and may be used to simplify fractions. There are several theoretical and real-world uses for the Euclidean algorithm. It is employed in conducting division in modular arithmetic as well as simplifying fractions. This algorithm is employed in both strategies for cracking these cryptosystems by factoring very big composite numbers as well as the cryptographic protocols that safeguard internet communications.

The greatest common divisor of two positive integers can be discovered using the Euclidean method. The greatest integer that divides two numbers evenly is called the GCD. Factorizing both integers and multiplying common prime factors is an easy approach to find GCD.

GCD's fundamental Euclidean algorithm:

The following facts form the basis of the algorithm.

  • GCD remains same if we lower a larger integer by subtracting a smaller one from it. Therefore, if we continually deduct the greater of two, we arrive at GCD.
  • Now, if we divide the smaller number rather of subtracting it, the process halts when we reach the final result of 0.

A recursive function to calculate GCD using Euclid's approach is provided below:


GCD(10, 15) = 5
GCD(35, 10) = 5
GCD(31, 2) = 1
Process executed in 1.11 seconds
Press any key to continue.

Time Complexity: O (Log min(a, b))

Auxiliary Space: O (Log (min(a,b))

Extended Euclidean Algorithm

Additionally, the extended Euclidean method discovers integer coefficients x and y such that axe + by = gcd(a, b) is true.

The results of the recursive call gcd(b%a, a) are used by the extended Euclidean algorithm to update the results of gcd(a, b). Let x1 and y1 be the recursively derived values for x and y. Using the equations shown below, x and y are updated.

An application of the aforesaid strategy is shown below:


gcd(35, 15) = 5
Process executed in 1.11 seconds
Press any key to continue.

Time Complexity: O (log N)

Auxiliary Space: O (log N)

How does Extended Algorithm Work?

How Can an Extended Algorithm Be Useful?

When a and b are coprime (or gcd is 1), the extended Euclidean method is quite helpful. Since y is the modular multiplicative inverse of "b modulo a," and x is the inverse of "a modulo b," In instance, a crucial step in the RSA public-key encryption method is the computation of the modular multiplicative inverse.

Next TopicFloyd's Algorithm

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA