Overview

The Euclidean algorithm transforms a common-divisor problem into a sequence of smaller problems. It is both an exact computational procedure and a short proof that greatest common divisors are accessible without knowing prime factorizations. Keeping track of the same divisions also produces coefficients for a linear combination of the original integers, which is the extended algorithm.

Prerequisites

Understand integer divisibility and division with remainder. For b>0b>0, division writes a=qb+ra=qb+r with 0≤r<b0\le r<b. A greatest common divisor is chosen nonnegative, so negative starting inputs can first be replaced by their absolute values.

Definition and conventions

Start with a pair (a,b)(a,b) of nonnegative integers. While b>0b>0, replace it by (b,r)(b,r), where rr is the remainder of aa on division by bb. Stop when the second component is zero. The first component of the final pair is gcd⁡(a,b)\gcd(a,b).

For (0,0)(0,0), the procedure stops immediately and returns zero, matching the convention for that gcd. Division by a zero second component is never attempted. If the initial first component is smaller, the first quotient is zero; the next pair simply reverses the order.

Intuition

The equality a=qb+ra=qb+r shows that a common divisor of a,ba,b is also a divisor of b,rb,r. The converse follows from a=qb+ra=qb+r. Therefore each step preserves exactly the same set of common divisors. Meanwhile the positive second components strictly decrease, so the process cannot continue forever.

Examples and non-examples

For 119119 and 5151, the divisions are

119=2⋅51+17,51=3⋅17+0.119=2\cdot51+17,\qquad51=3\cdot17+0.

The gcd is 1717. The first equation gives the certificate 17=119−2⋅5117=119-2\cdot51. Hence 119x+51y=17119x+51y=17 has an integer solution, but 119x+51y=1119x+51y=1 does not.

For 3535 and 1212, division yields remainders 1111 and 11. Back substitution gives 1=3⋅12−351=3\cdot12-35, so −1-1 is an inverse of 3535 modulo 1212. It represents the same residue class as 1111. Returning the last quotient instead of the last nonzero remainder would give an incorrect algorithm.

Important properties

The invariant and termination prove correctness. At the final pair (d,0)(d,0), the nonnegative greatest common divisor is dd. Since the earlier pairs have the same common divisors, this is also the gcd of the initial pair.

Every remainder is an integer linear combination of the original inputs: this is true initially, and subtraction of a multiple preserves it. Thus back substitution constructs Bézout coefficients. Those coefficients are usually not unique. If au+bv=dau+bv=d and both inputs are nonzero, replacing (u,v)(u,v) by (u+tb/d,v−ta/d)(u+tb/d,v-ta/d) leaves the equality unchanged for any integer tt.

Connections and further reading

The procedure solves linear Diophantine equations and computes modular inverses when the gcd is one. Polynomial division provides an analogous algorithm over a field; general rings need a suitable division function. Judson, Subsection 2.2.1 gives the integer foundation.