Number TheoryPrimes and Factorization

gcd and lcm from Prime Factorizations

Take the smaller exponent of each prime for the gcd and the larger for the lcm; then gcd(a, b) · lcm(a, b) = ab.

Not started Jump to practice

Once numbers are written as products of primes (thanks to unique factorization), their gcd and lcm can be read off directly, and a tidy identity connects the two.

Write $a$ and $b$ using the same list of primes, allowing exponent $0$: $a = \prod p^{\alpha_p}$ and $b = \prod p^{\beta_p}$. Then $$\gcd(a, b) = \prod p^{\min(\alpha_p, \beta_p)}, \qquad \operatorname{lcm}(a, b) = \prod p^{\max(\alpha_p, \beta_p)}.$$

Why it works

By unique factorization, $d \mid a$ exactly when $d$ uses only the primes of $a$, each with an exponent at most $\alpha_p$. So a common divisor can use each prime at most $\min(\alpha_p, \beta_p)$ times, and the greatest one uses exactly that many. A common multiple must contain each number completely, so it needs at least $\max(\alpha_p, \beta_p)$ copies of each prime, and the least one uses exactly that many. Since $\min(\alpha, \beta) + \max(\alpha, \beta) = \alpha + \beta$ for every prime, $$\gcd(a, b) \cdot \operatorname{lcm}(a, b) = ab.$$
Exponents of each prime in the first example.
$2$$3$$5$$7$
$a = 360$3210
$b = 1134$1401
gcd: smaller1200
lcm: larger3411

Worked example: Reading off gcd and lcm

Find $\gcd(a, b)$ and $\operatorname{lcm}(a, b)$ for $a = 2^{3} \cdot 3^{2} \cdot 5 = 360$ and $b = 2 \cdot 3^{4} \cdot 7 = 1134$.
  1. Smaller exponents: $2^{1} \cdot 3^{2} \cdot 5^{0} \cdot 7^{0} = 18$.
  2. Larger exponents: $2^{3} \cdot 3^{4} \cdot 5 \cdot 7 = 8 \cdot 81 \cdot 35 = 22{,}680$.
  3. Check: $18 \cdot 22{,}680 = 408{,}240 = 360 \cdot 1134$.

Answer: $\gcd = 18$, $\operatorname{lcm} = 22{,}680$.

Worked example: Working backward

Two positive integers have gcd $12$ and lcm $360$. One of them is $72$. What is the other?
  1. $\gcd \cdot \operatorname{lcm} = ab$, so $b = \dfrac{12 \cdot 360}{72} = 60$.
  2. Check: $72 = 2^{3} \cdot 3^{2}$ and $60 = 2^{2} \cdot 3 \cdot 5$. Smaller exponents: $2^{2} \cdot 3 = 12$. Larger: $2^{3} \cdot 3^{2} \cdot 5 = 360$.

Answer: $60$.

Worked example: Three numbers

Find $\gcd$ and $\operatorname{lcm}$ of $12 = 2^{2} \cdot 3$, $18 = 2 \cdot 3^{2}$ and $20 = 2^{2} \cdot 5$.
  1. gcd: smallest exponents, $2^{1} \cdot 3^{0} \cdot 5^{0} = 2$.
  2. lcm: largest exponents, $2^{2} \cdot 3^{2} \cdot 5 = 180$.
  3. Here $2 \cdot 180 = 360$, but $12 \cdot 18 \cdot 20 = 4320$: the product rule is only for two numbers.

Answer: $\gcd = 2$, $\operatorname{lcm} = 180$.

Common mistake

Taking the larger exponents for the gcd (that gives the lcm), or using $\gcd \cdot \operatorname{lcm} = \text{product}$ for three numbers. For $2, 2, 2$: the gcd and lcm are both $2$, so their product is $4$, not $2 \cdot 2 \cdot 2 = 8$. With three numbers, min plus max misses the middle exponent.

Two special cases are worth remembering. Numbers are coprime exactly when they share no prime at all; then every minimum is $0$, so $\gcd(a, b) = 1$ and $\operatorname{lcm}(a, b) = ab$. And when $a \mid b$, every exponent of $a$ is at most the matching exponent of $b$, so $\gcd(a, b) = a$ and $\operatorname{lcm}(a, b) = b$. The lcm answers "when do two cycles line up again?": lights blinking every $12$ and every $18$ seconds blink together every $\operatorname{lcm}(12, 18) = 36$ seconds.

For large numbers, factoring is slow, while the Euclidean algorithm is fast. So computers find the gcd by Euclid and then the lcm as $ab / \gcd(a, b)$.

Key idea

gcd: the smaller exponent of each prime. lcm: the larger. For two numbers, $\gcd(a, b) \cdot \operatorname{lcm}(a, b) = ab$.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 2 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 1, Prime Numbers
  • Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT OpenCourseWare 6.042J), chapter on number theory: the fundamental theorem of arithmetic
@teddyrookbook GitHub