- Euclidean algorithm
number theory, the Euclidean algorithm (also called Euclid's algorithm) is an algorithmto determine the greatest common divisor(GCD) of two elements of any Euclidean domain(for example, the integers). 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 the ancient 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 by Euclidand it may have been known up to 200 years earlier. It was almost certainly known by Eudoxus of Cnidus(about 375 BC), and Aristotle(about 330 BC) hinted at it in his "Topics", 158b, 29–35.
Description of the algorithm
natural numbers "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 integers, and in general all Euclidean domains. Applying the algorithm to the more general case other than natural numbers will be discussed in more detail later in the article.
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
An efficient, iterative method, for compilers that don't optimize
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.
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
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).
Least common multiple
Extended Euclidean algorithm
Binary GCD algorithm
Lehmer's GCD algorithm
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, and Clifford 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.
* [http://www.cut-the-knot.org/blue/Euclid.shtml Euclid's Algorithm] at
* [http://www.cut-the-knot.org/blue/binary.shtml Binary Euclid's Algorithm (Java)] at
* [http://www.cut-the-knot.org/blue/EuclidAlg.shtml Euclid's Game (Java)] at
*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.sharpdeveloper.net/content/articles/dot-net-data-structures-and-algorithms.aspx .NET Implementation of Euclidean algorithm]
Wikimedia Foundation. 2010.