See How Things Work

The greatest common divisor

Unit
Factors, Multiples & Primes
Part
3. GCD & LCM
Lesson
3.1

The greatest common divisor of two whole numbers is the largest number that divides both: gcd(18, 30) = 6. This lesson defines common divisors and the gcd, finds it from lists of divisors, settles the cases with 0 and 1, and defines relatively prime numbers, whose only common divisor is 1.

Common divisors

The lesson “Factors and multiples” found all the divisors of a number from its factor pairs. Two numbers can then be compared: some divisors belong to both.

Definition (Common divisor).

Let a and b be whole numbers. A whole number d is a common divisor of a and b if d | a and d | b.⁠[1]

The number 1 divides every number, so 1 is a common divisor of any two numbers.⁠[2] When a and b are both 0, every whole number is a common divisor, since every number divides 0.⁠[3] But when at least one of them is not 0, there are only finitely many common divisors, and among them there is a largest one.⁠[4]

Proposition .

Let a ≥ 1, and let b be any whole number. Every common divisor d of a and b satisfies 1 ≤ d ≤ a. So a and b have finitely many common divisors, and 1 is one of them.

Proof.

d divides a ≥ 1, so by the Proposition on the size of a factor in the lesson “Factors and multiples”, 1 ≤ d ≤ a. Only the numbers 1, 2, …, a are candidates, and 1 divides both a and b.

Example (The common divisors of 18 and 30).

Find all the common divisors of 18 and 30.

  1. Divisors of 18, from the factor pairs 1 × 18, 2 × 9, 3 × 6: 1, 2, 3, 6, 9, 18.
  2. Divisors of 30, from 1 × 30, 2 × 15, 3 × 10, 5 × 6: 1, 2, 3, 5, 6, 10, 15, 30.
  3. The numbers in both lists: 1, 2, 3, 6.

Answer: the common divisors are 1, 2, 3 and 6. The largest of them is 6. Check: 18 = 3 × 6 and 30 = 5 × 6.

divisorsof 18:divisorsof 30:123691812356101530common: 1, 2, 3, 6; greatest: 6
Figure. The divisors of 18 (top) and of 30 (bottom). The four common divisors 1, 2, 3 and 6 are green and joined to each other; the greatest, 6, is ringed. 9 and 18 divide only 18; 5, 10, 15 and 30 divide only 30.

The greatest common divisor

Definition (Greatest common divisor).

Let a and b be whole numbers, not both 0. The greatest common divisor of a and b is the largest whole number that divides both a and b. It is written gcd(a, b).⁠[5]

So gcd(18, 30) = 6. The definition does not depend on the order of the two numbers, so gcd(a, b) = gcd(b, a). The same number is also called the greatest common factor, abbreviated GCF,⁠[6] and Euclid called it the greatest common measure of two numbers.⁠[7]

The definition gives a method: list the common divisors of the two numbers and take the largest.⁠[8] A shorter search follows from it. For a ≥ 1, every common divisor is a divisor of a; so gcd(a, b) is the largest divisor of a that also divides b. Test the divisors of the smaller number from the largest down, dividing the other number by each (lesson “Division with remainder”); the first one that leaves remainder 0 is the gcd.

Example (A larger pair: 72 and 108).

Find gcd(72, 108).

  1. The factor pairs of 72 are 1 × 72, 2 × 36, 3 × 24, 4 × 18, 6 × 12 and 8 × 9. Its divisors, from the largest down: 72, 36, 24, 18, 12, 9, 8, ….
  2. 72: 108 = 1 × 72 + 36, remainder 36. Not a divisor of 108.
  3. 36: 108 = 3 × 36, remainder 0. So 36 divides both numbers, and no larger divisor of 72 does.

Answer: gcd(72, 108) = 36. Check by full lists: the divisors of 108 are 1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54, 108, and the common divisors are 1, 2, 3, 4, 6, 9, 12, 18, 36, the largest being 36.

Example (A common mistake: a common multiple instead of a common divisor).

Someone says: “gcd(12, 18) = 36, because 36 is the greatest number that both go into.” Find the error.

  1. 12 and 18 divide 36: 36 is a common multiple of 12 and 18, not a common divisor. And there is no greatest common multiple: 72, 108, … are common multiples too.
  2. The gcd must divide both numbers, so it is at most 12. Divisors of 12 from the largest down: 12 (18 = 1 × 12 + 6, no), 6 (18 = 3 × 6, yes).

Answer: gcd(12, 18) = 6. “Divides both” and “divisible by both” are different conditions; the second leads to the least common multiple, in the lesson “The least common multiple”.

Looking ahead.

In the examples, every common divisor divides the gcd: 1, 2 and 3 divide 6, and 1, 2, 3, 4, 6, 9, 12 and 18 divide 36. This is always true; Euclid states it as a corollary: “If a number measures two numbers, then it also measures their greatest common measure.”⁠[9] It is proved in the next two lessons, “Finding the GCD from prime factorizations” and “The Euclidean algorithm”.

Zero, one and divisors of each other

The definition allows 0 and 1. Their cases follow directly from it.

Proposition .

For every whole number a ≥ 1: gcd(a, 0) = a, gcd(a, a) = a and gcd(a, 1) = 1.

Proof.

Every number divides 0, so the common divisors of a and 0 are the divisors of a; the largest is a. The common divisors of a and a are again the divisors of a. The only divisor of 1 is 1, so 1 is the only common divisor of a and 1.

The first of them, gcd(a, 0) = a, is where the Euclidean algorithm ends, in the lesson “The Euclidean algorithm”.⁠[10]

Convention.

gcd(0, 0) is not defined. Every whole number divides both 0 and 0, so there is no greatest common divisor. That is why the definition requires a and b not both 0, as Rosen’s does.⁠[5][3]

Proposition (when one divides the other).

If a ≥ 1 and a | b, then gcd(a, b) = a.

Proof.

a | a and a | b, so a is a common divisor. Every common divisor divides a ≥ 1, so it is at most a. Hence a is the greatest.

Example (Edge cases).

Find gcd(15, 0), gcd(1, 97), gcd(13, 91) and gcd(0, 0).

  1. gcd(15, 0) = 15: every divisor of 15 divides 0, and the largest divisor of 15 is 15.
  2. gcd(1, 97) = 1: the only divisor of 1 is 1.
  3. 91 = 7 × 13, so 13 | 91, and by the Proposition gcd(13, 91) = 13.
  4. gcd(0, 0): every whole number divides 0, so there is no greatest common divisor. It is undefined.

Answer: 15, 1, 13, and undefined.

Relatively prime numbers

The smallest possible gcd is 1. That case has its own name.

Definition (Relatively prime).

Two whole numbers a and b, not both 0, are relatively prime if gcd(a, b) = 1, that is, if their only common divisor is 1.⁠[11]

Relatively prime numbers are also said to be prime to each other,⁠[12] or coprime.⁠[13] Euclid’s definition reads: “Numbers relatively prime are those which are measured by a unit alone as a common measure.”⁠[14]

Caution.

Relatively prime is a property of a pair of numbers, and neither number needs to be prime.⁠[15] For example, 8 and 15 are both composite, but their only common divisor is 1.

Proposition .
  1. Two different primes are relatively prime.
  2. A prime p and a whole number a are relatively prime exactly when p ∤ a; if p | a, then gcd(p, a) = p.
  3. Two consecutive whole numbers n and n + 1 are relatively prime.
Proof.

(2) The divisors of p are 1 and p, so gcd(p, a) is 1 or p, and it is p exactly when p | a. (1) If p and q are different primes, then p ∤ q, since the only divisors of q are 1 and q (lesson “Prime and composite numbers”); by (2), gcd(p, q) = 1.

(3) Let d be a common divisor of n and n + 1. By the rule on differences of the lesson “Divisibility”, d divides (n + 1) − n = 1, so d = 1.

Part (2) is Proposition 29 of Book VII of the Elements: “Any prime number is relatively prime to any number which it does not measure.”⁠[16] Part (3) is a standard exercise.⁠[17]

Example (Which pairs are relatively prime?).

Decide for 8 and 15; 21 and 35; 391 and 392; 13 and 52.

  1. 8: divisors 1, 2, 4, 8. 15: 1, 3, 5, 15. Only 1 is common: gcd(8, 15) = 1. Relatively prime, though neither is prime.
  2. 21 = 3 × 7 and 35 = 5 × 7: 7 is a common divisor, so gcd(21, 35) ≥ 7. Not relatively prime. In fact gcd(21, 35) = 7: the divisors of 21 from the top are 21 and 7, and 21 ∤ 35 (35 = 1 × 21 + 14), while 7 | 35.
  3. 391 and 392 are consecutive: relatively prime, by part (3).
  4. 13 is prime and 52 = 4 × 13: gcd(13, 52) = 13 by part (2). Not relatively prime.

Answer: 8 and 15, and 391 and 392, are relatively prime; the other two pairs are not.

Looking ahead.

The lesson “Divisibility rules and why they work” combined the tests for 2 and 3 into a test for 6, and noted that tests cannot always be combined: 4 | 12 and 6 | 12, but 24 ∤ 12. The condition is that the two divisors are relatively prime: if a | c and b | c with gcd(a, b) = 1, then a × b | c.⁠[18] This is proved in the next lesson, “Finding the GCD from prime factorizations”.

A picture: square tiles

A rectangular floor w units long and h units wide (w and h whole numbers) is to be covered with equal square tiles of side d, laid edge to edge in rows starting from one corner, without cutting a tile. Along the length, whole tiles cover a multiple of d; they reach the end exactly when w is a multiple of d. The same holds for the width. So:

Proposition .

Square tiles of side d cover a w by h floor exactly, in this way, if and only if d is a common divisor of w and h. The largest such tile has side gcd(w, h).

18126 by 6: fits18124 by 4: does not fit
Figure. A floor 18 units long and 12 units wide. Square tiles of side 6 cover it exactly, 3 along the length and 2 along the width, because 6 divides both 18 and 12. Tiles of side 4 do not: 12 = 3 × 4, but 18 = 4 × 4 + 2, so a strip 2 units wide stays uncovered (hatched). The largest tile that fits has side gcd(18, 12) = 6.
Game

The largest square tile

Round
1
Score
0
Streak
0

A floor is 20 units long and 15 units wide. What is the side of the largest square tile that covers it exactly, with whole tiles laid edge to edge? Change the tile with − and +, then check.

2015
Tile side:11 by 1 tiles fit: 20 × 15 = 300 tiles.

Exercises

Exercise .

List the common divisors of 24 and 40, and find gcd(24, 40).

▸ Show solution

Divisors of 24: 1, 2, 3, 4, 6, 8, 12, 24. Divisors of 40: 1, 2, 4, 5, 8, 10, 20, 40. Common: 1, 2, 4, 8. So gcd(24, 40) = 8. Check: 24 = 3 × 8, 40 = 5 × 8.

Exercise .

Find gcd(45, 0), gcd(45, 1), gcd(45, 45) and gcd(9, 45).

▸ Show solution

gcd(45, 0) = 45; gcd(45, 1) = 1; gcd(45, 45) = 45; gcd(9, 45) = 9, since 45 = 5 × 9.

Exercise .

Which pairs are relatively prime: 14 and 15; 14 and 21; 25 and 36; 17 and 51?

▸ Show solution
  1. 14 and 15: consecutive, relatively prime.
  2. 14 and 21: both divisible by 7. Not relatively prime (gcd 7).
  3. 25 and 36: divisors of 25 are 1, 5, 25; 36 = 7 × 5 + 1 and 36 = 1 × 25 + 11, so only 1 is common. Relatively prime.
  4. 17 and 51: 51 = 3 × 17, so gcd = 17. Not relatively prime.
Exercise .

A wall 84 cm long and 60 cm high is to be covered with equal square tiles, without cutting. What is the largest possible tile, and how many are needed?

▸ Show solution

The tile side must be gcd(84, 60). Divisors of 60 from the largest down: 60 (84 = 1 × 60 + 24), 30 (84 = 2 × 30 + 24), 20 (84 = 4 × 20 + 4), 15 (84 = 5 × 15 + 9), 12 (84 = 7 × 12, remainder 0).

So the largest tile has side 12 cm, and 7 × 5 = 35 tiles are needed (84 = 7 × 12, 60 = 5 × 12).

Exercise .

Prove that gcd(n, n + 2) is 1 or 2 for every whole number n ≥ 1, and say when each happens.

▸ Show solution

Let d divide n and n + 2. By the rule on differences, d divides (n + 2) − n = 2, so d is 1 or 2. Hence gcd(n, n + 2) is 1 or 2.

Both occur: gcd(7, 9) = 1 and gcd(8, 10) = 2. It is 2 exactly when n is even, since then 2 divides both n and n + 2.

Exercise .

Let a ≥ b ≥ 1. Prove that gcd(a, b) = gcd(a − b, b). Use it to find gcd(108, 72) in one step.

▸ Show solution

Let d be a common divisor of a and b. By the rule on differences, d | a − b; so d is a common divisor of a − b and b. Conversely, if d | a − b and d | b, then d | (a − b) + b = a by the rule on sums. So the pairs (a, b) and (a − b, b) have exactly the same common divisors. Since b ≥ 1, neither pair is (0, 0), and the largest common divisor is the same: gcd(a, b) = gcd(a − b, b).

Example: gcd(108, 72) = gcd(36, 72) = 36, as found above.

Summary

  1. A common divisor of a and b divides both. 1 is always one; if a and b are both 0, every number is one.
  2. gcd(a, b), for a and b not both 0, is the largest common divisor: list the common divisors, or test the divisors of the smaller number from the top.
  3. gcd(a, 0) = a, gcd(a, a) = a and gcd(a, 1) = 1 for a ≥ 1; if a | b then gcd(a, b) = a. gcd(0, 0) is undefined.
  4. a and b are relatively prime (coprime) when gcd(a, b) = 1. Two different primes, a prime and a number it does not divide, and two consecutive numbers are relatively prime.
  5. Square tiles of side d cover a w by h floor exactly when d divides both; the largest has side gcd(w, h).

Sources

  1. 1.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.3, “The Greatest Common Divisor”, p. 20 (the whole topic: pp. 20–21).
    “If a and b are arbitrary integers, then an integer d is said to be a common divisor of a and b if both d | a and d | b.”
    Used for: the definition of a common divisor.
  2. 2.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.3, “The Greatest Common Divisor”, pp. 20–21 (the whole topic: pp. 20–21).
    “Because 1 is a divisor of every integer, 1 is a common divisor of a and b”
    Used for: 1 is a common divisor of any two numbers.
  3. 3.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.3, “The Greatest Common Divisor”, p. 21 (the whole topic: pp. 20–21).
    “Now every integer divides zero, so that if a = b = 0, then every integer serves as a common divisor of a and b.”
    Used for: when a and b are both 0, every number is a common divisor; so gcd(0, 0) is not defined.
  4. 4.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.3, “The Greatest Common Divisor”, p. 21 (the whole topic: pp. 20–21).
    “However, when at least one of a or b is different from zero, there are only a finite number of positive common divisors. Among these, there is a largest one, called the greatest common divisor of a and b.”
    Used for: when at least one of the numbers is not 0, there are finitely many common divisors and a largest one.
  5. 5.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.6, “Greatest Common Divisors and Least Common Multiples”), Definition 2, p. 280 (the whole topic: pp. 280–281).
    “Let a and b be integers, not both zero. The largest integer d such that d ∣a and d ∣b is called the greatest common divisor of a and b. The greatest common divisor of a and b is denoted by gcd(a, b).”
    Used for: the definition of gcd(a, b) for numbers not both 0, and the notation gcd(a, b).
  6. 6.
    Lynn Marecek, MaryAnne Anthony-Smith and Andrea Honeycutt Mathis, Prealgebra (2nd ed., OpenStax, Rice University, 2020).
    Chapter 10, “Polynomials”, section 10.6, “Introduction to Factoring Polynomials” (“Find the Greatest Common Factor of Two or More Expressions”), p. 846 (the whole topic: pp. 846–847).
    “The greatest common factor (GCF) of two or more expressions is the largest expression that is a factor of all the expressions.”
    Used for: the name greatest common factor, abbreviated GCF.
  7. 7.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “To find the greatest common measure of two given numbers not relatively prime.”
    Used for: Euclid’s name for the gcd, the greatest common measure (Elements VII.2).
  8. 8.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.6, “Greatest Common Divisors and Least Common Multiples”), p. 280 (the whole topic: pp. 280–281).
    “One way to find the greatest common divisor of two integers is to find all the positive common divisors of both integers and then take the largest divisor.”
    Used for: the method: list the common divisors and take the largest.
  9. 9.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “If a number measures two numbers, then it also measures their greatest common measure.”
    Used for: every common divisor divides the gcd (Euclid, Elements VII.2, Corollary).
  10. 10.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 5, “Induction and Recursion”, section 5.4, “Recursive Algorithms” (5.4.1, “Introduction”), p. 382 (the whole topic: pp. 381–383).
    “For instance, for finding the greatest common divisor, the reduction continues until the smaller of the two numbers is zero, because gcd(a, 0) = a when a > 0.”
    Used for: gcd(a, 0) = a for a ≥ 1; the Euclidean algorithm ends with it.
  11. 11.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.6, “Greatest Common Divisors and Least Common Multiples”), Definition 3, p. 281 (the whole topic: pp. 281).
    “The integers a and b are relatively prime if their greatest common divisor is 1.”
    Used for: the definition of relatively prime numbers.
  12. 12.
    David M. Burton, The History of Mathematics: An Introduction (7th ed., McGraw-Hill, 2011).
    Chapter 4, “The Alexandrian School: Euclid”, section 4.3, “Euclid’s Number Theory” (“The Algorithm of Euclid”), p. 176 (the whole topic: pp. 176).
    “Two integers a and b are said to be relatively prime, or prime to each other”
    Used for: relatively prime numbers are also said to be prime to each other.
  13. 13.
    Eric W. Weisstein, “Relatively Prime”, MathWorld, Wolfram Research.
    “Relatively prime integers are sometimes also called strangers or coprime”
    Used for: relatively prime numbers are also called coprime.
  14. 14.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “Numbers relatively prime are those which are measured by a unit alone as a common measure.”
    Used for: Euclid’s definition of relatively prime numbers (Elements VII, Definition 12).
  15. 15.
    David M. Burton, The History of Mathematics: An Introduction (7th ed., McGraw-Hill, 2011).
    Chapter 4, “The Alexandrian School: Euclid”, section 4.3, “Euclid’s Number Theory” (“The Algorithm of Euclid”), p. 176 (the whole topic: pp. 176).
    “We should emphasize that it is possible for a pair of integers to be relatively prime without either integer being a prime.”
    Used for: relatively prime numbers need not be prime.
  16. 16.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “Any prime number is relatively prime to any number which it does not measure.”
    Used for: Euclid, Elements VII.29: a prime is relatively prime to every number it does not divide.
  17. 17.
    David M. Burton, The History of Mathematics: An Introduction (7th ed., McGraw-Hill, 2011).
    Chapter 4, “The Alexandrian School: Euclid”, section 4.3, “Euclid’s Number Theory”, Problems, p. 182 (the whole topic: pp. 172–176).
    “Prove that any two consecutive integers are relatively prime”
    Used for: two consecutive numbers are relatively prime (a standard exercise).
  18. 18.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.3, “The Greatest Common Divisor”, Corollary 2 to Theorem 2.4, p. 23 (the whole topic: pp. 22–23).
    “If 6 and 8 were relatively prime, of course, this situation would not arise. This brings us to Corollary 2. Corollary 2. If a | c and b | c, with gcd(a, b) = 1, then ab | c.”
    Used for: two divisibility tests combine when the divisors are relatively prime: if a | c and b | c with gcd(a, b) = 1, then a × b | c.