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

Table of contents |

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*).

## Properties

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

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

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*.

- gcd(
*a*, lcm(*b*,*c*)) = lcm(gcd(*a*,*b*), gcd(*a*,*c*)) - lcm(
*a*, gcd(*b*,*c*)) = gcd(lcm(*a*,*b*), lcm(*a*,*c*)).

*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.