Main Page | See live article | Alphabetical index

Greatest common divisor

In mathematics, the greatest common divisor (abbreviated GCD), or highest common factor (HCF) of two integers which are not both zero is the largest integer that divides both numbers.

The GCD of a and b is often written as gcd(a,b) or simply (a,b). For example, gcd(12,18) = 6, gcd(-4,14) = 2 and gcd(5,0) = 5. Two numbers are called coprime or relatively prime if their greatest common divisor equals 1. For example, 9 and 28 are relatively prime.

The greatest common divisor is useful for writing fractions in lowest terms. Consider for instance

42/56 = 3/4
where we cancelled 14, the greatest common divisor of 42 and 56.

Table of contents
1 Calculating the GCD
2 Properties
3 GCD in commutative rings

Calculating the GCD

While the GCD of two numbers can in principle be computed by determining the prime factorizations of the two numbers and comparing factors, this is never done in practice, because it is too slow. A much more efficient method is the Euclidean algorithm. An extended version of this algorithm can also compute integers p and q such that ap + bq = gcd(a, b).


Every common divisor of a and b divides the GCD of a and b.

The greatest common divisor of a and b (not both zero) may be defined alternatively and equivalently as follows: it is the smallest positive integer d which can be written in the form ap+bq where p and q are integers.

If d is the GCD of a and b, and a divides the product bc, then a/d divides c.

If m is any integer, then gcd(ma,mb) = m gcd(a,b) and gcd(a+mb,b) = gcd(a,b). If m is a nonzero common divisor of a and b, then gcd(a/m,b/m) = gcd(a,b)/m.

The GCD of three numbers can be computed as gcd(a,b,c) = gcd(gcd(a,b),c) = gcd(a, gcd(b, c)).

The GCD of a and b is closely related to their least common multiple lcm(a, b): we have

gcd(a, b) × lcm(a, b) = ab.
Furthermore, the following versions of distributivity hold true:
gcd(a, lcm(b, c)) = lcm(gcd(a, b), gcd(a, c))
lcm(a, gcd(b, c)) = gcd(lcm(a, b), lcm(a, c)).

Geometrically, gcd(a,b) is the number of points with integral coordinates on the straight line joining the points (0,0) and (a,b), excluding (0,0).

GCD in commutative rings

The greatest common divisor can more generally be defined for elements of an arbitrary commutative ring.

If R is a commutative ring, and a and b are in R, then an element of c of R is called a common divisor of a and b if it divides both a and b (that is, if there are elements x and y in R such that cx = a and cy = b). If c is a common divisor of a and b, and every common divisor of a and b divides c, then c is called a greatest common divisor of a and b.

Note that the GCD of a and b need not be unique, but if R is an integral domain then any two GCDs of a and b must be associate elements. Also, a and b need not have a GCD at all unless R is a unique factorization domain. If R is a Euclidean domain then a form of the Euclidean algorithm can be used to compute the GCD.