Skip to content

Extended Euclidean Algorithm

The Euclidean algorithm computes the greatest common divisor of two integers. The extended Euclidean algorithm does more. It also expresses the gcd as an integer linear...

From GCDs to Linear Combinations

The Euclidean algorithm computes the greatest common divisor of two integers. The extended Euclidean algorithm does more. It also expresses the gcd as an integer linear combination of the original two integers.

For integers aa and bb, not both zero, it finds integers xx and yy such that

gcd(a,b)=ax+by. \gcd(a,b)=ax+by.

This identity is called Bézout’s identity.

For example,

gcd(30,18)=6, \gcd(30,18)=6,

and

6=3018. 6=30-18.

Thus

6=130+(1)18. 6=1\cdot30+(-1)\cdot18.

The extended Euclidean algorithm gives such coefficients systematically.

The Basic Idea

The ordinary Euclidean algorithm repeatedly divides with remainder:

a=bq+r. a=bq+r.

The key observation is that each remainder can be written as a linear combination of aa and bb.

Since

r=abq, r=a-bq,

the first remainder is already a linear combination.

The next remainder is obtained by dividing bb by rr, and it can also be rewritten in terms of aa and bb. Continuing this process, the last nonzero remainder, which is the gcd, becomes a linear combination of the original numbers.

Example

Compute gcd(1071,462)\gcd(1071,462) and express it as a linear combination of 10711071 and 462462.

The Euclidean algorithm gives

1071=4622+147, 1071=462\cdot2+147, 462=1473+21, 462=147\cdot3+21, 147=217+0. 147=21\cdot7+0.

Thus

gcd(1071,462)=21. \gcd(1071,462)=21.

Now work backward.

From the second equation,

21=4621473. 21=462-147\cdot3.

From the first equation,

147=10714622. 147=1071-462\cdot2.

Substitute this into the expression for 2121:

21=4623(10714622). 21=462-3(1071-462\cdot2).

Expanding,

21=46231071+6462. 21=462-3\cdot1071+6\cdot462.

Thus

21=(3)1071+7462. 21=(-3)\cdot1071+7\cdot462.

So one Bézout representation is

gcd(1071,462)=(3)1071+7462. \gcd(1071,462)=(-3)1071+7\cdot462.

Tabular Form

The backward substitution method is clear but can become cumbersome. A tabular version tracks coefficients during the Euclidean algorithm.

Start with

r0=a,r1=b. r_0=a, \qquad r_1=b.

Write each remainder as

ri=sia+tib. r_i=s_i a+t_i b.

Initially,

r0=1a+0b, r_0=1\cdot a+0\cdot b,

so

s0=1,t0=0. s_0=1, \qquad t_0=0.

Also,

r1=0a+1b, r_1=0\cdot a+1\cdot b,

so

s1=0,t1=1. s_1=0, \qquad t_1=1.

If

ri1=qiri+ri+1, r_{i-1}=q_i r_i+r_{i+1},

then

ri+1=ri1qiri. r_{i+1}=r_{i-1}-q_i r_i.

Hence the coefficients satisfy

si+1=si1qisi, s_{i+1}=s_{i-1}-q_i s_i, ti+1=ti1qiti. t_{i+1}=t_{i-1}-q_i t_i.

When the final nonzero remainder is reached, its coefficients give the desired Bézout identity.

Existence of Bézout Coefficients

The extended Euclidean algorithm proves that for any integers aa and bb, not both zero, there exist integers xx and yy such that

gcd(a,b)=ax+by. \gcd(a,b)=ax+by.

This is a stronger statement than merely saying that the gcd exists.

It says that the greatest common divisor belongs to the set of all integer linear combinations

ax+by,x,yZ. ax+by, \qquad x,y\in\mathbb{Z}.

This fact will become essential in solving Diophantine equations and congruences.

Modular Inverses

One of the most important applications is the computation of modular inverses.

Suppose

gcd(a,n)=1. \gcd(a,n)=1.

Then the extended Euclidean algorithm gives integers xx and yy such that

ax+ny=1. ax+ny=1.

Reducing this equation modulo nn, we obtain

ax1(modn). ax\equiv1\pmod n.

Thus xx is a multiplicative inverse of aa modulo nn.

For example, find the inverse of 77 modulo 2626. The Euclidean algorithm gives

26=73+5, 26=7\cdot3+5, 7=51+2, 7=5\cdot1+2, 5=22+1. 5=2\cdot2+1.

Working backward,

1=522. 1=5-2\cdot2.

Since

2=75, 2=7-5,

we get

1=52(75)=3527. 1=5-2(7-5)=3\cdot5-2\cdot7.

Since

5=2637, 5=26-3\cdot7,

we obtain

1=3(2637)27=326117. 1=3(26-3\cdot7)-2\cdot7 =3\cdot26-11\cdot7.

Hence

1171(mod26). -11\cdot7\equiv1\pmod{26}.

Therefore an inverse of 77 modulo 2626 is

1115(mod26). -11\equiv15\pmod{26}.

Indeed,

715=1051(mod26). 7\cdot15=105\equiv1\pmod{26}.

Linear Diophantine Equations

The extended Euclidean algorithm also solves equations of the form

ax+by=c. ax+by=c.

Such an equation has an integer solution exactly when

gcd(a,b)c. \gcd(a,b)\mid c.

If d=gcd(a,b)d=\gcd(a,b), and

d=ax0+by0, d=ax_0+by_0,

then multiplying by c/dc/d gives

c=a(x0cd)+b(y0cd). c=a\left(x_0\frac cd\right)+b\left(y_0\frac cd\right).

Thus a solution is obtained directly from Bézout coefficients.

Role in Number Theory

The extended Euclidean algorithm is one of the central computational tools of elementary number theory.

It computes gcds, proves Bézout identities, finds modular inverses, solves linear Diophantine equations, and prepares the ground for the Chinese remainder theorem.

Its importance comes from the fact that it turns divisibility into explicit arithmetic. It does not merely prove that a gcd exists. It shows how the gcd is built from the two original integers.