In number theory, the Euclidean algorithm (also called Euclid's algorithm) is an algorithm to 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 Euclid and 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
Given two 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 number 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)
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
Main article: 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:
a b Explanations
gcd( 1071, 1029) The initial arguments
= gcd( 1029, 42) The second argument is 1071 mod 1029
= gcd( 42, 21) The second argument is 1029 mod 42
= gcd( 21, 0) The second argument is 42 mod 21
= 21 Since b=0, we return a
With the iterative algorithm:
a b Explanation
1071 1029 Step 1: The initial inputs
1029 42 Step 2: The remainder of 1071 divided by 1029 is 42, which is put on the right, and the divisor 1029 is put on the left.
42 21 Step 3: We repeat the loop, dividing 1029 by 42, and get 21 as remainder.
21 0 Step 4: Repeat the loop again, since 42 is divisible by 21, we get 0 as remainder, and the algorithm terminates. The number on the left, that is 21, is the gcd as required.
Observe that a ≥ b in each call. If initially, b > a, there is no problem; the first iteration effectively swaps the two values.
Proof
Suppose a and b are the natural numbers whose gcd has to be determined. Now, suppose b > 0, and the remainder of the division of a by b is r. Therefore a = qb + r where q is the quotient of the division.
Any common divisor of a and b is also a divisor of r. To see why this is true, consider that r can be written as r = a − qb. Now, if there is a common divisor d of a and b such that a = sd and b = td, then r = (s−qt)d. Since all these numbers, including s−qt, are whole numbers, it can be seen that r is divisible by d.
The above analysis is true for any divisor d; thus, the greatest common divisor of a and b is also the greatest common divisor of b and r. Therefore it is enough if we continue searching for the greatest common divisor with the numbers b and r. Since r is smaller in absolute value than b, we will reach r = 0 after finitely many steps.
Reference:
http://en.wikipedia.org/wiki/Euclidean_Algorithm