Sub topic 3
Every Integer Factors Uniquely into Primes
This chapter assumes you already know what divisibility means (a divides b when b % a == 0) and how primes and composites are defined. It also assumes you can read short Python functions that use loops and dictionaries. Here we turn those definitions into the most important structural fact about the integers: the fundamental theorem of arithmetic.
The statement
Every integer n > 1 can be written as a product of primes, and that product is unique apart from the order of the factors.
The theorem makes two claims, and they need separate proofs:
- Existence: at least one prime factorization exists.
- Uniqueness: no two factorizations use different primes or different counts of the same prime.
Here are a few examples:
| n | Prime factorization | Canonical form |
|---|---|---|
| 12 | 2 × 2 × 3 | 2² · 3 |
| 84 | 2 × 2 × 3 × 7 | 2² · 3 · 7 |
| 360 | 2 × 2 × 2 × 3 × 3 × 5 | 2³ · 3² · 5 |
| 97 | 97 | 97 |
| 1024 | 2 × 2 × ... × 2 (ten times) | 2¹⁰ |
The canonical form writes each distinct prime once, in increasing order, with an exponent giving how many times it appears: n = p1^a1 · p2^a2 · ... · pk^ak. Uniqueness means each integer has exactly one canonical form. You can treat that list of exponents as the number's fingerprint.
Existence: breaking numbers down
Picture splitting a number into two smaller factors, again and again, until no piece can be split further. The result is a factor tree. The leaves are the primes.
The proof turns this picture into an argument by smallest counterexample:
- Suppose some integer greater than 1 has no prime factorization. Let
nbe the smallest such integer. nis not prime, because a prime is its own factorization (a product with one factor).- So
nis composite:n = a · bwith1 < a, b < n. - Both
aandbare smaller thann, so by the choice ofneach has a prime factorization. - Put the two factorizations side by side and you have one for
n. That contradicts step 1.
So no counterexample exists. The argument also explains why the splitting process always ends: each split produces strictly smaller numbers that are still at least 2, and a chain of shrinking positive integers cannot go on forever.
Uniqueness: Euclid's lemma does the work
Existence is the easy half. The diagram shows two different trees for 360 that reach the same leaves, but a picture is not a proof. Uniqueness depends on one key fact.
Euclid's lemma
If a prime p divides a product a · b, then p divides a or p divides b.
Proof. Suppose p | ab but p does not divide a. The only positive divisors of p are 1 and p, so gcd(p, a) = 1. By Bézout's identity there are integers x and y with
p·x + a·y = 1.
Multiply both sides by b:
p·b·x + a·b·y = b.
The first term is a multiple of p. The second is a multiple of ab, which p divides. So p divides their sum, which is b. ∎
By induction the lemma extends to longer products: if p divides q1 · q2 · ... · qs, then p divides at least one qj.
The uniqueness proof
Suppose n has two prime factorizations:
p1 · p2 · ... · pr = q1 · q2 · ... · qs.
p1divides the left side, so it divides the right side.- By the extended Euclid's lemma,
p1divides someqj. qjis prime andp1 > 1, sop1 = qj.- Cancel that common factor from both sides. You get two factorizations of the smaller number
n / p1. - Repeat. Each step pairs one
pwith one equalqand removes both. When one side runs out, the other must also be empty, because a product of one or more primes is greater than 1. Sor = s, and the two lists are the same up to order. ∎
Why 1 is not prime
People often think excluding 1 is an arbitrary convention. The theorem gives the reason. If 1 counted as prime, then 12 = 2² · 3 = 1 · 2² · 3 = 1³ · 2² · 3, and factorizations would stop being unique. You would have to add "ignoring any 1s" to every statement that depends on uniqueness. Declaring 1 to be neither prime nor composite keeps the theorem clean. In the same spirit, 1 counts as the empty product: it is the product of zero primes, and every exponent in its canonical form is 0.
Uniqueness is not automatic
You might expect uniqueness to hold in any number system with multiplication. It doesn't, and seeing it fail shows how much Euclid's lemma is doing.
Consider the Hilbert numbers: the positive integers that leave remainder 1 when divided by 4, namely 1, 5, 9, 13, 17, 21, 25, 29, 33, ... The product of two such numbers is again one of them. Call a Hilbert number "Hilbert-prime" if it cannot be written as a product of two smaller Hilbert numbers greater than 1. The number 9 is Hilbert-prime, because its only factor 3 is not in the set. The numbers 21 and 49 are Hilbert-prime for the same reason. Now look at 441:
441 = 9 · 49 = 21 · 21.
That gives two different factorizations into Hilbert-primes. Existence still holds, because the smallest-counterexample argument works unchanged. Uniqueness breaks because Euclid's lemma fails: 21 divides 9 · 49 but divides neither 9 nor 49. The ordinary integers avoid this because Bézout's identity holds in them, and Bézout's identity is what proves Euclid's lemma.
Working with factorizations in code
A dictionary mapping each prime to its exponent is a natural way to represent a factorization. Python's Counter fits well because missing primes read as exponent 0. The factorizer below is deliberately simple. It divides by every candidate d in turn. Later chapters make it much faster, first by stopping at the square root and then by using a smallest-prime-factor table.
from collections import Counter
from math import gcd, lcm, prod
def factor(n):
"""Return the prime factorization of n >= 2 as a Counter {prime: exponent}."""
if n < 2:
raise ValueError("n must be at least 2")
factors = Counter()
d = 2
while n > 1:
while n % d == 0:
factors[d] += 1
n //= d
d += 1
return factors
def rebuild(factors):
"""Multiply a factorization back into an integer (empty -> 1)."""
return prod(p ** e for p, e in factors.items())
def divisor_count(n):
return prod(e + 1 for e in factor(n).values())
fa, fb = factor(360), factor(84)
print(dict(fa)) # {2: 3, 3: 2, 5: 1}
print(dict(fb)) # {2: 2, 3: 1, 7: 1}
print(rebuild(fa)) # 360
# Counter '&' takes the minimum exponent, '|' takes the maximum.
print(rebuild(fa & fb), gcd(360, 84)) # 12 12
print(rebuild(fa | fb), lcm(360, 84)) # 2520 2520
print(divisor_count(360)) # 24Notice that factor never records a composite d, even though it tries every value. By the time the loop reaches, say, d = 6, every factor of 2 and 3 has already been divided out of n, so n % 6 cannot be 0. This "smaller primes have already removed their multiples" idea comes back as the core of the sieve.
math.lcm requires Python 3.9 or later.
What uniqueness buys you
Because each integer has one exponent fingerprint, many operations reduce to simple arithmetic on exponents. Write a = ∏ p^αp and b = ∏ p^βp. Then:
| Question | Answer in terms of exponents |
|---|---|
| Does `a` divide `b`? | `αp ≤ βp` for every prime `p` |
| `gcd(a, b)` | `∏ p^min(αp, βp)` |
| `lcm(a, b)` | `∏ p^max(αp, βp)` |
| Number of divisors of `a` | `∏ (αp + 1)` |
| Is `a` a perfect square? | every `αp` is even |
The divisor count formula works because each divisor picks an exponent from 0 to αp for each prime independently. For 360 that gives (3+1)(2+1)(1+1) = 24. None of these formulas would be valid without uniqueness. If 441 could also be written as 21 · 21 in the ordinary integers, "the exponent of 3 in 441" would have no single answer.
The identity gcd(a, b) · lcm(a, b) = a · b follows directly, because min(x, y) + max(x, y) = x + y for every prime's exponents. Check it: 12 · 2520 = 30240 = 360 · 84.
Common mistakes
- Treating 1 as prime, or calling
factor(1). The integer 1 has the empty factorization. Code should either return an emptyCounteror reject the input explicitly, as the function above does. - Forgetting repeated factors. If the inner
whileloop above were anif, thenfactor(8)would record a single 2 and lose2². Always divide by a prime as many times as it goes. - Using Euclid's lemma with a composite divisor. "6 divides ab, so 6 divides a or b" is false, as the 4 × 9 example showed.
- Assuming uniqueness proves itself. The Hilbert numbers show that existence alone does not give uniqueness. The proof needs Bézout's identity and Euclid's lemma.
- Negative numbers and zero. The theorem covers
n > 1. Negative integers factor as −1 times a positive factorization. Zero has no prime factorization, because every prime divides it.
Exercises
Exercise
Write the canonical factorization of 2520 and use it to count its positive divisors.
Show answer
2520 = 2³ · 3² · 5 · 7. The number of divisors is (3+1)(2+1)(1+1)(1+1) = 4 · 3 · 2 · 2 = 48.
Exercise
Using exponent vectors, find gcd(756, 1176) and lcm(756, 1176), then check that their product equals 756 × 1176.
Show answer
756 = 2² · 3³ · 7 and 1176 = 2³ · 3 · 7². The gcd takes minimum exponents: 2² · 3 · 7 = 84. The lcm takes maximum exponents: 2³ · 3³ · 7² = 10584. Check: 84 × 10584 = 889056 and 756 × 1176 = 889056.
Exercise
Explain why the factor function above never adds a composite number to the Counter, even though d steps through every integer starting at 2.
Show answer
When the loop reaches a composite d, every prime factor of d is smaller than d and has already been fully divided out of n by an earlier inner loop. So n is no longer divisible by any prime factor of d, and therefore not by d. The test n % d == 0 fails, and d is never recorded.
Exercise
In the Hilbert numbers (1, 5, 9, 13, ...), find a second number besides 441 with two different factorizations into Hilbert-primes, and say which step of the ordinary uniqueness proof breaks.
Show answer
One example is 693 = 9 · 77 = 21 · 33. Here 9, 21, 33, and 77 are each 1 mod 4 and have no factorization into smaller Hilbert numbers greater than 1. The proof breaks at Euclid's lemma: 21 divides 9 · 77 but divides neither 9 nor 77. The reason is that Bézout's identity cannot be carried out inside the Hilbert numbers.
Exercise
Prove that a positive integer n is a perfect square if and only if every exponent in its canonical factorization is even.
Show answer
If n = m², write m = ∏ p^ep. Then n = ∏ p^(2ep), so every exponent is even. Conversely, if n = ∏ p^(2fp), set m = ∏ p^fp. Then m² = n. Uniqueness makes "the exponent of p in n" well defined, so the condition is meaningful.