Open In App

Extended Euclid Division Algorithm

Last Updated : 28 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Extended Euclidean Algorithm is an extension of the classic Euclidean Algorithm. While the Euclidean Algorithm focuses on finding the greatest common divisor (GCD) of two integers, the Extended Euclidean Algorithm can also find integers x and y to express their greatest common divisor (gcd) as a linear combination of numbers.

Extended Euclidean Algorithm, states that

For any two integers a and b (with b≠0 ), it is possible to express their greatest common divisor (GCD) as a linear combination of a and b. Specifically, if d=GCD(a, b) then there exist unique integers x and y such that

d = ax + by

Where d is the greatest divisor of a and b i.e. d= GCD(a, b)

This expression of greatest common divisor (GCD) as a linear combination of a and b is called the Bezout Identity.

Proof of Extended Euclidean Algorithm

The proof of the Extended Euclidean Algorithm involves demonstrating two main aspects:

  • Finding the GCD: Show that the algorithm indeed finds the greatest common divisor (GCD) of two integers a and b.
  • Linear Combination: Prove that it expresses the GCD as a linear combination of a and b.

Follow these steps to understand the proof of the Extended Euclidean Algorithm, which calculates the GCD of two integers a and b and expresses it as a linear combination.

Step 1: Euclidean Algorithm Setup.
First, write the Euclidean Algorithm as a series of steps finding remainders:

  • a = bq1 + r1 (where r1​ is the remainder when a is divided by b.)
  • b = r1 q2 + r2
  • r1 = r2 q3 + r3
  • Continue until rn − 1 = rnqn + 1 + 0

Here, GCD(a ,b) = rn, the last non-zero remainder.

Step 2: Expressing the GCD as a Linear Combination.
This critical step where we will express each remainder ri as a linear combination of a and b.

  • Starting with the final remainder equation that equals the GCD.
    • rn​ = rn − 2 ​ − rn − 1 ​qn​ 

Here, qn​ is the quotient from the previous division.

We will continue this process backtracking through all previous equations, substituting for each each ri​ until we can express rn​ (which is GCD(a, b)) purely in terms of a and b.

For example, if we had the sequence:

  • rn − 1 = rn − 3 − rn − 2qn − 1

Substituting this into the equation for rn​:

  • rn = (rn − 3 − rn − 2qn − 1) − rn − 1qn

Continue this process until all ri are expressed in terms of a and b.

Step 3: Recursive Construction of Coefficients.
To keep track of the coefficients x and y in the equation ax + by = GCD⁡(a, b), we define:

  • x0 = 1, y0 = 0 (for a)
  • x1 = 0, y1 = 1 (for b)

For each step i, as we perform divisions, we also compute:
For each remainder equation:

  • ri = axi + byi

The coefficients x and y for the new remainder can be calculated as follows:
From

  • ri = ri − 2 − ri − 1qi:
  • xi = xi − 2 − xi − 1qi
  • yi = yi − 2 − yi − 1qi

This process allows for the recursive construction of the coefficients x and y for each step.

Step 4: Final Expression for GCD⁡(a, b).
After back-substituting through the remainder equations, you will eventually reach a point where:

  • rn = ax + by

where rn = GCD⁡(a, b). The integers x and y are the coefficients we sought.
This concludes the proof of the Extended Euclidean Algorithm, illustrating how to find x and y such that ax + by = GCD(a, b).

Lets understand the Extended Euclid Division Algorithm with an example,

Example 1: Find two integers a and b such that 240a + 46b = GCD(240, 46).

Solution:

First we use Euclid's algorithm to find the GCD( 240, 46);

240 = 46 × 5 + 1 0 .... (i)
46 = 10 × 4 + 6 .... (ii)
10 = 6 × 1 + 4 .... (iii)
6 = 4 × 1 + 2 .... (iv)
4 = 2 × 2 + 0 .... (v)

So GCD( 240, 46) is 2

Now we rewrite the remainder equation to express the GCD as a combination of 240 and 46.

from eq (iv) we get
2 = 6 - 4 × 1

From eq(iii) we get , ( 4 = 10 - 6) substituting this value in the above equation
2 = 6 - ( 10 -6) × 1
2 = 6 × 2 - 10

From eq(ii) we get, ( 6 = 46 -10 × 4) substituting this value in the above equation
2 = (46 − 10 × 4 ) × 2 − 10 = 46 × 2 − 10 × 9
2 = 46 × 2 − 10 × 9

From eq(i) we get, ( 10 = 240 − 46 × 5) substituting this value in the above equation
2 = 46 × 2 − ( 240 − 46 × 5 ) × 9 = 46 × 2 − ( 240 × 9 − 46 × 45) = 46 × 47 - 240 × 9
2 = 46 × 47 - 240 × 9

So, x = − 9 and y = 47,

which satisfies the equation ax + by = GCD ( a, b )
240( − 9) + 46(47) = 2

Example 2: Solve the linear equation 81x + 57y=3 to finding values of x and y using the Extended Euclidean Algorithm.

Solution:

First we use Euclid's algorithm to find the GCD( 81, 57);

81 = 57 × 1 + 24 .... (i)
57 = 24 × 2 + 9 .... (ii)
24 = 9 × 2 + 6 .... (iii)
9 = 6 × 1 + 3 .... (iv)
6 = 3 × 2 + 0 .... (v)

So GCD( 240, 46) is 3

Now we rewrite the remainder equation to express the GCD as a combination of 81 and 57.

from eq (iv) ( 9 = 6 × 1 + 3 ) we get
3 = 9 - 6 × 1

From eq(iii) we get , ( 6 = 24 - 9 × 2) substituting this value in the above equation
3 = 9 - ( 24 - 9 × 2 ) × 1
3 = 9 × 3 - 24

From eq(ii) we get, ( 9 = 57 -24 × 2) substituting this value in the above equation
3 = (57 -24 × 2) × 3 - 24
3 = 57 × 3 − 24 × 7

From eq(i) we get, ( 24 = 81 - 57 × 1) substituting this value in the above equation
3 = 57 × 3 − (81 - 57 ) × 7 = 57 × 10 − 81 × 7

3 = 57 × 10 + 81 × (-7)

Comparing it with 81x + 57y = 3 we get

So, x = -7 and y = 10,

which satisfies the equation

240( − 9) + 46(47) = 2

ax + by = GCD ( a, b )

Uses of the Extended Euclidean Algorithm

The Extended Euclidean Algorithm is helpful in a few important areas:

  • Cryptography: It helps in finding modular inverses, which are necessary for encryption and decryption in RSA and other cryptographic methods.
  • Solving Equations: It can solve certain equations that need integer solutions, called linear Diophantine equations, like
    ( ax + by = c)
  • Modular Arithmetic: It finds inverses that help in modular arithmetic, allowing operations with fractions in modular systems.
  • Computer Algorithms: Many computer programs use the Extended Euclidean Algorithm for mathematical functions, especially when working with large numbers.

Next Article - Applications of GCD in Real Life

For Programmer -
Euclidean Algorithms basic and extended


Next Article

Similar Reads