- Euclidean algorithm
In
number theory , the Euclidean algorithm (also called Euclid's algorithm) is analgorithm to determine thegreatest common divisor (GCD) of two elements of anyEuclidean domain (for example, theintegers ). Its major significance is that it does not require factoring the two integers, and it is also significant in that it is one of the oldest algorithms known, dating back to theancient Greeks .History of the Euclidean algorithm
The Euclidean algorithm is one of the oldest algorithms known, since it appeared in Euclid's "Elements" around
300 BC (7th book, Proposition 2). Euclid originally formulated the problem geometrically, as the problem of finding the greatest common "measure" for two line lengths (a line that could be used to measure both lines without a remainder), and his algorithm proceeded by repeated subtraction of the shorter from the longer segment. However, the algorithm was probably not discovered byEuclid and it may have been known up to 200 years earlier. It was almost certainly known byEudoxus of Cnidus (about 375 BC), andAristotle (about 330 BC) hinted at it in his "Topics", 158b, 29–35.Description of the algorithm
Given two
natural number s "a" and "b", not both equal to zero: check if "b" is zero; if yes, "a" is the gcd. If not, repeat the process using, respectively, "b", and the remainder after dividing "a" by "b". The remainder after dividing "a" by "b" is usually written as "a" mod "b".These algorithms can be used in any context where division with remainder is possible. This includes rings of polynomials over a field as well as the ring of
Gaussian integer s, and in general allEuclidean domain s. Applying the algorithm to the more general case other than natural numbers will be discussed in more detail later in the article.Using recursion
Using
recursion , the algorithm can be expressed: function gcd(a, b) if b = 0 return a else return gcd(b, a mod b)or in C/C++ as
Using iteration
An efficient, iterative method, for compilers that don't optimize
tail recursion :function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a
The extended Euclidean algorithm
By keeping track of the quotients occurring during the algorithm, one can also determine integers "p" and "q" with "ap" + "bq" = gcd("a", "b").This is known as the extended Euclidean algorithm.
Original algorithm
The original algorithm as described by Euclid treated the problem geometrically, using repeated subtraction rather than mod (remainder).
function gcd(a, b) if a = 0 return b while b ≠ 0 if a > b a := a − b else b := b − a return a
An example
As an example, consider computing the gcd of 1071 and 1029, which is 21.Recall that “mod” means “the remainder after dividing.”
With the recursive algorithm:
This agrees with the explicit factorization. For general Euclidean domains, the proof of correctness is by induction on some size function. For the integers, this size function is just the identity. For rings of polynomials over a field, it is the degree of the polynomial (note that each step in the above table reduces the degree by at least one).
ee also
*
Least common multiple
*Extended Euclidean algorithm
*Binary GCD algorithm
*Lehmer's GCD algorithm References
*
Donald Knuth . "The Art of Computer Programming ", Volume 2: "Seminumerical Algorithms", Third Edition. Addison-Wesley, 1997. ISBN 0-201-89684-2. Sections 4.5.2–4.5.3, pp.333–379.
*Thomas H. Cormen ,Charles E. Leiserson ,Ronald L. Rivest , andClifford Stein . "Introduction to Algorithms ", Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 31.2: Greatest common divisor, pp.856–862.
*Clark Kimberling . "A Visual Euclidean Algorithm," "Mathematics Teacher" 76 (1983) 108-109.External links
* [http://www.cut-the-knot.org/blue/Euclid.shtml Euclid's Algorithm] at
cut-the-knot
* [http://www.cut-the-knot.org/blue/binary.shtml Binary Euclid's Algorithm (Java)] atcut-the-knot
* [http://www.cut-the-knot.org/blue/EuclidAlg.shtml Euclid's Game (Java)] atcut-the-knot
*MathWorld | urlname=EuclideanAlgorithm | title=Euclidean Algorithm
*MathWorld | urlname=LamesTheorem | title=Lamé's Theorem
*PlanetMath | urlname=EuclidsAlgorithm | title=Euclid's algorithm
* [http://plus.maths.org/issue40/features/wardhaugh/index.html Music and Euclid's algorithm]
* [http://www.mathpages.com/home/kmath384.htm The Euclidean Algorithm] at MathPages
* [http://www.math.sc.edu/~sumner/numbertheory/euclidean/euclidean.html Implementation] in Javascript
* [http://www.sharpdeveloper.net/content/articles/dot-net-data-structures-and-algorithms.aspx .NET Implementation of Euclidean algorithm]
Wikimedia Foundation. 2010.