In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's Topics in Algebra and Serge Lang's Algebra, use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two integers, the largest number that divides them both without a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in his Elements (). It is an example of an algorithm, and is one of the oldest algorithms in common use. It can be used to reduce fractions to their simplest form, and is a part of many other number-theoretic and cryptographic calculations.

The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, is the GCD of and (as and , and the same number is also the GCD of and . Since this replacement reduces the larger of the two numbers, repeating this process gives successively smaller pairs of numbers until the two numbers become equal. When that occurs, that number is the GCD of the original two numbers. By reversing the steps or using the extended Euclidean algorithm, the GCD can be expressed as a linear combination of the two original numbers, that is the sum of the two numbers, each multiplied by an integer (for example, ). The fact that the GCD can always be expressed in this way is known as Bézout's identity.

The version of the Euclidean algorithm described above—which follows Euclid's original presentation—may require many subtraction steps to find the GCD when one of the given numbers is much bigger than the other. A more efficient version of the algorithm shortcuts these steps, instead replacing the larger of the two numbers by its remainder when divided by the smaller of the two (with this version, the algorithm stops when reaching a zero remainder). With this improvement, the algorithm never requires more steps than five times the number of digits (base 10) of the smaller integer. This was proven by Gabriel Lamé in 1844 (Lamé's Theorem), and marks the beginning of computational complexity theory. Additional methods for improving the algorithm's efficiency were developed in the 20th century.

The Euclidean algorithm has many theoretical and practical applications. It is used for reducing fractions to their simplest form and for performing division in modular arithmetic. Computations using this algorithm form part of the cryptographic protocols that are used to secure internet communications, and in methods for breaking these cryptosystems by factoring large composite numbers. The Euclidean algorithm may be used to solve Diophantine equations, such as finding numbers that satisfy multiple congruences according to the Chinese remainder theorem, to construct continued fractions, and to find accurate rational approximations to real numbers. Finally, it can be used as a basic tool for proving theorems in number theory such as Lagrange's four-square theorem and the uniqueness of prime factorizations.

The original algorithm was described only for natural numbers and geometric lengths (real numbers), but the algorithm was generalized in the 19th century to other types of numbers, such as Gaussian integers and polynomials of one variable. This led to modern abstract algebraic notions such as Euclidean domains.

Background: greatest common divisor

The Euclidean algorithm calculates the greatest common divisor (GCD) of two natural numbers and . The greatest common divisor is the largest natural number that divides both and without leaving a remainder. Synonyms for GCD include greatest common factor (GCF), highest common factor (HCF), highest common divisor (HCD), and greatest common measure (GCM). The greatest common divisor is often written as or, more simply, as , although the latter notation is ambiguous, also used for concepts such as an ideal in the ring of integers, which is closely related to GCD.

If , then and are said to be coprime (or relatively prime). This property does not imply that or are themselves prime numbers. For example, and factor as and , so they are not prime, but their prime factors are different, so and are coprime, with no common factors other than .

Let . Since and are both multiples of , they can be written and , and there is no larger number for which this is true. The natural numbers and must be coprime, since any common factor could be factored out of and to make greater. Thus, any other number that divides both and must also divide . The greatest common divisor of and is the unique (positive) common divisor of and that is divisible by any other common divisor .

The greatest common divisor can be visualized as follows. Consider a rectangular area by , and any common divisor that divides both and exactly. The sides of the rectangle can be divided into segments of length , which divides the rectangle into a grid of squares of side length . The GCD is the largest value of for which this is possible. For illustration, a rectangular area can be divided into a grid of: squares, squares, squares, squares, squares or squares. Therefore, is the GCD of and . A rectangular area can be divided into a grid of squares, with two squares along one edge () and five squares along the other ().

The greatest common divisor of two numbers and is the product of the prime factors shared by the two numbers, where each prime factor can be repeated as many times as it divides both and . For example, since can be factored into , and can be factored into , the GCD of and equals , the product of their shared prime factors (with 3 repeated since divides both). If two numbers have no common prime factors, their GCD is (obtained here as an instance of the empty product); in other words, they are coprime. A key advantage of the Euclidean algorithm is that it can find the GCD efficiently without having to compute the prime factors. Factorization of large integers is believed to be a computationally very difficult problem, and the security of many widely used cryptographic protocols is based upon its infeasibility.

Another definition of the GCD is helpful in advanced mathematics, particularly ring theory. The greatest common divisor of two nonzero numbers and is also their smallest positive integral linear combination, that is, the smallest positive number of the form where and are integers. The set of all integral linear combinations of and is actually the same as the set of all multiples of (, where is an integer). In modern mathematical language, the ideal generated by and is the ideal generated by  alone (an ideal generated by a single element is called a principal ideal, and all ideals of the integers are principal ideals). Some properties of the GCD are in fact easier to see with this description, for instance the fact that any common divisor of and also divides the GCD (it divides both terms of ). The equivalence of this GCD definition with the other definitions is described below.

The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. For example,

Thus, Euclid's algorithm, which computes the GCD of two integers, suffices to calculate the GCD of arbitrarily many integers.

Description Procedure

The Euclidean algorithm can be thought of as constructing a sequence of non-negative integers that begins with the two given integers and and will eventually terminate with the integer zero: with The integer will then be the GCD and we can state The algorithm indicates how to construct the intermediate remainders via division-with-remainder on the preceding pair by finding an integer quotient so that:

Because the sequence of non-negative integers is strictly decreasing, it eventually must terminate. In other words, since for every and each is an integer that is strictly smaller than the preceding there eventually cannot be a non-negative integer smaller than zero, and hence the algorithm must terminate. In fact, the algorithm will always terminate at the th step with equal to zero.

To illustrate, suppose the GCD of 1071 and 462 is requested. The sequence is initially and in order to find we need to find integers and such that:

This is the quotient since This determines and so the sequence is now The next step is to continue the sequence to find by finding integers and such that:

This is the quotient since This determines and so the sequence is now The next step is to continue the sequence to find by finding integers and such that:

This is the quotient since This determines and so the sequence is completed as as no further non-negative integer smaller than can be found. The penultimate remainder is therefore the requested GCD:

We can generalize slightly by dropping any ordering requirement on the initial two values and If the algorithm may continue and trivially find that as the sequence of remainders will be If then we can also continue since suggesting the next remainder should be itself, and the sequence is Normally, this would be invalid because it breaks the requirement but now we have by construction, so the requirement is automatically satisfied and the Euclidean algorithm can continue as normal. Therefore, dropping any ordering between the first two integers does not affect the conclusion that the sequence must eventually terminate because the next remainder will always satisfy and everything continues as above. The only modifications that need to be made are that only for and that the sub-sequence of non-negative integers for is strictly decreasing, therefore excluding from both statements.

Proof of validity

The existence of a step such that follows from the condition , for . This ensures that the algorithm terminates.

The last non-zero remainder being equal to follows from the following properties of

1. , for all .

In fact, the common divisors of and are exactly all the divisors of , of which itself is the largest.

2.

In fact, the sets of common divisors of the pairs and are the same. In particular, the largest of their common divisors are the same.

To see this, assume that is a common divisor of and . This means that there are integers such that and . It follows that , where is an integer. Therefore, is also a divisor of .

Conversely, if is a common divisor of and , then there are integers and such that and . From this , where is an integer. Therefore, is also a common divisor of and .

Now, the Euclidean algorithm starts with the pair and at each step, the pair of remainders is replaced by . Property 2 shows that the of the pairs remains invariant. In particular, the of the first pair and the last pair are the same. Applying Property 1, .

Worked example

For illustration, the Euclidean algorithm can be used to find the greatest common divisor of and . To begin, multiples of are subtracted from until the remainder is less than . Two such multiples can be subtracted (), leaving a remainder of : .

Then multiples of are subtracted from until the remainder is less than . Three multiples can be subtracted (), leaving a remainder of : .

Then multiples of are subtracted from until the remainder is less than . Seven multiples can be subtracted (), leaving no remainder: .

Since the last remainder is zero, the algorithm ends with as the greatest common divisor of and . This agrees with the found by prime factorization above. In tabular form, the steps are:

Step kEquationQuotient and remainder 0 and 1 and 2 and ; algorithm ends

Visualization