Euclidean algorithm

This is an old revision of this page, as edited by 195.194.119.220 (talk) at 15:28, 29 September 2006 (Relation with continued fractions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
This article is not about Euclidean geometry.

In number theory, the Euclidean algorithm (also called Euclid's algorithm) is an algorithm to determine the greatest common divisor (GCD) of two integers or elements of any Euclidean ___domain (for example, polynomials over a field) by repeatedly dividing the two numbers and the remainder in turns. 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. Euclid originally formulated the problem geometrically, as the problem of finding a common "measure" for two line lengths, 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.

this boring and i dont like to do this on my course



This is boring i hate this stuff lol

Running time

 
Plot of the running time for gcd(x,y). Red indicates a fast computation, while successively bluer points indicate slower computations

When analyzing the running time of Euclid's algorithm, it turns out that the inputs requiring the most divisions are two successive Fibonacci numbers (because their ratios are the convergents in the slowest continued fraction expansion to converge, that of the golden ratio), and the worst case requires O(n) divisions, where n is the number of digits in the input. However, the divisions themselves are not constant time operations; the actual time complexity of the algorithm is  . The reason is that division of two n-bit numbers takes time  , where m is the length of the quotient. Consider the computation of gcd(a,b) where a and b have at most n bits, let   be the sequence of numbers produced by the algorithm, and let   be their lengths. Then  , and the running time is bounded by

 

This is considerably better than Euclid's original algorithm, in which the modulus operation is effectively performed using repeated subtraction in   steps. Consequently, that version of the algorithm requires   time for n-digit numbers, or   time for the number m.

Euclid's algorithm is widely used in practice, especially for small numbers, due to its simplicity. An alternative algorithm, the binary GCD algorithm, exploits the binary representation used by computers to avoid divisions and thereby increase efficiency, although it too is O(n²); it merely shrinks the constant hidden by the big-O notation on many real machines.


boring boring broing lol

boring lol

boring

ummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm boring

PHP/JavaScript implementation

PHP

function gcd($a, $b) {
  if ($b == 0) return $a;
  else return gcd($b, $a % $b);
}

JavaScript

function gcd(var a, var b) {
  if (b == 0) return a;
  else return gcd(b, a % b);
}


See also

References

  • Euclid's Algorithm at cut-the-knot
  • Binary Euclid's Algorithm (Java) at cut-the-knot
  • Euclid's Game (Java) at cut-the-knot
  • Weisstein, Eric W. "Euclidean Algorithm". MathWorld.
  • Euclid's algorithm at PlanetMath.