Intermediate level

GCD and LCM

The greatest common divisor and the lowest common multiple of two numbers. Learn the divisibility rules, the listing method, the Euclidean algorithm, and the formula tying GCD to LCM.

Before you start

This topic builds on earlier ideas. Before you start, it's worth working through the lessons below — they'll make everything click:

All formulas

  • Divisibility

    ab    b=ak,  kZa \mid b \iff b = a \cdot k, \; k \in \mathbb{Z}

    a divides b when b is a multiple of a

  • Greatest common divisor

    gcd(a,b)=max{d:da and db}\gcd(a, b) = \max\{d : d \mid a \text{ and } d \mid b\}

    the largest number dividing both

  • Lowest common multiple

    lcm(a,b)=min{w>0:aw and bw}\operatorname{lcm}(a, b) = \min\{w > 0 : a \mid w \text{ and } b \mid w\}

    the smallest positive number both divide

  • The identity tying GCD to LCM

    gcd(a,b)lcm(a,b)=ab\gcd(a, b) \cdot \operatorname{lcm}(a, b) = a \cdot b

    know one and you can compute the other

  • Euclidean algorithm

    gcd(a,b)=gcd(b,amodb)\gcd(a, b) = \gcd(b, a \bmod b)

    repeat until the remainder is zero

We say that aa divides bb (written aba \mid b) when bb is a multiple of aa:

ab    b=ak,  kZa \mid b \iff b = a \cdot k, \; k \in \mathbb{Z}

Before hunting for common divisors it pays to know the divisibility rules — they spot a divisor without any dividing:

  • by 2 — when the last digit is even,
  • by 3 — when the digit sum is divisible by 3,
  • by 4 — when the number formed by the last two digits is divisible by 4,
  • by 5 — when the last digit is 0 or 5,
  • by 9 — when the digit sum is divisible by 9,
  • by 10 — when the last digit is 0.

Greatest common divisor

The GCD of two numbers is the largest number dividing both:

gcd(a,b)=max{d:da and db}\gcd(a, b) = \max\{d : d \mid a \text{ and } d \mid b\}

The simplest method is to list the divisors of both numbers and take the largest they share:

Compute gcd(24, 36)

When gcd(a,b)=1\gcd(a, b) = 1 the numbers share no divisor beyond one — they are coprime. That happens for 88 and 1515, for instance, even though neither of them is prime.

The Euclidean algorithm

Listing divisors gets unwieldy for larger numbers. A relation known for over two thousand years is far quicker:

gcd(a,b)=gcd(b,amodb)\gcd(a, b) = \gcd(b, a \bmod b)

Replace the pair by "the smaller number and the remainder" until the remainder hits zero. The last non-zero number is the GCD.

Compute gcd(48, 18) with the Euclidean algorithm

Lowest common multiple

The LCM is the smallest positive number divisible by both:

lcm(a,b)=min{w>0:aw and bw}\operatorname{lcm}(a, b) = \min\{w > 0 : a \mid w \text{ and } b \mid w\}

Multiples of 44: 4,8,12,16,20,24,4, 8, \mathbf{12}, 16, 20, 24, \ldots; multiples of 66: 6,12,18,24,6, \mathbf{12}, 18, 24, \ldots — the first shared one is 1212, so lcm(4,6)=12\operatorname{lcm}(4, 6) = 12.

Nothing has to be listed, though, because the GCD and the LCM are tied together:

gcd(a,b)lcm(a,b)=ab\gcd(a, b) \cdot \operatorname{lcm}(a, b) = a \cdot b
Compute lcm(24, 36)

Why it matters

Both ideas come back with fractions: the GCD of the numerator and the denominator reduces a fraction to lowest terms, and the LCM of the denominators is the lowest common denominator when adding. Outside maths the LCM answers questions like "how often do both buses leave at the same time", and the GCD "how large can equal square tiles be to cover a rectangle without cutting".

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 8Score: 0
gcd(10, 15) =

Common mistakes

  • Swapping the GCD and the LCM — the GCD is no larger than either number, the LCM no smaller; a result the other way round signals a slip.
  • Taking the product as the LCMaba \cdot b is a common multiple, but the lowest one only when the numbers are coprime.
  • Stopping the Euclidean algorithm one step too late — the GCD is the last non-zero remainder, not the zero that ends the procedure.
  • Assuming two composite numbers cannot be coprime88 and 1515 are both composite, yet gcd(8,15)=1\gcd(8, 15) = 1.

Formula card

Topic: GCD and LCM

  • Divisibility

    ab    b=ak,  kZa \mid b \iff b = a \cdot k, \; k \in \mathbb{Z}

    a divides b when b is a multiple of a

  • Greatest common divisor

    gcd(a,b)=max{d:da and db}\gcd(a, b) = \max\{d : d \mid a \text{ and } d \mid b\}

    the largest number dividing both

  • Lowest common multiple

    lcm(a,b)=min{w>0:aw and bw}\operatorname{lcm}(a, b) = \min\{w > 0 : a \mid w \text{ and } b \mid w\}

    the smallest positive number both divide

  • The identity tying GCD to LCM

    gcd(a,b)lcm(a,b)=ab\gcd(a, b) \cdot \operatorname{lcm}(a, b) = a \cdot b

    know one and you can compute the other

  • Euclidean algorithm

    gcd(a,b)=gcd(b,amodb)\gcd(a, b) = \gcd(b, a \bmod b)

    repeat until the remainder is zero

Frequently asked questions

Related articles