Sub topic 9
GCD and LCM from Prime Factorizations
This chapter assumes you can already write the prime factorization of an integer, such as 360 = 2^3 × 3^2 × 5. It also assumes you can work with exponent notation and know what factors and multiples are. All integers here are positive unless stated otherwise.
What GCD and LCM mean
The greatest common divisor (GCD) of two or more positive integers is the largest integer that divides every one of them. It is also called the greatest common factor (GCF).
The least common multiple (LCM) of two or more positive integers is the smallest positive integer that every one of them divides.
For 12 and 18:
- The common divisors are 1, 2, 3, and 6, so GCD(12, 18) = 6.
- The positive multiples of 12 are 12, 24, 36, 48, ... and of 18 are 18, 36, 54, ... The first shared value is 36, so LCM(12, 18) = 36.
Listing works for small numbers. For numbers like 360 and 756, prime factorization is faster and less error-prone.
The prime-exponent method
Write each number as a product of primes. Then look at each prime that appears in any of the numbers:
- GCD: take each prime to the smallest exponent it has across the numbers. A prime missing from a number has exponent 0, so it drops out of the GCD.
- LCM: take each prime to the largest exponent it has across the numbers.
Take 84 and 90:
| Prime | 84 = 2^2 × 3 × 7 | 90 = 2 × 3^2 × 5 | Min (GCD) | Max (LCM) |
|---|---|---|---|---|
| 2 | exponent 2 | exponent 1 | 1 | 2 |
| 3 | exponent 1 | exponent 2 | 1 | 2 |
| 5 | exponent 0 | exponent 1 | 0 | 1 |
| 7 | exponent 1 | exponent 0 | 0 | 1 |
GCD = 2^1 × 3^1 = 6. LCM = 2^2 × 3^2 × 5 × 7 = 1260.
The reasoning is direct. A divisor of both numbers cannot use more of a prime than the smaller exponent allows. A common multiple must contain at least as much of each prime as the larger exponent requires.
Checking the method in code
This program factors both numbers and applies the min/max rule. It also compares the result with Python's built-in gcd.
from math import gcd
def prime_factors(n):
factors = {}
p = 2
while p * p <= n:
while n % p == 0:
factors[p] = factors.get(p, 0) + 1
n //= p
p += 1
if n > 1:
factors[n] = factors.get(n, 0) + 1
return factors
def gcd_lcm_from_factors(a, b):
fa, fb = prime_factors(a), prime_factors(b)
g, l = 1, 1
for p in set(fa) | set(fb):
ea, eb = fa.get(p, 0), fb.get(p, 0)
g *= p ** min(ea, eb)
l *= p ** max(ea, eb)
return g, l
print(gcd_lcm_from_factors(84, 90)) # (6, 1260)
print(gcd(84, 90)) # 6The product rule for two integers
For two positive integers a and b:
GCD(a, b) × LCM(a, b) = a × b
The reason is in the exponents. For each prime, min(e1, e2) + max(e1, e2) = e1 + e2. The GCD and LCM together use exactly the exponents that a and b use together.
from math import gcd
a, b = 84, 90
g = gcd(a, b)
l = a * b // g
print(g, l) # 6 1260
print(g * l == a * b) # TrueThis gives a fast LCM: LCM(a, b) = a × b ÷ GCD(a, b). It also lets you find a missing number. If GCD(a, b) = 12, LCM(a, b) = 360, and a = 36, then b = 12 × 360 ÷ 36 = 120.
Three or more integers
The prime-exponent method extends directly. Take the minimum (or maximum) exponent of each prime across all the numbers.
For 12 = 2^2 × 3, 18 = 2 × 3^2, and 30 = 2 × 3 × 5:
- GCD = 2^1 × 3^1 = 6. The prime 5 is missing from 12 and 18, so its minimum exponent is 0.
- LCM = 2^2 × 3^2 × 5 = 180.
from math import gcd, lcm
print(gcd(12, 18, 30)) # 6
print(lcm(12, 18, 30)) # 180Passing several arguments to gcd and lcm needs Python 3.9 or later.
The Euclidean algorithm
When a number is hard to factor, the Euclidean algorithm finds the GCD from remainders alone. It relies on GCD(a, b) = GCD(b, a mod b). Repeat until the remainder is 0. The last nonzero value is the GCD.
For 90 and 84:
- GCD(90, 84) = GCD(84, 6), because 90 = 84 × 1 + 6.
- GCD(84, 6) = GCD(6, 0), because 84 = 6 × 14 + 0.
- The answer is 6.
def euclid(a, b):
while b:
a, b = b, a % b
return a
print(euclid(90, 84)) # 6This works because any number that divides both a and b also divides the remainder a − qb, and the reverse holds too. The pair keeps the same common divisors while the numbers shrink. On the GRE the numbers are usually small, so factorization is normally quicker.
Special cases
- A multiple of the other. If b is a multiple of a, then GCD(a, b) = a and LCM(a, b) = b. For example, GCD(7, 21) = 7 and LCM(7, 21) = 21.
- Relatively prime integers. Two integers with GCD 1 share no prime factor. Their LCM is their product. GCD(8, 15) = 1 and LCM(8, 15) = 120.
- Consecutive integers. GCD(n, n + 1) = 1, because a common divisor would also divide their difference, 1. So LCM(n, n + 1) = n(n + 1).
- Involving 1. GCD(1, n) = 1 and LCM(1, n) = n.
- Involving 0. GCD(0, n) = n for positive n, because every integer divides 0. LCM with 0 is not meaningful for GRE purposes. The GRE almost always uses positive integers.
- Negative integers. Divisors and multiples ignore sign. GCD and LCM are defined as positive values, so GCD(−12, 18) = 6.
Word problems and what they signal
GRE word problems hide GCD and LCM behind everyday wording. The key is to decide which one the situation needs.
- Splitting or tiling into equal pieces (GCD). Phrases such as "largest possible," "equal groups with nothing left over," or "square tiles covering the floor exactly" point to a GCD. A 84 cm by 90 cm floor covered with square tiles uses tiles of side GCD(84, 90) = 6 cm at most.
- Repeating events that line up (LCM). Phrases such as "at the same time again," "next time together," or "smallest number divisible by each" point to an LCM. Buses that leave every 12 and 18 minutes leave together again after LCM(12, 18) = 36 minutes.
- Smallest number with given remainders. If a number leaves the same remainder r when divided by several values, it equals LCM + r (or a multiple of the LCM, plus r). For example, a number leaving remainder 1 when divided by 4, 6, and 10 is 60k + 1, and the smallest such value above 1 is 61.
Using GCD and LCM in quantitative comparison
Quantitative comparison asks you to compare two quantities. When an unknown integer is constrained by a GCD or LCM, work out the prime-exponent restrictions first. Then test numbers that fit.
Example. x is a positive integer, and GCD(x, 18) = 6.
- Quantity A: x
- Quantity B: 12
Since 18 = 2 × 3^2 and 6 = 2 × 3, x must contain the prime 2 at least once. It must contain 3 exactly once, because a higher power of 3 would raise the GCD. Other primes are unrestricted.
- x = 6 satisfies the condition, and A < B.
- x = 30 = 2 × 3 × 5 satisfies it too, and A > B.
Both outcomes occur, so the relationship cannot be determined. Finding two valid test numbers that disagree is enough to settle a "cannot be determined" answer.
A second case where the numbers give a definite answer: Quantity A is LCM(18, 24), and Quantity B is 70. Since 18 = 2 × 3^2 and 24 = 2^3 × 3, the LCM is 2^3 × 3^2 = 72, so Quantity A is greater.
Common errors
- Multiplying the two numbers and calling it the LCM. This works only when the GCD is 1.
- Taking the largest exponent for the GCD or the smallest for the LCM. Remember: GCD takes the minimum and LCM takes the maximum.
- Forgetting that a prime missing from one number has exponent 0, which sets that prime's GCD exponent to 0.
- Applying GCD × LCM = product to three or more numbers.
- Choosing the LCM when the problem asks to divide into the largest equal groups (a GCD problem), or the reverse.
Exercise
Find the GCD and LCM of 360 and 756, and verify the product rule.
Show answer
360 = 2^3 × 3^2 × 5 and 756 = 2^2 × 3^3 × 7. GCD = 2^2 × 3^2 = 36. LCM = 2^3 × 3^3 × 5 × 7 = 7560. Check: 36 × 7560 = 272160 and 360 × 756 = 272160.
Exercise
The GCD of two positive integers a and b is 12, and their LCM is 360. If a = 36, what is b?
Show answer
Use GCD × LCM = a × b. Then b = 12 × 360 ÷ 36 = 120. Check: 36 = 2^2 × 3^2 and 120 = 2^3 × 3 × 5, so GCD = 2^2 × 3 = 12 and LCM = 2^3 × 3^2 × 5 = 360.
Exercise
Three lights blink every 4, 6, and 10 seconds. They all blink together at time 0. After how many seconds do they next blink together?
Show answer
The answer is LCM(4, 6, 10). With 4 = 2^2, 6 = 2 × 3, and 10 = 2 × 5, the LCM is 2^2 × 3 × 5 = 60 seconds.