In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, 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
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
, the largest number that divides them both without a
remainder
In mathematics, the remainder is the amount "left over" after performing some computation. In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient ( integer division). In a ...
. It is named after the ancient Greek
mathematician
A mathematician is someone who uses an extensive knowledge of mathematics in their work, typically to solve mathematical problems. Mathematicians are concerned with numbers, data, quantity, mathematical structure, structure, space, Mathematica ...
Euclid
Euclid (; ; BC) was an ancient Greek mathematician active as a geometer and logician. Considered the "father of geometry", he is chiefly known for the '' Elements'' treatise, which established the foundations of geometry that largely domina ...
, who first described it in
his ''Elements'' ().
It is an example of an ''
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
'', a step-by-step procedure for performing a calculation according to well-defined rules,
and is one of the oldest algorithms in common use. It can be used to reduce
fraction
A fraction (from , "broken") represents a part of a whole or, more generally, any number of equal parts. When spoken in everyday English, a fraction describes how many parts of a certain size there are, for example, one-half, eight-fifths, thre ...
s 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
In mathematics, a linear combination or superposition is an Expression (mathematics), expression constructed from a Set (mathematics), set of terms by multiplying each term by a constant and adding the results (e.g. a linear combination of ''x'' a ...
of the two original numbers, that is the sum of the two numbers, each multiplied by an
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
(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
In theoretical computer science and mathematics, computational complexity theory focuses on classifying computational problems according to their resource usage, and explores the relationships between these classifications. A computational problem ...
. 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
fraction
A fraction (from , "broken") represents a part of a whole or, more generally, any number of equal parts. When spoken in everyday English, a fraction describes how many parts of a certain size there are, for example, one-half, eight-fifths, thre ...
s to their
simplest form and for performing
division in
modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic operations for integers, other than the usual ones from elementary arithmetic, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to mo ...
. Computations using this algorithm form part of the
cryptographic protocol
A cryptographic protocol is an abstract or concrete Communications protocol, protocol that performs a information security, security-related function and applies cryptographic methods, often as sequences of cryptographic primitives. A protocol desc ...
s that are used to secure
internet
The Internet (or internet) is the Global network, global system of interconnected computer networks that uses the Internet protocol suite (TCP/IP) to communicate between networks and devices. It is a internetworking, network of networks ...
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
In mathematics, the Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer ''n'' by several integers, then one can determine uniquely the remainder of the division of ''n'' by the product of thes ...
, 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
Number theory is a branch of pure mathematics devoted primarily to the study of the integers and arithmetic functions. Number theorists study prime numbers as well as the properties of mathematical objects constructed from integers (for example ...
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 integer
In number theory, a Gaussian integer is a complex number whose real and imaginary parts are both integers. The Gaussian integers, with ordinary addition and multiplication of complex numbers, form an integral domain, usually written as \mathbf ...
s and
polynomials of one variable. This led to modern
abstract algebra
In mathematics, more specifically algebra, abstract algebra or modern algebra is the study of algebraic structures, which are set (mathematics), sets with specific operation (mathematics), operations acting on their elements. Algebraic structur ...
ic notions such as
Euclidean domains.
Background: greatest common divisor
The Euclidean algorithm calculates the greatest common divisor (GCD) of two
natural number
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
s 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
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
(or relatively prime). This property does not imply that or are themselves
prime number
A prime number (or a prime) is a natural number greater than 1 that is not a Product (mathematics), product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime ...
s. 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 mathematics, an empty product, or nullary product or vacuous product, is the result of multiplication, multiplying no factors. It is by convention equal to the multiplicative identity (assuming there is an identity for the multiplication operat ...
); 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 protocol
A cryptographic protocol is an abstract or concrete Communications protocol, protocol that performs a information security, security-related function and applies cryptographic methods, often as sequences of cryptographic primitives. A protocol desc ...
s 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.
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 validity of the Euclidean algorithm can be proven by a two-step argument. In the first step, the final nonzero remainder is shown to divide both and . Since it is a common divisor, it must be less than or equal to the greatest common divisor . In the second step, it is shown that any common divisor of and , including , must divide ; therefore, must be less than or equal to . These two opposite inequalities imply .
To demonstrate that divides both and (the first step), divides its predecessor
:
since the final remainder is zero. also divides its next predecessor
:
because it divides both terms on the right-hand side of the equation. Iterating the same argument, divides all the preceding remainders, including and . None of the preceding remainders , , etc. divide and , since they leave a remainder. Since is a common divisor of and , .
In the second step, any natural number that divides both and (in other words, any common divisor of and ) divides the remainders . By definition, and can be written as multiples of : and , where and are natural numbers. Therefore, divides the initial remainder , since . An analogous argument shows that also divides the subsequent remainders , , etc. Therefore, the greatest common divisor must divide , which implies that . Since the first part of the argument showed the reverse (), it follows that . Thus, is the greatest common divisor of all the succeeding pairs:
: .
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:
Visualization
The Euclidean algorithm can be visualized in terms of the tiling analogy given above for the greatest common divisor.
Assume that we wish to cover an rectangle with square tiles exactly, where is the larger of the two numbers. We first attempt to tile the rectangle using square tiles; however, this leaves an residual rectangle untiled, where . We then attempt to tile the residual rectangle with square tiles. This leaves a second residual rectangle , which we attempt to tile using square tiles, and so on. The sequence ends when there is no residual rectangle, i.e., when the square tiles cover the previous residual rectangle exactly. The length of the sides of the smallest square tile is the GCD of the dimensions of the original rectangle. For example, the smallest square tile in the adjacent figure is (shown in red), and is the GCD of and , the dimensions of the original rectangle (shown in green).
Euclidean division
At every step , the Euclidean algorithm computes a quotient and remainder from two numbers and
: ,
where the is non-negative and is strictly less than the
absolute value
In mathematics, the absolute value or modulus of a real number x, is the non-negative value without regard to its sign. Namely, , x, =x if x is a positive number, and , x, =-x if x is negative (in which case negating x makes -x positive), ...
of . The theorem which underlies the definition of the
Euclidean division ensures that such a quotient and remainder always exist and are unique.
In Euclid's original version of the algorithm, the quotient and remainder are found by repeated subtraction; that is, is subtracted from repeatedly until the remainder is smaller than . After that and are exchanged and the process is iterated. Euclidean division reduces all the steps between two exchanges into a single step, which is thus more efficient. Moreover, the quotients are not needed, thus one may replace Euclidean division by the
modulo operation
In computing and mathematics, the modulo operation returns the remainder or signed remainder of a Division (mathematics), division, after one number is divided by another, the latter being called the ''modular arithmetic, modulus'' of the operatio ...
, which gives only the remainder. Thus the iteration of the Euclidean algorithm becomes simply
: .
Implementations
Implementations of the algorithm may be expressed in
pseudocode. For example, the division-based version may be
programmed as
function gcd(a, b)
while b ≠ 0
t := b
b := a mod b
a := t
return a
At the beginning of the th iteration, the variable holds the latest remainder , whereas the variable holds its predecessor, . The step is equivalent to the above recursion formula . The
temporary variable holds the value of while the next remainder is being calculated. At the end of the loop iteration, the variable holds the remainder , whereas the variable holds its predecessor, .
(If negative inputs are allowed, or if the
mod
function may return negative values, the last line must be replaced with .)
In the subtraction-based version, which was Euclid's original version, the remainder calculation () is replaced by repeated subtraction. Contrary to the division-based version, which works with arbitrary integers as input, the subtraction-based version supposes that the input consists of positive integers and stops when :
function gcd(a, b)
while a ≠ b
if a > b
a := a − b
else
b := b − a
return a
The variables and alternate holding the previous remainders and . Assume that is larger than at the beginning of an iteration; then equals , since . During the loop iteration, is reduced by multiples of the previous remainder until is smaller than . Then is the next remainder . Then is reduced by multiples of until it is again smaller than , giving the next remainder , and so on.
The recursive version is based on the equality of the GCDs of successive remainders and the stopping condition .
function gcd(a, b)
if b = 0
return a
else
return gcd(b, a mod b)
(As above, if negative inputs are allowed, or if the
mod
function may return negative values, the instruction must be replaced by .)
For illustration, the is calculated from the equivalent . The latter GCD is calculated from the , which in turn is calculated from the .
Method of least absolute remainders
In another version of Euclid's algorithm, the quotient at each step is increased by one if the resulting negative remainder is smaller in magnitude than the typical positive remainder.
Previously, the equation
:
assumed that . However, an alternative negative remainder can be computed:
:
if or
:
if .
If is replaced by . when , then one gets a variant of Euclidean algorithm such that
:
at each step.
Leopold Kronecker
Leopold Kronecker (; 7 December 1823 – 29 December 1891) was a German mathematician who worked on number theory, abstract algebra and logic, and criticized Georg Cantor's work on set theory. Heinrich Weber quoted Kronecker
as having said, ...
has shown that this version requires the fewest steps of any version of Euclid's algorithm.
More generally, it has been proven that, for every input numbers ''a'' and ''b'', the number of steps is minimal if and only if is chosen in order that
where
is the
golden ratio
In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their summation, sum to the larger of the two quantities. Expressed algebraically, for quantities and with , is in a golden ratio to if
\fr ...
.
Historical development

The Euclidean algorithm is one of the oldest algorithms in common use.
[, p. 318] It appears in
Euclid's ''Elements'' (c. 300 BC), specifically in Book 7 (Propositions 1–2) and Book 10 (Propositions 2–3). In Book 7, the algorithm is formulated for integers, whereas in Book 10, it is formulated for lengths of line segments. (In modern usage, one would say it was formulated there for
real number
In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
s. But lengths, areas, and volumes, represented as real numbers in modern usage, are not measured in the same units and there is no natural unit of length, area, or volume; the concept of real numbers was unknown at that time.) The latter algorithm is geometrical. The GCD of two lengths and corresponds to the greatest length that measures and evenly; in other words, the lengths and are both integer multiples of the length .
The algorithm was probably not discovered by
Euclid
Euclid (; ; BC) was an ancient Greek mathematician active as a geometer and logician. Considered the "father of geometry", he is chiefly known for the '' Elements'' treatise, which established the foundations of geometry that largely domina ...
, who compiled results from earlier mathematicians in his ''Elements''.
The mathematician and historian
B. L. van der Waerden suggests that Book VII derives from a textbook on
number theory
Number theory is a branch of pure mathematics devoted primarily to the study of the integers and arithmetic functions. Number theorists study prime numbers as well as the properties of mathematical objects constructed from integers (for example ...
written by mathematicians in the school of
Pythagoras.
The algorithm was probably known by
Eudoxus of Cnidus (about 375 BC).
The algorithm may even pre-date Eudoxus, judging from the use of the technical term ἀνθυφαίρεσις (''anthyphairesis'', reciprocal subtraction) in works by Euclid and
Aristotle
Aristotle (; 384–322 BC) was an Ancient Greek philosophy, Ancient Greek philosopher and polymath. His writings cover a broad range of subjects spanning the natural sciences, philosophy, linguistics, economics, politics, psychology, a ...
. Claude Brezinski, following remarks by
Pappus of Alexandria
Pappus of Alexandria (; ; AD) was a Greek mathematics, Greek mathematician of late antiquity known for his ''Synagoge'' (Συναγωγή) or ''Collection'' (), and for Pappus's hexagon theorem in projective geometry. Almost nothing is known a ...
, credits the algorithm to
Theaetetus (c. 417 – c. 369 BC).
Centuries later, Euclid's algorithm was discovered independently both in India and in China,
primarily to solve
Diophantine equations that arose in astronomy and making accurate calendars. In the late 5th century, the Indian mathematician and astronomer
Aryabhata described the algorithm as the "pulverizer",
perhaps because of its effectiveness in solving Diophantine equations. Although a special case of the
Chinese remainder theorem
In mathematics, the Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer ''n'' by several integers, then one can determine uniquely the remainder of the division of ''n'' by the product of thes ...
had already been described in the Chinese book ''
Sunzi Suanjing'', the general solution was published by
Qin Jiushao in his 1247 book ''Shushu Jiuzhang'' (數書九章 ''
Mathematical Treatise in Nine Sections''). The Euclidean algorithm was first described ''numerically'' and popularized in Europe in the second edition of
Bachet's ''Problèmes plaisants et délectables'' (''Pleasant and enjoyable problems'', 1624).
In Europe, it was likewise used to solve Diophantine equations and in developing
continued fractions. The
extended Euclidean algorithm was published by the English mathematician
Nicholas Saunderson, who attributed it to
Roger Cotes as a method for computing continued fractions efficiently.
In the 19th century, the Euclidean algorithm led to the development of new number systems, such as
Gaussian integer
In number theory, a Gaussian integer is a complex number whose real and imaginary parts are both integers. The Gaussian integers, with ordinary addition and multiplication of complex numbers, form an integral domain, usually written as \mathbf ...
s and
Eisenstein integers. In 1815,
Carl Gauss used the Euclidean algorithm to demonstrate unique factorization of
Gaussian integer
In number theory, a Gaussian integer is a complex number whose real and imaginary parts are both integers. The Gaussian integers, with ordinary addition and multiplication of complex numbers, form an integral domain, usually written as \mathbf ...
s, although his work was first published in 1832.
Gauss mentioned the algorithm in his ''
Disquisitiones Arithmeticae'' (published 1801), but only as a method for
continued fractions.
Peter Gustav Lejeune Dirichlet seems to have been the first to describe the Euclidean algorithm as the basis for much of number theory. Lejeune Dirichlet noted that many results of number theory, such as unique factorization, would hold true for any other system of numbers to which the Euclidean algorithm could be applied. Lejeune Dirichlet's lectures on number theory were edited and extended by
Richard Dedekind, who used Euclid's algorithm to study
algebraic integers, a new general type of number. For example, Dedekind was the first to prove
Fermat's two-square theorem using the unique factorization of Gaussian integers. Dedekind also defined the concept of a
Euclidean domain, a number system in which a generalized version of the Euclidean algorithm can be defined (as described
below). In the closing decades of the 19th century, the Euclidean algorithm gradually became eclipsed by Dedekind's more general theory of
ideals.
Other applications of Euclid's algorithm were developed in the 19th century. In 1829,
Charles Sturm showed that the algorithm was useful in the
Sturm chain method for counting the real roots of polynomials in any given interval.
The Euclidean algorithm was the first
integer relation algorithm, which is a method for finding integer relations between commensurate real numbers. Several novel
integer relation algorithms have been developed, such as the algorithm of
Helaman Ferguson and R.W. Forcade (1979) and the
LLL algorithm.
In 1969, Cole and Davie developed a two-player game based on the Euclidean algorithm, called ''The Game of Euclid'', which has an optimal strategy. The players begin with two piles of and stones. The players take turns removing multiples of the smaller pile from the larger. Thus, if the two piles consist of and stones, where is larger than , the next player can reduce the larger pile from stones to stones, as long as the latter is a nonnegative integer. The winner is the first player to reduce one pile to zero stones.
Mathematical applications
Bézout's identity
Bézout's identity states that the greatest common divisor of two integers and can be represented as a linear sum of the original two numbers and . In other words, it is always possible to find integers and such that .
The integers and can be calculated from the quotients , , etc. by reversing the order of equations in Euclid's algorithm. Beginning with the next-to-last equation, can be expressed in terms of the quotient and the two preceding remainders, and :
: .
Those two remainders can be likewise expressed in terms of their quotients and preceding remainders,
: and
: .
Substituting these formulae for and into the first equation yields as a linear sum of the remainders and . The process of substituting remainders by formulae involving their predecessors can be continued until the original numbers and are reached:
:
:
: .
After all the remainders , , etc. have been substituted, the final equation expresses as a linear sum of and , so that .
The Euclidean algorithm, and thus Bézout's identity, can be generalized to the context of
Euclidean domains.
Principal ideals and related problems
Bézout's identity provides yet another definition of the greatest common divisor of two numbers and .
Consider the set of all numbers , where and are any two integers. Since and are both divisible by , every number in the set is divisible by . In other words, every number of the set is an integer multiple of . This is true for every common divisor of and . However, unlike other common divisors, the greatest common divisor is a member of the set; by Bézout's identity, choosing and gives . A smaller common divisor cannot be a member of the set, since every member of the set must be divisible by . Conversely, any multiple of can be obtained by choosing and , where and are the integers of Bézout's identity. This may be seen by multiplying Bézout's identity by ''m'',
: .
Therefore, the set of all numbers is equivalent to the set of multiples of . In other words, the set of all possible sums of integer multiples of two numbers ( and ) is equivalent to the set of multiples of . The GCD is said to be the generator of the
ideal of and . This GCD definition led to the modern
abstract algebra
In mathematics, more specifically algebra, abstract algebra or modern algebra is the study of algebraic structures, which are set (mathematics), sets with specific operation (mathematics), operations acting on their elements. Algebraic structur ...
ic concepts of a
principal ideal (an ideal generated by a single element) and a
principal ideal domain (a
domain in which every ideal is a principal ideal).
Certain problems can be solved using this result. For example, consider two measuring cups of volume and . By adding/subtracting multiples of the first cup and multiples of the second cup, any volume can be measured out. These volumes are all multiples of .
Extended Euclidean algorithm
The integers and of Bézout's identity can be computed efficiently using the
extended Euclidean algorithm. This extension adds two recursive equations to Euclid's algorithm
:
:
with the starting values
:
: .
Using this recursion, Bézout's integers and are given by and , where is the step on which the algorithm terminates with .
The validity of this approach can be shown by induction. Assume that the recursion formula is correct up to step of the algorithm; in other words, assume that
:
for all less than . The th step of the algorithm gives the equation
: .
Since the recursion formula has been assumed to be correct for and , they may be expressed in terms of the corresponding and variables
: .
Rearranging this equation yields the recursion formula for step , as required
: .
Matrix method
The integers and can also be found using an equivalent
matrix method.
The sequence of equations of Euclid's algorithm
:
can be written as a product of quotient matrices multiplying a two-dimensional remainder vector
:
Let represent the product of all the quotient matrices
:
This simplifies the Euclidean algorithm to the form
:
To express as a linear sum of and , both sides of this equation can be multiplied by the
inverse of the matrix .
The
determinant
In mathematics, the determinant is a Scalar (mathematics), scalar-valued function (mathematics), function of the entries of a square matrix. The determinant of a matrix is commonly denoted , , or . Its value characterizes some properties of the ...
of equals , since it equals the product of the determinants of the quotient matrices, each of which is negative one. Since the determinant of is never zero, the vector of the final remainders can be solved using the inverse of
:
Since the top equation gives
: ,
the two integers of Bézout's identity are and . The matrix method is as efficient as the equivalent recursion, with two multiplications and two additions per step of the Euclidean algorithm.
Euclid's lemma and unique factorization
Bézout's identity is essential to many applications of Euclid's algorithm, such as demonstrating the
unique factorization of numbers into prime factors. To illustrate this, suppose that a number can be written as a product of two factors and , that is, . If another number also divides but is coprime with , then must divide , by the following argument: If the greatest common divisor of and is , then integers and can be found such that
:
by Bézout's identity. Multiplying both sides by gives the relation:
:
Since divides both terms on the right-hand side, it must also divide the left-hand side, . This result is known as
Euclid's lemma.
Specifically, if a prime number divides , then it must divide at least one factor of . Conversely, if a number is coprime to each of a series of numbers , , ..., , then is also coprime to their product, .
Euclid's lemma suffices to prove that every number has a unique factorization into prime numbers. To see this, assume the contrary, that there are two independent factorizations of into and prime factors, respectively
: .
Since each prime divides by assumption, it must also divide one of the factors; since each is prime as well, it must be that . Iteratively dividing by the factors shows that each has an equal counterpart ; the two prime factorizations are identical except for their order. The unique factorization of numbers into primes has many applications in mathematical proofs, as shown below.
Linear Diophantine equations
Diophantine equations are equations in which the solutions are restricted to integers; they are named after the 3rd-century Alexandrian mathematician
Diophantus. A typical ''linear'' Diophantine equation seeks integers and such that
:
where , and are given integers. This can be written as an equation for in
modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic operations for integers, other than the usual ones from elementary arithmetic, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to mo ...
:
: .
Let be the greatest common divisor of and . Both terms in are divisible by ; therefore, must also be divisible by , or the equation has no solutions. By dividing both sides by , the equation can be reduced to Bezout's identity
: ,
where and can be found by the
extended Euclidean algorithm. This provides one solution to the Diophantine equation, and .
In general, a linear Diophantine equation has no solutions, or an infinite number of solutions. To find the latter, consider two solutions, and , where
:
or equivalently
: .
Therefore, the smallest difference between two solutions is , whereas the smallest difference between two solutions is . Thus, the solutions may be expressed as
:
: .
By allowing to vary over all possible integers, an infinite family of solutions can be generated from a single solution . If the solutions are required to be ''positive'' integers , only a finite number of solutions may be possible. This restriction on the acceptable solutions allows some systems of Diophantine equations with more unknowns than equations to have a finite number of solutions; this is impossible for a
system of linear equations
In mathematics, a system of linear equations (or linear system) is a collection of two or more linear equations involving the same variable (math), variables.
For example,
: \begin
3x+2y-z=1\\
2x-2y+4z=-2\\
-x+\fracy-z=0
\end
is a system of th ...
when the solutions can be any
real number
In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
(see
Underdetermined system).
Multiplicative inverses and the RSA algorithm
A
finite field
In mathematics, a finite field or Galois field (so-named in honor of Évariste Galois) is a field (mathematics), field that contains a finite number of Element (mathematics), elements. As with any field, a finite field is a Set (mathematics), s ...
is a set of numbers with four generalized operations. The operations are called addition, subtraction, multiplication and division and have their usual properties, such as
commutativity,
associativity and
distributivity. An example of a finite field is the set of 13 numbers using
modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic operations for integers, other than the usual ones from elementary arithmetic, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to mo ...
. In this field, the results of any mathematical operation (addition, subtraction, multiplication, or division) is reduced
modulo
In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the '' modulus'' of the operation.
Given two positive numbers and , mo ...
; that is, multiples of are added or subtracted until the result is brought within the range –. For example, the result of . Such finite fields can be defined for any prime ; using more sophisticated definitions, they can also be defined for any power of a prime . Finite fields are often called
Galois fields, and are abbreviated as or ).
In such a field with numbers, every nonzero element has a unique
modular multiplicative inverse, such that . This inverse can be found by solving the congruence equation , or the equivalent linear Diophantine equation
: .
This equation can be solved by the Euclidean algorithm, as described
above. Finding multiplicative inverses is an essential step in the
RSA algorithm, which is widely used in
electronic commerce
E-commerce (electronic commerce) refers to Commerce, commercial activities including the electronic buying or selling Goods and services, products and services which are conducted on online platforms or over the Internet. E-commerce draws on tec ...
; specifically, the equation determines the integer used to decrypt the message. Although the RSA algorithm uses
rings rather than fields, the Euclidean algorithm can still be used to find a multiplicative inverse where one exists. The Euclidean algorithm also has other applications in
error-correcting codes; for example, it can be used as an alternative to the
Berlekamp–Massey algorithm for decoding
BCH and
Reed–Solomon codes, which are based on Galois fields.
Chinese remainder theorem
Euclid's algorithm can also be used to solve multiple linear Diophantine equations. Such equations arise in the
Chinese remainder theorem
In mathematics, the Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer ''n'' by several integers, then one can determine uniquely the remainder of the division of ''n'' by the product of thes ...
, which describes a novel method to represent an integer ''x''. Instead of representing an integer by its digits, it may be represented by its remainders ''x''
''i'' modulo a set of ''N'' coprime numbers ''m''
''i'':
:
The goal is to determine ''x'' from its ''N'' remainders ''x''
''i''. The solution is to combine the multiple equations into a single linear Diophantine equation with a much larger modulus ''M'' that is the product of all the individual moduli ''m''
''i'', and define ''M''
''i'' as
:
Thus, each ''M''
''i'' is the product of all the moduli ''except'' ''m''
''i''. The solution depends on finding ''N'' new numbers ''h''
''i'' such that
:
With these numbers ''h''
''i'', any integer ''x'' can be reconstructed from its remainders ''x''
''i'' by the equation
:
Since these numbers ''h''
''i'' are the multiplicative inverses of the ''M''
''i'', they may be found using Euclid's algorithm as described in the previous subsection.
Stern–Brocot tree
The Euclidean algorithm can be used to arrange the set of all positive
rational number
In mathematics, a rational number is a number that can be expressed as the quotient or fraction of two integers, a numerator and a non-zero denominator . For example, is a rational number, as is every integer (for example,
The set of all ...
s into an infinite
binary search tree, called the
Stern–Brocot tree.
The number 1 (expressed as a fraction 1/1) is placed at the root of the tree, and the location of any other number ''a''/''b'' can be found by computing gcd(''a'',''b'') using the original form of the Euclidean algorithm, in which each step replaces the larger of the two given numbers by its difference with the smaller number (not its remainder), stopping when two equal numbers are reached. A step of the Euclidean algorithm that replaces the first of the two numbers corresponds to a step in the tree from a node to its right child, and a step that replaces the second of the two numbers corresponds to a step in the tree from a node to its left child. The sequence of steps constructed in this way does not depend on whether ''a''/''b'' is given in lowest terms, and forms a path from the root to a node containing the number ''a''/''b''. This fact can be used to prove that each positive rational number appears exactly once in this tree.
For example, 3/4 can be found by starting at the root, going to the left once, then to the right twice:
:
The Euclidean algorithm has almost the same relationship to another binary tree on the rational numbers called the
Calkin–Wilf tree. The difference is that the path is reversed: instead of producing a path from the root of the tree to a target, it produces a path from the target to the root.
Continued fractions
The Euclidean algorithm has a close relationship with
continued fractions.
The sequence of equations can be written in the form
:
The last term on the right-hand side always equals the inverse of the left-hand side of the next equation. Thus, the first two equations may be combined to form
:
The third equation may be used to substitute the denominator term ''r''
1/''r''
0, yielding
:
The final ratio of remainders ''r''
''k''/''r''
''k''−1 can always be replaced using the next equation in the series, up to the final equation. The result is a continued fraction
:
In the worked example
above, the gcd(1071, 462) was calculated, and the quotients ''q''
''k'' were 2, 3 and 7, respectively. Therefore, the fraction 1071/462 may be written
: