Euclidean Algorithm

DOWNLOAD Mathematica Notebook EXPLORE THIS TOPIC IN the MathWorld Classroom

The Euclidean algorithm, also called Euclid's algorithm, is an algorithm for finding the greatest common divisor of two numbers a and b. The algorithm can also be defined for more general rings than just the integers Z. There are even principal rings which are not Euclidean but where the equivalent of the Euclidean algorithm can be defined. The algorithm for rational numbers was given in Book VII of Euclid's Elements. The algorithm for reals appeared in Book X, making it the earliest example of an integer relation algorithm (Ferguson et al. 1999).

The Euclidean algorithm is an example of a P-problem whose time complexity is bounded by a quadratic function of the length of the input values (Bach and Shallit 1996).

Let a=bq+r, then find a number u which divides both a and b (so that a=su and b=tu), then u also divides r since

 r=a-bq=su-qtu=(s-qt)u.
(1)

Similarly, find a number v which divides b and r (so that b=s^'v and r=t^'v), then v divides a since

 a=bq+r=s^'vq+t^'v=(s^'q+t^')v.
(2)

Therefore, every common divisor of a and b is a common divisor of b and r, so the procedure can be iterated as follows:

 q_1=|_a/b_|  a=bq_1+r_1  r_1=a-bq_1 ; q_2=|_b/(r_1)_|  b=q_2r_1+r_2  r_2=b-q_2r_1 ; q_3=|_(r_1)/(r_2)_|  r_1=q_3r_2+r_3  r_3=r_1-q_3r_2 ; q_4=|_(r_2)/(r_3)_|  r_2=q_4r_3+r_4  r_4=r_2-q_4r_3 ; q_n=|_(r_(n-2))/(r_(n-1))_|  r_(n-2)=q_nr_(n-1)+r_n  r_n=r_(n-2) ;    -q_nr_(n-1); q_(n+1)=|_(r_(n-1))/(r_n)_|  r_(n-1)=q_(n+1)r_n+0  r_n=r_(n-1)/q_(n+1)
(3)

For integers, the algorithm terminates when q_(n+1) divides r_(n-1) exactly, at which point r_n corresponds to the greatest common divisor of a and b, GCD(a,b)=r_n. For real numbers, the algorithm yields either an exact relation or an infinite sequence of approximate relations (Ferguson et al. 1999).

An important consequence of the Euclidean algorithm is finding integers x and y such that

 ax+by=GCD(a,b).
(4)

This can be done by starting with the equation for r_n, substituting for r_(n-1) from the previous equation, and working upward through the equations.

Note that the r_i are just remainders, so the algorithm can be easily applied by hand by repeatedly computing remainders of consecutive terms starting with the two numbers of interest (with the larger of the two written first). As an example, consider applying the algorithm to (a,b)=(42,30). This gives 42, 30, 12, 6, 0, so GCD(42,30)=6. Similarly, applying the algorithm to (144, 55) gives 144, 55, 34, 21, 13, 8, 5, 3, 2, 1, 0, so GCD(144,55)=1 and 144 and 55 are relatively prime.

A concise Wolfram Language implementation can be given as follows.

  Remainder[a_, b_] := Mod[a, b]
  Remainder[a_, 0] := 0
  EuclideanAlgorithmGCD[a_, b_] := FixedPointList[
    {Last[#], Remainder @@ #}&, {a, b}][[-3, 1]]

Lamé showed that the number of steps needed to arrive at the greatest common divisor for two numbers less than n is

 steps<=(lnn)/(lnphi)+(lnsqrt(5))/(lnphi)
(5)

where phi is the golden ratio. Numerically, Lamé's expression evaluates to

 steps<=4.785log_(10)n+1.6723,
(6)

which, for n>=1, is always <=5 times the number of digits in the smaller number (Wells 1986, p. 59). As shown by Lamé's theorem, the worst case occurs when the algorithm is applied to two consecutive Fibonacci numbers. Heilbronn showed that the average number of steps is 12ln2/pi^2lnn=0.843lnn for all pairs (n,b) with b<n. Kronecker showed that the shortest application of the algorithm uses least absolute remainders. The quotients obtained are distributed as shown in the following table (Wagon 1991).

quotient%
141.5
217.0
39.3

Let T(m,n) be the number of divisions required to compute GCD(m,n) using the Euclidean algorithm, and define T(m,0)=0 if m>=0. Then the function T(m,n) is given by the recurrence relation

 T(m,n)={1+T(n,m mod n)   for m>=n; 1+T(n,m)   for m<n.
(7)

Tabulating this function for 0<=m<n gives

 0     ; 0 1    ; 0 1 2   ; 0 1 1 2  ; 0 1 2 3 2 ; 0 1 1 1 2 2
(8)

(OEIS A051010). The maximum numbers of steps for a given n=1, 2, 3, ... are 1, 2, 2, 3, 2, 3, 4, 3, 3, 4, 4, 5, ... (OEIS A034883).

EuclideanAlgorithmT

Consider the function

 T(n)=1/nsum_(0<=m<n)T(m,n)
(9)

giving the average number of steps when n is fixed and m chosen at random (Knuth 1998, pp. 344 and 353-357). The first few values of T(n) are 0, 1/2, 1, 1, 8/5, 7/6, 13/7, 7/4, ... (OEIS A051011 and A051012). Norton (1990) showed that

 T(n)=(12ln2)/(pi^2)[lnn-sum_(d|n)(Lambda(d))/d]+C+1/nsum_(d|n)phi(d)O(d^(-1/6+epsilon)),
(10)

where Lambda(d) is the Mangoldt function and C is Porter's constant (Knuth 1998, pp. 355-356).

EuclideanAlgorithmTau

The related function

 tau(n)=1/(phi(n))sum_(0<=m<n; GCD(m,n)=1)T(m,n)
(11)

where phi(n) is the totient function, gives the average number of divisions when n is fixed and m is a random number coprime to n. Porter (1975) showed that

 tau(n)=(12ln2)/(pi^2)lnn+C+O(n^(-1/6)+epsilon)
(12)

(Knuth 1998, pp. 354-355).

Finally, define

 A(N)=1/(N^2)sum_(1<=m<=N; 1<=n<=N)T(m,n),
(13)

as the average number of divisions when m and n are both chosen at random in [1,N] Norton (1990) proved that

 A(N)=(12ln2)/(pi^2)[lnN-1/2+6/(pi^2)zeta^'(2)]+C-1/2+O(N^(-1/6+epsilon)),
(14)

where zeta^'(z) is the derivative of the Riemann zeta function.

There exist 21 quadratic fields in which there is a Euclidean algorithm (Inkeri 1947, Barnes and Swinnerton-Dyer 1952).

For additional details, see Uspensky and Heaslet (1939) and Knuth (1998).

Although various attempts were made to generalize the algorithm to find integer relations between n>=3 variables, none were successful until the discovery of the Ferguson-Forcade algorithm (Ferguson et al. 1999). Several other integer relation algorithms have now been discovered.

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.