Euclidean algorithm
{{#invoke:Hatnote|hatnote}}
In mathematics, the Euclidean algorithm^{[a]}, or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in Euclid's Elements (c. 300 BC). It is an example of an algorithm, a step-by-step procedure for performing a calculation according to well-defined rules, and is one of the oldest numerical algorithms still 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, 21 is the GCD of 252 and 105 (252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 147 = 252 − 105. Since this replacement reduces the larger of the two numbers, repeating this process gives successively smaller pairs of numbers until one of the two numbers reaches zero. When that occurs, the other number (the one that is not zero) is the GCD of the original two numbers. By reversing the steps, the GCD can be expressed as a sum of the two original numbers each multiplied by a positive or negative integer, e.g., Template:Nowrap begin21 = 5 × 105 + (−2) × 252.Template:Nowrap end 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 (and by Euclid) can take 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 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, 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 is 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
{{#invoke:main|main}}
The Euclidean algorithm calculates the greatest common divisor (GCD) of two natural numbers a and b. The greatest common divisor g is the largest natural number that divides both a and b without leaving a remainder. Synonyms for the GCD include the greatest common factor (GCF), the highest common factor (HCF), and the greatest common measure (GCM). The greatest common divisor is often written as gcd(a, b) or, more simply, as (a, b),^{[1]} although the latter notation is also used for other mathematical concepts, such as two-dimensional vectors.
If gcd(a, b) = 1, then a and b are said to be coprime (or relatively prime).^{[2]} This property does not imply that a or b are themselves prime numbers.^{[3]} For example, neither 6 nor 35 is a prime number, since they both have two prime factors: 6 = 2 × 3 and 35 = 5 × 7. Nevertheless, 6 and 35 are coprime. No natural number other than 1 divides both 6 and 35, since they have no prime factors in common.
Let g = gcd(a, b). Since a and b are both multiples of g, they can be written a = mg and b = ng, and there is no larger number G > g for which this is true. The natural numbers m and n must be coprime, since any common factor could be factored out of m and n to make g greater. Thus, any other number c that divides both a and b must also divide g. The greatest common divisor g of a and b is the unique (positive) common divisor of a and b that is divisible by any other common divisor c.^{[4]}
The GCD can be visualized as follows.^{[5]} Consider a rectangular area a by b, and any common divisor c that divides both a and b exactly. The sides of the rectangle can be divided into segments of length c, which divides the rectangle into a grid of squares of side length c. The greatest common divisor g is the largest value of c for which this is possible. For illustration, a 24-by-60 rectangular area can be divided into a grid of: 1-by-1 squares, 2-by-2 squares, 3-by-3 squares, 4-by-4 squares, 6-by-6 squares or 12-by-12 squares. Therefore, 12 is the greatest common divisor of 24 and 60. A 24-by-60 rectangular area can be divided into a grid of 12-by-12 squares, with two squares along one edge (24/12 = 2) and five squares along the other (60/12 = 5).
The GCD of two numbers a and b is the product of the prime factors shared by the two numbers, where a same prime factor can be used multiple times, but only as long as the product of these factors divides both a and b.^{[6]} For example, since 1386 can be factored into 2 × 3 × 3 × 7 × 11, and 3213 can be factored into 3 × 3 × 3 × 7 × 17, the greatest common divisor of 1386 and 3213 equals 63 = 3 × 3 × 7, the product of their shared prime factors. If two numbers have no prime factors in common, their greatest common divisor is 1 (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.^{[7]}^{[8]} Factorization of large integers is believed to be a computationally very difficult problem, and the security of many modern cryptography systems is based upon its infeasibility.^{[9]}
Another definition of the GCD is helpful in advanced mathematics, particularly ring theory.^{[10]} The greatest common divisor g of two nonzero numbers a and b is also their smallest positive integral linear combination, that is, the smallest positive number of the form ua + vb where u and v are integers. The set of all integral linear combinations of a and b is actually the same as the set of all multiples of g (mg, where m is an integer). In modern mathematical language, the ideal generated by a and b is the ideal generated by g 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 a and b also divides the GCD (it divides both terms of ua + vb). 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,^{[11]} but it can also be calculated by repeatedly taking the GCDs of pairs of numbers.^{[12]} For example,
- gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b).
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 proceeds in a series of steps such that the output of each step is used as an input for the next one. Let k be an integer that counts the steps of the algorithm, starting with zero. Thus, the initial step corresponds to k = 0, the next step corresponds to k = 1, and so on.
Each step begins with two nonnegative remainders r_{k−1} and r_{k−2}. Since the algorithm ensures that the remainders decrease steadily with every step, r_{k−1} is less than its predecessor r_{k−2}. The goal of the kth step is to find a quotient q_{k} and remainder r_{k} that satisfy the equation
- r_{k−2} = q_{k} r_{k−1} + r_{k}
and that have r_{k} < r_{k−1}. In other words, multiples of the smaller number r_{k−1} are subtracted from the larger number r_{k−2} until the remainder r_{k} is smaller than r_{k−1}.
In the initial step (k = 0), the remainders r_{−2} and r_{−1} equal a and b, the numbers for which the GCD is sought. In the next step (k = 1), the remainders equal b and the remainder r_{0} of the initial step, and so on. Thus, the algorithm can be written as a sequence of equations
- a = q_{0} b + r_{0}
- b = q_{1} r_{0} + r_{1}
- r_{0} = q_{2} r_{1} + r_{2}
- r_{1} = q_{3} r_{2} + r_{3}
- …
If a is smaller than b, the first step of the algorithm swaps the numbers. For example, if a < b, the initial quotient q_{0} equals zero, and the remainder r_{0} is a. Thus, r_{k} is smaller than its predecessor r_{k−1} for all k ≥ 0.
Since the remainders decrease with every step but can never be negative, a remainder r_{N} must eventually equal zero, at which point the algorithm stops.^{[13]} The final nonzero remainder r_{N−1} is the greatest common divisor of a and b. The number N cannot be infinite because there are only a finite number of nonnegative integers between the initial remainder r_{0} and zero.
Proof of validity
The validity of the Euclidean algorithm can be proven by a two-step argument.^{[14]} In the first step, the final nonzero remainder r_{N−1} is shown to divide both a and b. Since it is a common divisor, it must be less than or equal to the greatest common divisor g. In the second step, it is shown that any common divisor of a and b, including g, must divide r_{N−1}; therefore, g must be less than or equal to r_{N−1}. These two conclusions are inconsistent unless r_{N−1} = g.
To demonstrate that r_{N−1} divides both a and b (the first step), r_{N−1} divides its predecessor r_{N−2}
- r_{N−2} = q_{N} r_{N−1}
since the final remainder r_{N} is zero. r_{N−1} also divides its next predecessor r_{N−3}
- r_{N−3} = q_{N−1} r_{N−2} + r_{N−1}
because it divides both terms on the right-hand side of the equation. Iterating the same argument, r_{N−1} divides all the preceding remainders, including a and b. None of the preceding remainders r_{N−2}, r_{N−3}, etc. divide a and b, since they leave a remainder. Since r_{N−1} is a common divisor of a and b, r_{N−1} ≤ g.
In the second step, any natural number c that divides both a and b (in other words, any common divisor of a and b) divides the remainders r_{k}. By definition, a and b can be written as multiples of c: a = mc and b = nc, where m and n are natural numbers. Therefore, c divides the initial remainder r_{0}, since r_{0} = a − q_{0}b = mc − q_{0}nc = (m − q_{0}n)c. An analogous argument shows that c also divides the subsequent remainders r_{1}, r_{2}, etc. Therefore, the greatest common divisor g must divide r_{N−1}, which implies that g ≤ r_{N−1}. Since the first part of the argument showed the reverse (r_{N−1} ≤ g), it follows that g = r_{N−1}. Thus, g is the greatest common divisor of all the succeeding pairs:^{[15]}^{[16]}
- g = gcd(a, b) = gcd(b, r_{0}) = gcd(r_{0}, r_{1}) = … = gcd(r_{N−2}, r_{N−1}) = r_{N−1}.
Worked example
For illustration, the Euclidean algorithm can be used to find the greatest common divisor of a = 1071 and b = 462. To begin, multiples of 462 are subtracted from 1071 until the remainder is less than 462. Two such multiples can be subtracted (q_{0} = 2), leaving a remainder of 147:
- 1071 = 2 × 462 + 147.
Then multiples of 147 are subtracted from 462 until the remainder is less than 147. Three multiples can be subtracted (q_{1} = 3), leaving a remainder of 21:
- 462 = 3 × 147 + 21.
Then multiples of 21 are subtracted from 147 until the remainder is less than 21. Seven multiples can be subtracted (q_{2} = 7), leaving no remainder:
- 147 = 7 × 21 + 0.
Since the last remainder is zero, the algorithm ends with 21 as the greatest common divisor of 1071 and 462. This agrees with the gcd(1071, 462) found by prime factorization above. In tabular form, the steps are
Step k | Equation | Quotient and remainder |
---|---|---|
0 | 1071 = q_{0} 462 + r_{0} | q_{0} = 2 and r_{0} = 147 |
1 | 462 = q_{1} 147 + r_{1} | q_{1} = 3 and r_{1} = 21 |
2 | 147 = q_{2} 21 + r_{2} | q_{2} = 7 and r_{2} = 0; algorithm ends |
Visualization
The Euclidean algorithm can be visualized in terms of the tiling analogy given above for the greatest common divisor.^{[17]} Assume that we wish to cover an a-by-b rectangle with square tiles exactly, where a is the larger of the two numbers. We first attempt to tile the rectangle using b-by-b square tiles; however, this leaves an r_{0}-by-b residual rectangle untiled, where r_{0} < b. We then attempt to tile the residual rectangle with r_{0}-by-r_{0} square tiles. This leaves a second residual rectangle r_{1}-by-r_{0}, which we attempt to tile using r_{1}-by-r_{1} 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 21-by-21 (shown in red), and 21 is the GCD of 1071 and 462, the dimensions of the original rectangle (shown in green).
Euclidean division
{{#invoke:main|main}}
At every step k, the Euclidean algorithm computes a quotient q_{k} and remainder r_{k} from two numbers r_{k−1} and r_{k−2}
- r_{k−2} = q_{k} r_{k−1} + r_{k}
where the magnitude of r_{k} is strictly less than that of r_{k−1}. The theorem which underlies the definition of the Euclidean division ensures that such a quotient and remainder always exist and are unique.^{[18]}
In Euclid's original version of the algorithm, the quotient and remainder are found by repeated subtraction; that is, r_{k−1} is subtracted from r_{k−2} repeatedly until the remainder r_{k} is smaller than r_{k−1}. After that r_{k} and r_{k−1} 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, which gives only the remainder. Thus the iteration of the Euclidean algorithm becomes simply
- r_{k} = r_{k−2} mod r_{k−1}.
Implementations
Implementations of the algorithm may be expressed in pseudocode. For example, the division-based version may be programmed as^{[19]}
function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a
At the beginning of the kth iteration, the variable b holds the latest remainder r_{k−1}, whereas the variable a holds its predecessor, r_{k−2}. The step b := a mod b is equivalent to the above recursion formula r_{k} ≡ r_{k−2} mod r_{k−1}. The temporary variable t holds the value of r_{k−1} while the next remainder r_{k} is being calculated. At the end of the loop iteration, the variable b holds the remainder r_{k}, whereas the variable a holds its predecessor, r_{k−1}.
In the subtraction-based version which was Euclid's original version, the remainder calculation (b = a mod b) is replaced by repeated subtraction.^{[20]} 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 a = b:
function gcd(a, b) while a ≠ b if a > b a := a − b else b := b − a return a
The variables a and b alternate holding the previous remainders r_{k−1} and r_{k−2}. Assume that a is larger than b at the beginning of an iteration; then a equals r_{k−2}, since r_{k−2} > r_{k−1}. During the loop iteration, a is reduced by multiples of the previous remainder b until a is smaller than b. Then a is the next remainder r_{k}. Then b is reduced by multiples of a until it is again smaller than a, giving the next remainder r_{k+1}, and so on.
The recursive version^{[21]} is based on the equality of the GCDs of successive remainders and the stopping condition gcd(r_{N−1}, 0) = r_{N−1}.
function gcd(a, b) if b = 0 return a else return gcd(b, a mod b)
For illustration, the gcd(1071, 462) is calculated from the equivalent gcd(462, 1071 mod 462) = gcd(462, 147). The latter GCD is calculated from the gcd(147, 462 mod 147) = gcd(147, 21), which in turn is calculated from the gcd(21, 147 mod 21) = gcd(21, 0) = 21.
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.^{[22]}^{[23]} Previously, the equation
- r_{k−2} = q_{k} r_{k−1} + r_{k}
assumed that |r_{k−1}| > r_{k} > 0. However, an alternative negative remainder e_{k} can be computed:
- r_{k−2} = (q_{k} + 1) r_{k−1} + e_{k}
if r_{k−1} > 0 or
- r_{k−2} = (q_{k} − 1) r_{k−1} + e_{k}
if r_{k−1} < 0.
If r_{k} is replaced by e_{k}. when |e_{k}| < |r_{k}|, then one gets a variant of Euclidean algorithm such that
- |r_{k}| ≤ |r_{k−1}| / 2
at each step.
Leopold Kronecker has shown that this version requires the least number of steps of any version of Euclid's algorithm.^{[22]}^{[23]} More generally, it has been proven that, for every input numbers a and b, the number of steps is minimal if and only if q_{k} is chosen in order that where is the golden ratio.^{[24]}
Historical development
The Euclidean algorithm is one of the oldest algorithms in common use.^{[25]} 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 numbers. 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 a and b corresponds to the greatest length g that measures a and b evenly; in other words, the lengths a and b are both integer multiples of the length g.
The algorithm was probably not discovered by Euclid, who compiled results from earlier mathematicians in his Elements.^{[26]}^{[27]} The mathematician and historian B. L. van der Waerden suggests that Book VII derives from a textbook on number theory written by mathematicians in the school of Pythagoras.^{[28]} The algorithm was probably known by Eudoxus of Cnidus (about 375 BC).^{[25]}^{[29]} The algorithm may even pre-date Eudoxus,^{[30]}^{[31]} judging from the use of the technical term ἀνθυφαίρεσις (anthyphairesis, reciprocal subtraction) in works by Euclid and Aristotle.^{[32]}
Centuries later, Euclid's algorithm was discovered independently both in India and in China,^{[33]} primarily to solve Diophantine equations that arise in astronomy and making accurate calendars. In the late 5th century, the Indian mathematician and astronomer Aryabhata described the algorithm as the "pulverizer",^{[34]} perhaps because of its effectiveness in solving Diophantine equations.^{[35]} Although a special case of the Chinese remainder theorem had already been described by Chinese mathematician and astronomer Sun Tzu,^{[36]} the general solution was published by Qin Jiushao in his 1247 book Shushu Jiuzhang (數書九章 Mathematical Treatise in Nine Sections).^{[37]} The Euclidean algorithm was first described in Europe in the second edition of Bachet's Problèmes plaisants et délectables (Pleasant and enjoyable problems, 1624).^{[34]} 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.^{[38]}
In the 19th century, the Euclidean algorithm led to the development of new number systems, such as Gaussian integers and Eisenstein integers. In 1815, Carl Gauss used the Euclidean algorithm to demonstrate unique factorization of Gaussian integers, although his work was first published in 1832.^{[39]} Gauss mentioned the algorithm in his Disquisitiones Arithmeticae (published 1801), but only as a method for continued fractions.^{[33]} Peter Gustav Lejeune Dirichlet seems to have been the first to describe the Euclidean algorithm as the basis for much of number theory.^{[40]} 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.^{[41]} 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.^{[42]} 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.^{[43]}
"[The Euclidean algorithm] is the granddaddy of all algorithms, because it is the oldest nontrivial algorithm that has survived to the present day." |
Donald Knuth, The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 2nd edition (1981), p. 318. |
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.^{[44]}
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)^{[45]} and the LLL algorithm.^{[46]}^{[47]}
In 1969, Cole and Davie developed a two-player game based on the Euclidean algorithm, called The Game of Euclid,^{[48]} which has an optimal strategy.^{[49]} The players begin with two piles of a and b stones. The players take turns removing m multiples of the smaller pile from the larger. Thus, if the two piles consist of x and y stones, where x is larger than y, the next player can reduce the larger pile from x stones to x − my stones, as long as the latter is a nonnegative integer. The winner is the first player to reduce one pile to zero stones.^{[50]}^{[51]}
Mathematical applications
Bézout's identity
Bézout's identity states that the greatest common divisor g of two integers a and b can be represented as a linear sum of the original two numbers a and b.^{[52]} In other words, it is always possible to find integers s and t such that g = sa + tb.^{[53]}^{[54]}
The integers s and t can be calculated from the quotients q_{0}, q_{1}, etc. by reversing the order of equations in Euclid's algorithm.^{[55]} Beginning with the next-to-last equation, g can be expressed in terms of the quotient q_{N−1} and the two preceding remainders, r_{N−2} and r_{N−3}:
- g = r_{N−1} = r_{N−3} − q_{N−1} r_{N−2} .
Those two remainders can be likewise expressed in terms of their quotients and preceding remainders,
- r_{N−2} = r_{N−4} − q_{N−2} r_{N−3} and
- r_{N−3} = r_{N−5} − q_{N−3} r_{N−4} .
Substituting these formulae for r_{N−2} and r_{N−3} into the first equation yields g as a linear sum of the remainders r_{N−4} and r_{N−5}. The process of substituting remainders by formulae involving their predecessors can be continued until the original numbers a and b are reached:
- r_{2} = r_{0} − q_{2} r_{1}
- r_{1} = b − q_{1} r_{0}
- r_{0} = a − q_{0} b.
After all the remainders r_{0}, r_{1}, etc. have been substituted, the final equation expresses g as a linear sum of a and b: g = sa + tb. Bézout's identity, and therefore the previous algorithm, can both be generalized to the context of Euclidean domains.
Bézout's identity provides yet another definition of the greatest common divisor g of two numbers a and b.^{[10]} Consider the set of all numbers ua + vb, where u and v are any two integers. Since a and b are both divisible by g, every number in the set is divisible by g. In other words, every number of the set is an integer multiple of g. This is true for every common divisor of a and b. However, unlike other common divisors, the greatest common divisor is a member of the set; by Bézout's identity, choosing u = s and v = t gives g. A smaller common divisor cannot be a member of the set, since every member of the set must be divisible by g. Conversely, any multiple m of g can be obtained by choosing u = ms and v = mt, where s and t are the integers of Bézout's identity. This may be seen by multiplying Bézout's identity by m,
- mg = msa + mtb.
Therefore, the set of all numbers ua + vb is equivalent to the set of multiples m of g. In other words, the set of all possible sums of integer multiples of two numbers (a and b) is equivalent to the set of multiples of gcd(a, b). The GCD is said to be the generator of the ideal of a and b. This GCD definition led to the modern abstract algebraic 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.^{[56]} For example, consider two measuring cups of volume a and b. By adding/subtracting u multiples of the first cup and v multiples of the second cup, any volume ua + vb can be measured out. These volumes are all multiples of g = gcd(a, b).
Extended Euclidean algorithm
{{#invoke:main|main}}
The integers s and t of Bézout's identity can be computed efficiently using the extended Euclidean algorithm. This extension adds two recursive equations to Euclid's algorithm^{[57]}
- s_{k} = s_{k−2} − q_{k}s_{k−1}
- t_{k} = t_{k−2} − q_{k}t_{k−1}
with the starting values
- s_{−2} = 1, t_{−2} = 0
- s_{−1} = 0, t_{−1} = 1.
Using this recursion, Bézout's integers s and t are given by s = s_{N} and t = t_{N}, where N+1 is the step on which the algorithm terminates with r_{N+1} = 0.
The validity of this approach can be shown by induction. Assume that the recursion formula is correct up to step k − 1 of the algorithm; in other words, assume that
- r_{j} = s_{j} a + t_{j} b
for all j less than k. The kth step of the algorithm gives the equation
- r_{k} = r_{k−2} − q_{k}r_{k−1}.
Since the recursion formula has been assumed to be correct for r_{k−2} and r_{k−1}, they may be expressed in terms of the corresponding s and t variables
- r_{k} = (s_{k−2} a + t_{k−2} b) − q_{k}(s_{k−1} a + t_{k−1} b).
Rearranging this equation yields the recursion formula for step k, as required
- r_{k} = s_{k} a + t_{k} b = (s_{k−2} − q_{k}s_{k−1}) a + (t_{k−2} − q_{k}t_{k−1}) b.
Matrix method
The integers s and t can also be found using an equivalent matrix method.^{[58]} The sequence of equations of Euclid's algorithm
- a = q_{0} b + r_{0}
- b = q_{1} r_{0} + r_{1}
- …
- r_{N−2} = q_{N} r_{N−1} + 0
can be written as a product of 2-by-2 quotient matrices multiplying a two-dimensional remainder vector
Let M represent the product of all the quotient matrices
This simplifies the Euclidean algorithm to the form
To express g as a linear sum of a and b, both sides of this equation can be multiplied by the inverse of the matrix M.^{[58]}^{[59]} The determinant of M equals (−1)^{N+1}, since it equals the product of the determinants of the quotient matrices, each of which is negative one. Since the determinant of M is never zero, the vector of the final remainders can be solved using the inverse of M
Since the top equation gives
- g = (−1)^{N+1} ( m_{22} a − m_{12} b),
the two integers of Bézout's identity are s = (−1)^{N+1}m_{22} and t = (−1)^{N}m_{12}. 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.^{[60]} To illustrate this, suppose that a number L can be written as a product of two factors u and v, that is, L = uv. If another number w also divides L but is coprime with u, then w must divide v, by the following argument: If the greatest common divisor of u and w is 1, then integers s and t can be found such that
- 1 = su + tw .
by Bézout's identity. Multiplying both sides by v gives the relation
- v = suv + twv = sL + twv .
Since w divides both terms on the right-hand side, it must also divide the left-hand side, v. This result is known as Euclid's lemma.^{[61]} Specifically, if a prime number divides L, then it must divide at least one factor of L. Conversely, if a number w is coprime to each of a series of numbers a_{1}, a_{2}, …, a_{n}, then w is also coprime to their product, a_{1} × a_{2} × … × a_{n}.^{[61]}
Euclid's lemma suffices to prove that every number has a unique factorization into prime numbers.^{[62]} To see this, assume the contrary, that there are two independent factorizations of L into m and n prime factors, respectively
- L = p_{1}p_{2}…p_{m} = q_{1}q_{2}…q_{n} .
Since each prime p divides L by assumption, it must also divide one of the q factors; since each q is prime as well, it must be that p = q. Iteratively dividing by the p factors shows that each p has an equal counterpart q; 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.^{[63]} A typical linear Diophantine equation seeks integers x and y such that^{[64]}
- ax + by = c
where a, b and c are given integers. This can be written as an equation for x in modular arithmetic:
- ax ≡ c mod b.
Let g be the greatest common divisor of a and b. Both terms in ax + by are divisible by g; therefore, c must also be divisible by g, or the equation has no solutions. By dividing both sides by c/g, the equation can be reduced to Bezout's identity
- sa + tb = g
where s and t can be found by the extended Euclidean algorithm.^{[65]} This provides one solution to the Diophantine equation, x_{1} = s (c/g) and y_{1} = t (c/g).
In general, a linear Diophantine equation has no solutions, or an infinite number of solutions.^{[66]} To find the latter, consider two solutions, (x_{1}, y_{1}) and (x_{2}, y_{2}), where
- ax_{1} + by_{1} = c = ax_{2} + by_{2}
or equivalently
- a(x_{1} − x_{2}) = b(y_{2} − y_{1}).
Therefore, the smallest difference between two x solutions is b/g, whereas the smallest difference between two y solutions is a/g. Thus, the solutions may be expressed as
- x = x_{1} − bt/g
- y = y_{1} + at/g.
By allowing t to vary over all possible integers, an infinite family of solutions can be generated from a single solution (x_{1}, y_{1}). If the solutions are required to be positive integers (x > 0, y > 0), only a finite number of solutions may be possible. This restriction on the acceptable solutions allows systems of Diophantine equations to be solved with more unknowns than equations;^{[67]} this is impossible for a system of linear equations when the solutions can be any real number.
Multiplicative inverses and the RSA algorithm
A finite field 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 {0, 1, 2, …, 12} using modular arithmetic. In this field, the results of any mathematical operation (addition, subtraction, multiplication, or division) is reduced modulo 13; that is, multiples of 13 are added or subtracted until the result is brought within the range 0–12. For example, the result of 5 × 7 = 35 mod 13 = 9. Such finite fields can be defined for any prime p; using more sophisticated definitions, they can also be defined for any power m of a prime p^{ m}. Finite fields are often called Galois fields, and are abbreviated as GF(p) or GF(p^{ m}).
In such a field with m numbers, every nonzero element a has a unique modular multiplicative inverse, a^{−1} such that aa^{−1} = a^{−1}a ≡ 1 mod m. This inverse can be found by solving the congruence equation ax ≡ 1 mod m,^{[68]} or the equivalent linear Diophantine equation^{[69]}
- ax + my = 1.
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; specifically, the equation determines the integer used to decrypt the message.^{[70]} Note that 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.^{[71]}
Chinese remainder theorem
Euclid's algorithm can also be used to solve multiple linear Diophantine equations.^{[72]} Such equations arise in the Chinese remainder theorem, 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}:^{[73]}
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
{{#invoke:main|main}} The Euclidean algorithm can be used to arrange the set of all positive rational numbers 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.^{[74]} 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.^{[75]} 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
as can be confirmed by calculation.
Factorization algorithms
Calculating a greatest common divisor is an essential step in several integer factorization algorithms,^{[76]} such as Pollard's rho algorithm,^{[77]} Shor's algorithm,^{[78]} Dixon's factorization method^{[79]} and the Lenstra elliptic curve factorization.^{[80]} The Euclidean algorithm may be used to find this GCD efficiently. Continued fraction factorization uses continued fractions, which are determined using Euclid's algorithm.^{[81]}
Algorithmic efficiency
The computational efficiency of Euclid's algorithm has been studied thoroughly.^{[82]} This efficiency can be described by the number of division steps the algorithm requires, multiplied by the computational expense of each step. The first known analysis of Euclid's algorithm is due to A.-A.-L. Reynaud in 1811,^{[83]} who showed that the number of division steps on input (u, v) is bounded by v; later he improved this to v/2 + 2. Later, in 1841, P.-J.-E. Finck showed^{[84]} that the number of division steps is at most 2 log_{2} v + 1, and hence Euclid's algorithm runs in time polynomial in the size of the input; also see.^{[85]} His analysis was refined by Gabriel Lamé in 1844,^{[86]} who showed that the number of steps required for completion is never more than five times the number h of base-10 digits of the smaller number b.^{[87]}^{[88]}
In the uniform cost model (suitable for analyzing the complexity of gcd calculation on numbers that fit into a single machine word), each step of the algorithm takes constant time, and Lamé's analysis implies that the total running time is also O(h). However, in a model of computation suitable for computation with larger numbers, the computational expense of a single remainder computation in the algorithm can be as large as O(h^{2}).^{[89]} In this case the total time for all of the steps of the algorithm can be analyzed using a telescoping series, showing that it is also O(h^{2}). Modern algorithmic techniques based on the Schönhage–Strassen algorithm for fast integer multiplication can be used to speed this up, leading to subquadratic algorithms for the GCD.^{[90]}^{[91]}
Number of steps
The number of steps to calculate the GCD of two natural numbers, a and b, may be denoted by T(a, b).^{[92]} If g is the GCD of a and b, then a = mg and b = ng for two coprime numbers m and n. Then
- T(a, b) = T(m, n)
as may be seen by dividing all the steps in the Euclidean algorithm by g.^{[93]} By the same argument, the number of steps remains the same if a and b are multiplied by a common factor w: T(a, b) = T(wa, wb). Therefore, the number of steps T may vary dramatically between neighboring pairs of numbers, such as T(a, b) and T(a, b + 1), depending on the size of the two GCDs.
The recursive nature of the Euclidean algorithm gives another equation
- T(a, b) = 1 + T(b, r_{0}) = 2 + T(r_{0}, r_{1}) = … = N + T(r_{N−2}, r_{N−1}) = N + 1
where T(x, 0) = 0 by assumption.^{[92]}
Worst-case
If the Euclidean algorithm requires N steps for a pair of natural numbers a > b > 0, the smallest values of a and b for which this is true are the Fibonacci numbers F_{N+2} and F_{N+1}, respectively.^{[94]} This can be shown by induction.^{[95]} If N = 1, b divides a with no remainder; the smallest natural numbers for which this is true is b = 1 and a = 2, which are F_{2} and F_{3}, respectively. Now assume that the result holds for all values of N up to M − 1. The first step of the M-step algorithm is a = q_{0}b + r_{0}, and the second step is b = q_{1}r_{0} + r_{1}. Since the algorithm is recursive, it required M − 1 steps to find gcd(b, r_{0}) and their smallest values are F_{M+1} and F_{M}. The smallest value of a is therefore when q_{0} = 1, which gives a = b + r_{0} = F_{M+1} + F_{M} = F_{M+2}. This proof, published by Gabriel Lamé in 1844, represents the beginning of computational complexity theory,^{[96]} and also the first practical application of the Fibonacci numbers.^{[94]}
This result suffices to show that the number of steps in Euclid's algorithm can never be more than five times the number of its digits (base 10).^{[97]} For if the algorithm requires N steps, then b is greater than or equal to F_{N+1} which in turn is greater than or equal to φ^{N−1}, where φ is the golden ratio. Since b ≥ φ^{N−1}, then N − 1 ≤ log_{φ}b. Since log_{10}φ > 1/5, (N − 1)/5 < log_{10}φ log_{φ}b = log_{10}b. Thus, N ≤ 5 log_{10}b. Thus, the Euclidean algorithm always needs less than O(h) divisions, where h is the number of digits in the smaller number b.
Average
The average number of steps taken by the Euclidean algorithm has been defined in three different ways. The first definition is the average time T(a) required to calculate the GCD of a given number a and a smaller natural number b chosen with equal probability from the integers 0 to a − 1^{[92]}
However, since T(a, b) fluctuates dramatically with the GCD of the two numbers, the averaged function T(a) is likewise "noisy".^{[98]}
To reduce this noise, a second average τ(a) is taken over all numbers coprime with a
There are φ(a) coprime integers less than a, where φ is Euler's totient function. This tau average grows smoothly with a^{[99]}^{[100]}
with the residual error being of order a^{−(1/6) + ε}, where ε is infinitesimal. The constant C (Porter's Constant^{[101]})in this formula equals
where γ is the Euler–Mascheroni constant and ζ' is the derivative of the Riemann zeta function.^{[102]}^{[103]} The leading coefficient (12/π^{2}) ln 2 was determined by two independent methods.^{[104]}^{[105]}
Since the first average can be calculated from the tau average by summing over the divisors d of a^{[106]}
it can be approximated by the formula^{[107]}
where Λ(d) is the Mangoldt function.^{[108]}
A third average Y(n) is defined as the mean number of steps required when both a and b are chosen randomly (with uniform distribution) from 1 to n^{[107]}
Substituting the approximate formula for T(a) into this equation yields an estimate for Y(n)^{[109]}
Computational expense per step
In each step k of the Euclidean algorithm, the quotient q_{k} and remainder r_{k} are computed for a given pair of integers r_{k−2} and r_{k−1}
- r_{k−2} = q_{k} r_{k−1} + r_{k}.
The computational expense per step is associated chiefly with finding q_{k}, since the remainder r_{k} can be calculated quickly from r_{k−2}, r_{k−1}, and q_{k}
- r_{k} = r_{k−2} − q_{k} r_{k−1}.
The computational expense of dividing h-bit numbers scales as O(h(ℓ+1)), where ℓ is the length of the quotient.^{[89]}
For comparison, Euclid's original subtraction-based algorithm can be much slower. A single integer division is equivalent to the quotient q number of subtractions. If the ratio of a and b is very large, the quotient is large and many subtractions will be required. On the other hand, it has been shown that the quotients are very likely to be small integers. The probability of a given quotient q is approximately ln|u/(u − 1)| where u = (q + 1)^{2}.^{[110]} For illustration, the probability of a quotient of 1, 2, 3, or 4 is roughly 41.5%, 17.0%, 9.3%, and 5.9%, respectively. Since the operation of subtraction is faster than division, particularly for large numbers,^{[111]} the subtraction-based Euclid's algorithm is competitive with the division-based version.^{[112]} This is exploited in the binary version of Euclid's algorithm.^{[113]}
Combining the estimated number of steps with the estimated computational expense per step shows that the Euclid's algorithm grows quadratically (h^{2}) with the average number of digits h in the initial two numbers a and b. Let h_{0}, h_{1}, …, h_{N−1} represent the number of digits in the successive remainders r_{0}, r_{1}, …, r_{N−1}. Since the number of steps N grows linearly with h, the running time is bounded by
Alternative methods
Euclid's algorithm is widely used in practice, especially for small numbers, due to its simplicity.^{[114]} For comparison, the efficiency of alternatives to Euclid's algorithm may be determined.
One inefficient approach to finding the GCD of two natural numbers a and b is to calculate all their common divisors; the GCD is then the largest common divisor. The common divisors can be found by dividing both numbers by successive integers from 2 to the smaller number b. The number of steps of this approach grows linearly with b, or exponentially in the number of digits. Another inefficient approach is to find the prime factors of one or both numbers. As noted above, the GCD equals the product of the prime factors shared by the two numbers a and b.^{[6]} Present methods for prime factorization are also inefficient; many modern cryptography systems even rely on that inefficiency.^{[9]}
The binary GCD algorithm is an efficient alternative that substitutes division with faster operations by exploiting the binary representation used by computers.^{[115]}^{[116]} However, this alternative also scales like O(h²). It is generally faster than the Euclidean algorithm on real computers, even though it scales in the same way.^{[90]} Additional efficiency can be gleaned by examining only the leading digits of the two numbers a and b.^{[117]}^{[118]} The binary algorithm can be extended to other bases (k-ary algorithms),^{[119]} with up to fivefold increases in speed.^{[120]} Lehmer's GCD algorithm uses the same general principle as the binary algorithm to speed up GCD computations in arbitrary bases.
A recursive approach for very large integers (with more than 25,000 digits) leads to subquadratic integer GCD algorithms,^{[121]} such as those of Schönhage,^{[122]}^{[123]} and Stehlé and Zimmermann.^{[124]} These algorithms exploit the 2×2 matrix form of the Euclidean algorithm given above. These subquadratic methods generally scale as O(h (log h)^{2} (log log h)).^{[90]}^{[91]}
Generalizations
As described above, the Euclidean algorithm is used to find the greatest common divisor of two natural numbers (positive integers). However, it may be generalized to the real numbers, and to other mathematical objects, such as polynomials,^{[125]} quadratic integers^{[126]} and Hurwitz quaternions.^{[127]} In the latter cases, the Euclidean algorithm is used to demonstrate the crucial property of unique factorization, i.e., that such numbers can be factored uniquely into irreducible elements, the counterparts of prime numbers. Unique factorization is essential to many proofs of number theory.
Rational and real numbers
Euclid's algorithm can be applied to real numbers, as described by Euclid in Book 10 of his Elements. The goal of the algorithm is to identify a real number g such that two given real numbers, a and b, are integer multiples of it: a = mg and b = ng, where m and n are integers.^{[26]} This identification is equivalent to finding an integer relation among the real numbers a and b; that is, it determines integers s and t such that sa + tb = 0. Euclid uses this algorithm to treat the question of incommensurable lengths.^{[128]}^{[129]}
The real-number Euclidean algorithm differs from its integer counterpart in two respects. First, the remainders r_{k} are real numbers, although the quotients q_{k} are integers as before. Second, the algorithm is not guaranteed to end in a finite number N of steps. If it does, the fraction a/b is a rational number, i.e., the ratio of two integers
- a/b = mg/ng = m/n
and can be written as a finite continued fraction [q_{0}; q_{1}, q_{2}, …, q_{N}]. If the algorithm does not stop, the fraction a/b is an irrational number and can be described by an infinite continued fraction [q_{0}; q_{1}, q_{2}, …].^{[130]} Examples of infinite continued fractions are the golden ratio φ = [1; 1, 1, …] and the square root of two, √2 = [1; 2, 2, …].^{[131]} Generally speaking, the algorithm is unlikely to stop, since almost all ratios a/b of two real numbers are irrational.^{[132]}
An infinite continued fraction may be truncated at a step k [q_{0}; q_{1}, q_{2}, …, q_{k}] to yield an approximation to a/b that improves as k is increased. The approximation is described by convergents m_{k}/n_{k}; the numerator and denominators are coprime and obey the recurrence relation
- m_{k} = q_{k} m_{k−1} + m_{k−2}
- n_{k} = q_{k} n_{k−1} + n_{k−2}
where m_{−1} = n_{−2} = 1 and m_{−2} = n_{−1} = 0 are the initial values of the recursion. The convergent m_{k}/n_{k} is the best rational number approximation to a/b with denominator n_{k}:^{[133]}
Polynomials
{{#invoke:main|main}} Polynomials in a single variable x can be added, multiplied and factored into irreducible polynomials, which are the analogs of the prime numbers for integers. The greatest common divisor polynomial g(x) of two polynomials a(x) and b(x) is defined as the product of their shared irreducible polynomials, which can be identified using the Euclidean algorithm.^{[125]} The basic procedure is similar to integers. At each step k, a quotient polynomial q_{k}(x) and a remainder polynomial r_{k}(x) are identified to satisfy the recursive equation
- r_{k−2}(x) = q_{k}(x) r_{k−1}(x) + r_{k}(x)
where r_{−2}(x) = a(x) and r_{−1}(x) = b(x). The quotient polynomial is chosen so that the leading term of q_{k}(x) r_{k−1}(x) equals the leading term of r_{k−2}(x); this ensures that the degree of each remainder is smaller than the degree of its predecessor deg[r_{k}(x)] < deg[r_{k−1}(x)]. Since the degree is a nonnegative integer, and since it decreases with every step, the Euclidean algorithm concludes in a finite number of steps. The final nonzero remainder is the greatest common divisor of the original two polynomials, a(x) and b(x).^{[134]}
For example, consider the following two quartic polynomials, which each factor into two quadratic polynomials
- a(x) = x^{4} − 4x^{3} + 4 x^{2} − 3x + 14 = (x^{2} − 5x + 7)(x^{2} + x + 2)
and
- b(x) = x^{4} + 8x^{3} + 12x^{2} + 17x + 6 = (x^{2} + 7x + 3)(x^{2} + x + 2).
Dividing a(x) by b(x) yields a remainder r_{0}(x) = x^{3} + (2/3) x^{2} + (5/3) x − (2/3). In the next step, b(x) is divided by r_{0}(x) yielding a remainder r_{1}(x) = x^{2} + x + 2. Finally, dividing r_{0}(x) by r_{1}(x) yields a zero remainder, indicating that r_{1}(x) is the greatest common divisor polynomial of a(x) and b(x), consistent with their factorization.
Many of the applications described above for integers carry over to polynomials.^{[135]} The Euclidean algorithm can be used to solve linear Diophantine equations and Chinese remainder problems for polynomials; continued fractions of polynomials can also be defined.
The polynomial Euclidean algorithm has other applications as well, such as Sturm chains, a method for counting the zeros of a polynomial that lie inside a given real interval.^{[136]} This in turn has applications in several areas, such as the Routh–Hurwitz stability criterion in control theory.^{[137]}
Finally, the coefficients of the polynomials need not be drawn from integers, real numbers or even the complex numbers. For example, the coefficients may be drawn from a general field, such as the finite fields GF(p) described above. The corresponding conclusions about the Euclidean algorithm and its applications hold even for such polynomials.^{[125]}
Gaussian integers
The Gaussian integers are complex numbers of the form α = u + vi, where u and v are ordinary integers and i is the square root of negative one.^{[138]} By defining an analog of the Euclidean algorithm, Gaussian integers can be shown to be uniquely factorizable, by the argument above.^{[39]} This unique factorization is helpful in many applications, such as deriving all Pythagorean triples or proving Fermat's theorem on sums of two squares.^{[138]} In general, the Euclidean algorithm is convenient in such applications, but not essential; for example, the theorems can often be proven by other arguments.
The Euclidean algorithm developed for two Gaussian integers α and β is nearly the same as that for normal integers,^{[139]} but differs in two respects. As before, the task at each step k is to identify a quotient q_{k} and a remainder r_{k} such that
- r_{k} = r_{k−2} − q_{k} r_{k−1}
where r_{k−2} = α, r_{k−1} = β, and every remainder is strictly smaller than its predecessor, |r_{k}| < |r_{k−1}|. The first difference is that the quotients and remainders are themselves Gaussian integers, and thus are complex numbers. The quotients q_{k} are generally found by rounding the real and complex parts of the exact ratio (such as the complex number α/β) to the nearest integers.^{[139]} The second difference lies in the necessity of defining how one complex remainder can be "smaller" than another. To do this, we define a norm function f(u + vi) = u^{2} + v^{2}, which converts every Gaussian integer u + vi into a normal integer. After each step k of the Euclidean algorithm, the norm of the remainder f(r_{k}) is smaller than the norm of the preceding remainder, f(r_{k−1}). Since the norm is a nonnegative integer and decreases with every step, the Euclidean algorithm for Gaussian integers ends in a finite number of steps.^{[140]} The final nonzero remainder is the gcd(α,β), the Gaussian integer of largest norm that divides both α and β; it is unique up to multiplication by a unit, ±1 or ±i.^{[141]}
Many of the other applications of the Euclidean algorithm carry over to Gaussian integers. For example, it can be used to solve linear Diophantine equations and Chinese remainder problems for Gaussian integers;^{[142]} continued fractions of Gaussian integers can also be defined.^{[139]}
Euclidean domains
A set of elements under two binary operations, + and −, is called a Euclidean domain if it forms a commutative ring R and, roughly speaking, if a generalized Euclidean algorithm can be performed on them.^{[143]}^{[144]} The two operations of such a ring need not be the addition and multiplication of ordinary arithmetic; rather, they can be more general, such as the operations of a mathematical group or monoid. Nevertheless, these general operations should respect many of the laws governing ordinary arithmetic, such as commutativity, associativity and distributivity.
The generalized Euclidean algorithm requires a Euclidean function, i.e., a mapping f from R into the set of nonnegative integers such that, for any two nonzero elements a and b in R, there exist q and r in R such that a = qb + r and f(r) < f(b).^{[145]} An example of this mapping is the norm function used to order the Gaussian integers above.^{[146]} The function f can be the magnitude of the number, or the degree of a polynomial.^{[147]} The basic principle is that each step of the algorithm reduces f inexorably; hence, if f can be reduced only a finite number of times, the algorithm must stop in a finite number of steps. This principle relies heavily on the natural well-ordering of the non-negative integers;^{[148]} roughly speaking, this requires that every non-empty set of non-negative integers has a smallest member.
The fundamental theorem of arithmetic applies to any Euclidean domain: Any number from a Euclidean domain can be factored uniquely into irreducible elements. Any Euclidean domain is a unique factorization domain (UFD), although the converse is not true.^{[148]} The Euclidean domains and the UFD's are subclasses of the GCD domains, domains in which a greatest common divisor of two numbers always exists.^{[149]} In other words, a greatest common divisor may exist (for all pairs of elements in a domain), although it may not be possible to find it using a Euclidean algorithm. A Euclidean domain is always a principal ideal domain (PID), an integral domain in which every ideal is a principal ideal.^{[150]} Again, the converse is not true: not every PID is a Euclidean domain.
The unique factorization of Euclidean domains is useful in many applications. For example, the unique factorization of the Gaussian integers is convenient in deriving formulae for all Pythagorean triples and in proving Fermat's theorem on sums of two squares.^{[138]} Unique factorization was also a key element in an attempted proof of Fermat's Last Theorem published in 1847 by Gabriel Lamé, the same mathematician who analyzed the efficiency of Euclid's algorithm, based on a suggestion of Joseph Liouville.^{[151]} Lamé's approach required the unique factorization of numbers of the form x + ωy, where x and y are integers, and ω = e^{2iπ/n} is an nth root of 1, that is, ω^{n} = 1. Although this approach succeeds for some values of n (such as n=3, the Eisenstein integers), in general such numbers do not factor uniquely. This failure of unique factorization in some cyclotomic fields led Ernst Kummer to the concept of ideal numbers and, later, Richard Dedekind to ideals.^{[152]}
Unique factorization of quadratic integers
The quadratic integer rings are helpful to illustrate Euclidean domains. Quadratic integers are generalizations of the Gaussian integers in which the imaginary unit i is replaced by a number ω. Thus, they have the form u + v ω, where u and v are integers and ω has one of two forms, depending on a parameter D. If D does not equal a multiple of four plus one, then
If, however, D does equal a multiple of four plus one, then
If the function f corresponds to a norm function, such as that used to order the Gaussian integers above, then the domain is known as norm-Euclidean. The norm-Euclidean rings of quadratic integers are exactly those where D = −11, −7, −3, −2, −1, 2, 3, 5, 6, 7, 11, 13, 17, 19, 21, 29, 33, 37, 41, 57 or 73.^{[18]}^{[153]} The quadratic integers with D = −1 and −3 are known as the Gaussian integers and Eisenstein integers, respectively.
If f is allowed to be any Euclidean function, then the list of possible D values for which the domain is Euclidean is not yet known.^{[154]} The first example of a Euclidean domain that was not norm-Euclidean (with D = 69) was published in 1994.^{[154]} In 1973, Weinberger proved that a quadratic integer ring with D > 0 is Euclidean if, and only if, it is a principal ideal domain, provided that the generalized Riemann hypothesis holds.^{[126]}
Noncommutative rings
It is also possible to apply the Euclidean algorithm to noncommutative rings such as the set of Hurwitz quaternions.^{[127]} Let α and β represent two elements from such a ring. They have a common right divisor δ if α = ξδ and β = ηδ for some choice of ξ and η in the ring. Similarly, they have a common left divisor if α = δξ and β = δη for some choice of ξ and η in the ring. Since multiplication is not commutative, there are two versions of the Euclidean algorithm, one for right divisors and one for left divisors.^{[127]} Choosing the right divisors, the first step in finding the gcd(α, β) by the Euclidean algorithm can be written
- ρ_{0} = α − ψ_{0}β = (ξ − ψ_{0}η)δ
where ψ_{0} represents the quotient and ρ_{0} the remainder. This equation shows that any common right divisor of α and β is likewise a common divisor of the remainder ρ_{0}. The analogous equation for the left divisors would be
- ρ_{0} = α − βψ_{0} = δ(ξ − ηψ_{0}) .
With either choice, the process is repeated as above until the greatest common right or left divisor is identified. As in the Euclidean domain, the "size" of the remainder ρ_{0} must be strictly smaller than β, and there must be only a finite number of possible sizes for ρ_{0}, so that the algorithm is guaranteed to terminate.^{[155]}
Most of the results for the GCD carry over to noncommutative numbers. For example, Bézout's identity states that the right gcd(α, β) can be expressed as a linear combination of α and β.^{[156]} In other words, there are numbers σ and τ such that
- Γ_{right} = σα + τβ
The analogous identity for the left GCD is nearly the same:
- Γ_{left} = ασ + βτ .
Bézout's identity can be used to solve Diophantine equations. For instance, one of the standard proofs of Lagrange's four-square theorem, that every positive integer can be represented as a sum of four squares, is based on quaternion GCDs in this way.^{[155]}
See also
- Euclidean rhythm, a method for using the Euclidean algorithm to generate musical rhythms
Notes
- a. ^{^} 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.
References
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{6.0} ^{6.1} Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{9.0} ^{9.1} Template:Harvnb
- ↑ ^{10.0} ^{10.1} Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb, p. 320
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ ^{18.0} ^{18.1} Template:Harvnb
- ↑ Template:Harvnb, pp. 319–320
- ↑ Template:Harvnb, pp. 318–319
- ↑ Template:Harvnb
- ↑ ^{22.0} ^{22.1} Template:Harvnb
- ↑ ^{23.0} ^{23.1} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ ^{25.0} ^{25.1} Template:Harvnb, p. 318
- ↑ ^{26.0} ^{26.1} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ ^{33.0} ^{33.1} Template:Harvnb
- ↑ ^{34.0} ^{34.1} Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ ^{39.0} ^{39.1} {{#invoke:Citation/CS1|citation |CitationClass=journal }} Reprinted in {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Richard Dedekind in Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ Weisstein, Eric W., "Integer Relation", MathWorld.
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ ^{58.0} ^{58.1} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ ^{61.0} ^{61.1} Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb, pp. 225–349
- ↑ Template:Harvnb, pp. 369–371
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ Template:Harvnb, pp. 380–384
- ↑ Template:Harvnb, pp. 339–364
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }} As cited by Template:Harvtxt.
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{89.0} ^{89.1} Template:Harvnb, pp. 257–261
- ↑ ^{90.0} ^{90.1} ^{90.2} Template:Harvnb, pp. 77–79, 81–85, 425–431
- ↑ ^{91.0} ^{91.1} {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ ^{92.0} ^{92.1} ^{92.2} Template:Harvnb, p. 344
- ↑ Template:Harvnb
- ↑ ^{94.0} ^{94.1} Template:Harvnb, p. 343
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb, p. 353
- ↑ Template:Harvnb, p. 357
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ Weisstein, Eric W., "Porter's Constant", MathWorld.
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb, p. 354
- ↑ ^{107.0} ^{107.1} {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ Template:Harvnb, p. 355
- ↑ Template:Harvnb, p. 356
- ↑ Template:Harvnb, p. 352
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb, pp. 321–323
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ Template:Harvnb, p. 328
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{125.0} ^{125.1} ^{125.2} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{126.0} ^{126.1} {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ ^{127.0} ^{127.1} ^{127.2} Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}.
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{138.0} ^{138.1} ^{138.2} Template:Harvnb
- ↑ ^{139.0} ^{139.1} ^{139.2} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvtxt, p. 132.
- ↑ Template:Harvtxt, p. 161.
- ↑ ^{148.0} ^{148.1} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ Template:Harvtxt, p. 52.
- ↑ Template:Harvtxt, p. 131.
- ↑ {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ ^{154.0} ^{154.1} {{#invoke:Citation/CS1|citation |CitationClass=journal }}
- ↑ ^{155.0} ^{155.1} {{#invoke:citation/CS1|citation |CitationClass=book }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=book }}
Bibliography
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}. See also Vorlesungen über Zahlentheorie
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
- {{#invoke:citation/CS1|citation
|CitationClass=book }}
External links
- Demonstrations of Euclid's algorithm
- Weisstein, Eric W., "Euclidean Algorithm", MathWorld.
- Euclid's Algorithm at cut-the-knot
- Template:PlanetMath
- The Euclidean Algorithm at MathPages
- Euclid's Game at cut-the-knot
- Music and Euclid's algorithm