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:
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:
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.
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 and also divides the remainder . So , and the numbers shrink fast. Repeat until the remainder is 0; the last non-zero number is the GCF.
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:
So once you know the GCF, the LCM is one division away: . For 6 and 8: , so . Python 3.9+ has both built in, and they accept more than two numbers:
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 () |
| simplifying a fraction | GCF | |
| a common denominator | LCM |
In software
- Fractions and ratios: Python's
Fractiondivides numerator and denominator by their GCD every time, which is whyFraction(24, 36)prints2/3. Screen resolutions use it too: gives the 16:9 aspect ratio. - Scheduling and animation: two loops of 12 and 18 frames line up every 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).
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 and by listing factors and multiples.
Answer
,
Solution
Exercise 2: By prime factorization
Use prime factorizations to find the GCF and LCM of 540 and 252.
Answer
,
Solution
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
Solution
Check: and , 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: . That gives tiles. (b) The first common time is .
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 ?
- 7
- 14
- 28
- 2
If and , what is ?
- 120
- 1920
- 60
- 476
Two runners lap a track in 6 and 9 minutes. They start together. When are they next together at the start line?
- After 3 minutes
- After 15 minutes
- After 54 minutes
- After 18 minutes