TExcelTAD
Home › Basic Math & Pre-Algebra › GCF and LCM

GCF and LCM

The greatest common factor (GCF) of two numbers is the largest number that divides both. The least common multiple (LCM) is the smallest number that both divide. The GCF simplifies fractions and cuts things into the largest equal pieces; the LCM finds common denominators and tells you when two repeating cycles line up again. One of the oldest algorithms in the world computes them both.

Greatest common factor

The GCF is also called the greatest common divisor (GCD), and code usually calls it gcd. For 24 and 36:

factors of 24: 1,2,3,4,6,8,12,24factors of 36: 1,2,3,4,6,9,12,18,36common: 1,2,3,4,6,12GCF⁡(24,36)=12\begin{aligned} &\text{factors of 24: } 1, 2, 3, 4, 6, 8, 12, 24 \\ &\text{factors of 36: } 1, 2, 3, 4, 6, 9, 12, 18, 36 \\ &\text{common: } 1, 2, 3, 4, 6, 12 \\ &\operatorname{GCF}(24, 36) = 12 \end{aligned}

Listing works for small numbers. If two numbers have GCF 1, like 8 and 15, they are coprime (relatively prime): they share no factor except 1, even though neither is prime itself.

Least common multiple

For 6 and 8:

multiples of 6: 6,12,18,24,30,36,42,48,…multiples of 8: 8,16,24,32,40,48,…common: 24,48,…LCM⁡(6,8)=24\begin{aligned} &\text{multiples of 6: } 6, 12, 18, 24, 30, 36, 42, 48, \ldots \\ &\text{multiples of 8: } 8, 16, 24, 32, 40, 48, \ldots \\ &\text{common: } 24, 48, \ldots \\ &\operatorname{LCM}(6, 8) = 24 \end{aligned}

Every common multiple is a multiple of the LCM, so after 24 come 48, 72 and so on.

Using prime factorizations

With the prime factorizations, both are quick to read off:

  • GCF: take each prime that appears in both, with the smaller exponent.
  • LCM: take every prime that appears in either, with the larger exponent.
360=23×32×584=22×3×7GCF⁡=22×3=12LCM⁡=23×32×5×7=2 520\begin{aligned} 360 &= 2^3 \times 3^2 \times 5 \\ 84 &= 2^2 \times 3 \times 7 \\[1ex] \operatorname{GCF} &= 2^2 \times 3 = 12 \\ \operatorname{LCM} &= 2^3 \times 3^2 \times 5 \times 7 = 2\,520 \end{aligned}

The GCF is the biggest thing "inside" both numbers; the LCM is the smallest thing that "contains" both.

Euclid's algorithm

Factoring big numbers is slow, but there is a method from around 300 BC that never needs factoring. It rests on one fact: any common factor of aa and bb also divides the remainder a mod ba \bmod b. So GCF⁡(a,b)=GCF⁡(b,a mod b)\operatorname{GCF}(a, b) = \operatorname{GCF}(b, a \bmod b), and the numbers shrink fast. Repeat until the remainder is 0; the last non-zero number is the GCF.

GCF⁡(1386,588):1386=588×2+210588=210×2+168210=168×1+42168=42×4+0→GCF⁡=42\begin{aligned} &\operatorname{GCF}(1386, 588){:} \\ 1386 &= 588 \times 2 + 210 \\ 588 &= 210 \times 2 + 168 \\ 210 &= 168 \times 1 + 42 \\ 168 &= 42 \times 4 + 0 && \to \operatorname{GCF} = 42 \end{aligned}
python · try it yourself
def gcd(a, b):
    while b != 0:
        print(f"gcd({a}, {b}) -> {a} % {b} = {a % b}")
        a, b = b, a % b
    return a

print("result:", gcd(1386, 588))
gcd(1386, 588) -> 1386 % 588 = 210
gcd(588, 210) -> 588 % 210 = 168
gcd(210, 168) -> 210 % 168 = 42
gcd(168, 42) -> 168 % 42 = 0
result: 42

Four steps instead of listing every factor of both numbers. Even for numbers with hundreds of digits, Euclid's algorithm finishes in a few hundred steps. The Euclidean Algorithm lesson in the DSA course looks at it as an algorithm.

The link between GCF and LCM

For any two positive whole numbers:

GCF⁡(a,b)×LCM⁡(a,b)=a×b\operatorname{GCF}(a, b) \times \operatorname{LCM}(a, b) = a \times b

So once you know the GCF, the LCM is one division away: LCM⁡(a,b)=a×b÷GCF⁡(a,b)\operatorname{LCM}(a, b) = a \times b \div \operatorname{GCF}(a, b). For 6 and 8: GCF⁡=2\operatorname{GCF} = 2, so LCM⁡=48÷2=24\operatorname{LCM} = 48 \div 2 = 24. Python 3.9+ has both built in, and they accept more than two numbers:

python · try it yourself
import math

a, b = 360, 84
g, l = math.gcd(a, b), math.lcm(a, b)
print("gcd:", g, " lcm:", l)
print("gcd x lcm =", g * l, " a x b =", a * b)
print("lcm via gcd:", a * b // g)
print("three numbers:", math.gcd(12, 18, 30), math.lcm(4, 6, 10))
gcd: 12  lcm: 2520
gcd x lcm = 30240  a x b = 30240
lcm via gcd: 2520
three numbers: 6 60

Word problems: which one do I need?

The question asks for… Use Example
the largest equal pieces, groups, tiles GCF Cut 48 cm and 72 cm ribbons into equal pieces as long as possible: 24 cm
the first time two cycles meet again LCM Buses every 12 min and every 18 min, both leave at 9:00; next together at 9:36 (LCM⁡=36\operatorname{LCM} = 36)
simplifying a fraction GCF 2436=24÷1236÷12=23\frac{24}{36} = \frac{24 \div 12}{36 \div 12} = \frac{2}{3}
a common denominator LCM 16+18=424+324=724\frac{1}{6} + \frac{1}{8} = \frac{4}{24} + \frac{3}{24} = \frac{7}{24}

In software

  • Fractions and ratios: Python's Fraction divides numerator and denominator by their GCD every time, which is why Fraction(24, 36) prints 2/3. Screen resolutions use it too: 1920×1080÷gcd 1201920 \times 1080 \div \text{gcd } 120 gives the 16:9 aspect ratio.
  • Scheduling and animation: two loops of 12 and 18 frames line up every LCM⁡(12,18)=36\operatorname{LCM}(12, 18) = 36 frames; a combined pattern repeats only that often.
  • Cryptography: RSA key generation uses the GCD to check that numbers are coprime, and the extended Euclidean algorithm to compute modular inverses (see Modular Arithmetic).
python · try it yourself
import math

w, h = 1920, 1080
g = math.gcd(w, h)
print(f"{w}x{h} -> {w // g}:{h // g}")
print("loops of 12 and 18 frames realign every", math.lcm(12, 18), "frames")
1920x1080 -> 16:9
loops of 12 and 18 frames realign every 36 frames

Exercises

Exercise 1: By listing

Find GCF⁡(18,30)\operatorname{GCF}(18, 30) and LCM⁡(18,30)\operatorname{LCM}(18, 30) by listing factors and multiples.

Answer

GCF⁡=6\operatorname{GCF} = 6, LCM⁡=90\operatorname{LCM} = 90

Solution
factors of 18: 1,2,3,6,9,18factors of 30: 1,2,3,5,6,10,15,30→greatest common: 6multiples of 30: 30,60,90…; 30 and 60 are not multiples of 18, 90=18×5→LCM⁡ 90check: 6×90=540=18×30 ✓\begin{aligned} &\text{factors of 18: } 1, 2, 3, 6, 9, 18 \\ &\text{factors of 30: } 1, 2, 3, 5, 6, 10, 15, 30 && \to \text{greatest common: } 6 \\ &\text{multiples of 30: } 30, 60, 90 \ldots;\ 30 \text{ and } 60 \text{ are not multiples of } 18,\ 90 = 18 \times 5 && \to \operatorname{LCM}\ 90 \\ &\text{check: } 6 \times 90 = 540 = 18 \times 30\ \checkmark \end{aligned}

Exercise 2: By prime factorization

Use prime factorizations to find the GCF and LCM of 540 and 252.

Answer

GCF⁡=36\operatorname{GCF} = 36, LCM⁡=3 780\operatorname{LCM} = 3\,780

Solution
540=22×33×5252=22×32×7GCF⁡=22×32=36LCM⁡=22×33×5×7=4×27×35=3 780check: 36×3 780=136 080=540×252 ✓\begin{aligned} 540 &= 2^2 \times 3^3 \times 5 \\ 252 &= 2^2 \times 3^2 \times 7 \\ \operatorname{GCF} &= 2^2 \times 3^2 = 36 \\ \operatorname{LCM} &= 2^2 \times 3^3 \times 5 \times 7 = 4 \times 27 \times 35 = 3\,780 \\ \text{check: } 36 \times 3\,780 &= 136\,080 = 540 \times 252\ \checkmark \end{aligned}
python · try it yourself
import math
print(math.gcd(540, 252), math.lcm(540, 252))
36 3780

Exercise 3: Euclid by hand

Run Euclid's algorithm on 2 024 and 1 288.

Answer

GCF⁡=184\operatorname{GCF} = 184

Solution
2 024=1 288×1+7361 288=736×1+552736=552×1+184552=184×3+0→GCF⁡=184\begin{aligned} 2\,024 &= 1\,288 \times 1 + 736 \\ 1\,288 &= 736 \times 1 + 552 \\ 736 &= 552 \times 1 + 184 \\ 552 &= 184 \times 3 + 0 && \to \operatorname{GCF} = 184 \end{aligned}

Check: 2 024=184×112\,024 = 184 \times 11 and 1 288=184×71\,288 = 184 \times 7, and 11 and 7 are coprime.

Exercise 4: Tiles and lights

(a) A floor is 270 cm by 360 cm. What is the largest square tile that covers it exactly with no cutting, and how many tiles are needed? (b) Three lights blink every 4, 6 and 15 seconds, and all blink together now. When do they next blink together?

Answer

(a) 90 cm tiles, 12 of them. (b) after 60 seconds.

Solution

(a) The tile side must divide both lengths, and be as large as possible: GCF⁡(270,360)=90\operatorname{GCF}(270, 360) = 90. That gives 3×4=123 \times 4 = 12 tiles. (b) The first common time is LCM⁡(4,6,15)=22×3×5=60\operatorname{LCM}(4, 6, 15) = 2^2 \times 3 \times 5 = 60.

python · try it yourself
import math

side = math.gcd(270, 360)
print(side, (270 // side) * (360 // side))
print(math.lcm(4, 6, 15))
90 12
60

Test yourself

What is GCF⁡(42,56)\operatorname{GCF}(42, 56)?

  1. 7
  2. 14
  3. 28
  4. 2

If GCF⁡(a,b)=4\operatorname{GCF}(a, b) = 4 and a×b=480a \times b = 480, what is LCM⁡(a,b)\operatorname{LCM}(a, b)?

  1. 120
  2. 1920
  3. 60
  4. 476

Two runners lap a track in 6 and 9 minutes. They start together. When are they next together at the start line?

  1. After 3 minutes
  2. After 15 minutes
  3. After 54 minutes
  4. After 18 minutes