TOPICS
Search

Greatest Common Divisor


The greatest common divisor, sometimes also called the highest common divisor (Hardy and Wright 1979, p. 20), of two positive integers a and b is the largest divisor common to a and b. For example, GCD(3,5)=1, GCD(12,60)=12, and GCD(12,90)=6. The greatest common divisor GCD(a,b,c,...) can also be defined for three or more positive integers as the largest divisor shared by all of them. Two or more positive integers that have greatest common divisor 1 are said to be relatively prime to one another, often simply just referred to as being "relatively prime."

Various notational conventions are summarized in the following table.

notationsource
GCD(a,b)this work, Zwillinger (1996, p. 91), Råde and Westergren (2004, p. 54)
gcd(a,b)Gellert et al. (1989, p. 25), D'Angelo and West (1990, p. 13), Graham et al. (1990, p. 103), Bressoud and Wagon (2000, p. 7), Yan (2002, p. 30), Bronshtein et al. (2007, pp. 323-324), Wolfram Language
g.c.d.(a,b)Andrews 1994, p. 22
(a,b)

The greatest common divisor of a, b, ... is implemented in the Wolfram Language as GCD[a, b, ...].

GreatestCommonDivisor

The plot above shows GCD(1,b) with rational b=m/n. Here, GCD(r_1,r_2,...) is the greatest rational number r for which all the r_i/r are integers. It is easy to see that if r_i=p_i/q_i, where GCD(p_i,q_i)=1, then GCD(r_1,r_2,...)=GCD(p_1,p_2,...)/LCM(q_1,q_2,...). Furthermore, if GCD(1,b) is extended by setting it equal to 0 if b is irrational, the resulting function is continuous at the irrationals, discontinuous at the rationals, and has Riemann integral equal to 0 over any finite interval.

GCDArray

The above plots show a number of visualizations of GCD(i,j) in the (i,j)-plane. The figure on the left is simply GCD(i,j), the figure in the middle is the absolute values of the two-dimensional discrete Fourier transform of GCD(i,j) (Trott 2004, pp. 25-26), and the figure at right is the absolute value of the transform of 1/GCD(i,j).

If d is the greatest common divisor of a and b, then d is the largest possible integer satisfying

a=dx
(1)
b=dy,
(2)

with x and y positive integers.

The Euclidean algorithm can be used to find the greatest common divisor of two integers and to find integers x and y such that

 ax+by=d.
(3)

The notion can also be generalized to more general rings than simply the integers Z. However, even for Euclidean rings, the notion of GCD of two elements of a ring is not the same as the GCD of two ideals of a ring. This is sometimes a source of confusion when studying rings other than Z, such as polynomial rings in several variables.

To compute the GCD, write the prime factorizations of a and b,

a=product_(i)p_i^(alpha_i)
(4)
b=product_(i)p_i^(beta_i),
(5)

where the p_is are all prime factors of a and b, and if p_i does not occur in one factorization, then the corresponding exponent is taken as 0. Then the greatest common divisor GCD(a,b) is given by

 GCD(a,b)=product_(i)p_i^(min(alpha_i,beta_i)),
(6)

where min denotes the minimum. For example, consider GCD(12,30).

12=2^2·3^1·5^0
(7)
30=2^1·3^1·5^1,
(8)

so

 GCD(12,30)=2^1·3^1·5^0=6.
(9)

The GCD is distributive

 GCD(ma,mb)=mGCD(a,b)
(10)
 GCD(ma,mb,mc)=mGCD(a,b,c),
(11)

and associative

GCD(a,b,c)=GCD(GCD(a,b),c)
(12)
=GCD(a,GCD(b,c))
(13)
GCD(ab,cd)=GCD(a,c)GCD(b,d)×GCD(a/(GCD(a,c)),d/(GCD(b,d)))×GCD(c/(GCD(a,c)),b/(GCD(b,d))).
(14)

If a=a_1GCD(a,b) and b=b_1GCD(a,b), then

GCD(a,b)=GCD(a_1GCD(a,b),b_1GCD(a,b))
(15)
=GCD(a,b)GCD(a_1,b_1),
(16)

so GCD(a_1,b_1)=1. The GCD is also idempotent

 GCD(a,a)=a,
(17)

commutative

 GCD(a,b)=GCD(b,a),
(18)

and satisfies the absorption law

 LCM(a,GCD(a,b))=a.
(19)
GCDRecurrence

A recurrence equation that converges to GCD(a,b) for positive odd a and b is given by

 x_n=(x_(n-1)+x_(n-2))/(2^(gde(x_(n-1)+x_(n-2),2)))
(20)

with x_1=a and x_2=b, where gde(n,b) is the greatest dividing exponent of b in n (Stehlé and Zimmerman 2004). The plot above shows the number of iterations required to converge for odd 1<=a,b<=199.

The probability that two integers picked at random are relatively prime is [zeta(2)]^(-1)=6/pi^2, where zeta(z) is the Riemann zeta function. Polezzi (1997) observed that GCD(m,n)=k, where k is the number of lattice points in the plane on the straight line connecting the vectors (0, 0) and (m,n) (excluding (m,n) itself). This observation is intimately connected with the probability of obtaining relatively prime integers, and also with the geometric interpretation of a reduced fraction y/x as a string through a lattice of points with ends at (1,0) and (x,y). The pegs it presses against (x_i,y_i) give alternate convergents y_i/x_i of the continued fraction for y/x, while the other convergents are obtained from the pegs it presses against with the initial end at (0, 1).

Knuth showed that

 GCD(2^p-1,2^q-1)=2^(GCD(p,q))-1.
(21)

See also

Bézout Numbers, Bézout's Theorem, Dirichlet Function, Euclid's Orchard, Euclidean Algorithm, Extended Greatest Common Divisor, Gauss's Lemma, Greatest Common Divisor Theorem, Half-GCD, Least Common Multiple, Least Prime Factor, Orchard-Planting Problem, Relatively Prime, Star of David Theorem Explore this topic in the MathWorld classroom

Related Wolfram sites

http://functions.wolfram.com/IntegerFunctions/GCD/

Explore with Wolfram|Alpha

References

Andrews, G. E. Number Theory. New York: Dover, 1994.Bressoud, D. M. and Wagon, S. A Course in Computational Number Theory. London: Springer-Verlag, 2000.Bronshtein, I. N.; Semendyayev, K. A.; Musiol, G.; and Muehlig, H. Handbook of Mathematics, 5th ed. Berlin: Springer-Verlag, 2007.D'Angelo, J. P. and West, D. B. Mathematical Thinking: Problem-Solving and Proofs, 2nd ed. Upper Saddle River, NJ: Prentice-Hall, 2000.Gellert, W.; Gottwald, S.; Hellwich, M.; Kästner, H.; and Künstner, H. (Eds.). VNR Concise Encyclopedia of Mathematics, 2nd ed. New York: Van Nostrand Reinhold, 1989.Graham, R. L.; Knuth, D. E.; and Patashnik, O. Concrete Mathematics: A Foundation for Computer Science. Reading, MA: Addison-Wesley, 1990.Nagell, T. "Least Common Multiple and Greatest Common Divisor." §5 in Introduction to Number Theory. New York: Wiley, pp. 16-19, 1951.Polezzi, M. "A Geometrical Method for Finding an Explicit Formula for the Greatest Common Divisor." Amer. Math. Monthly 104, 445-446, 1997.Råde, L. and Westergren, B. Mathematics Handbook for Science and Engineering. Berlin: Springer-Verlag, 2004.Séroul, R. "The Greatest Common Divisor." §2.4 in Programming for Mathematicians. Berlin: Springer-Verlag, pp. 9-11, 2000.Stehlé, D. and Zimmerman, P. "A Binary Recursive GCD Algorithm." In Algorithmic Number Theory. Proceedings of the 6th International Symposium (ANTS-VI) held at the University of Vermont, Burlington, VT, June 13-18, 2004 (Ed. D. Buell). Berlin: Springer-Verlag, pp. 411-425, 2004.Trott, M. The Mathematica GuideBook for Programming. New York: Springer-Verlag, pp. 25-26, 2004. http://www.mathematicaguidebooks.org/.Yan, S. Y. Number Theory for Computing, 2nd ed. Berlin: Springer, 2002.Zwillinger, D. (Ed.). "Greatest Common Divisor." §2.3.5 in CRC Standard Mathematical Tables and Formulae, 30th ed. Boca Raton, FL: CRC Press, p. 91, 1996.

Referenced on Wolfram|Alpha

Greatest Common Divisor

Cite this as:

Weisstein, Eric W. "Greatest Common Divisor." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/GreatestCommonDivisor.html

Subject classifications