Sub topic 3
Every Integer Factors Uniquely into Primes
This chapter assumes you know what prime and composite numbers are, can use the modulo operator %, and can read short Python functions with loops and lists.
The statement
The fundamental theorem of arithmetic says that every integer greater than 1 can be written as a product of primes in exactly one way, if you ignore the order of the factors.
It makes two separate claims:
- Existence: a prime factorization always exists.
- Uniqueness: there is only one such factorization.
For example, 360 = 2 × 2 × 2 × 3 × 3 × 5. You can reorder those factors, but no other set of primes multiplies to 360. The usual way to write a factorization is the canonical form, which lists the primes in increasing order with exponents: 360 = 2³ · 3² · 5.
If you start the tree as 360 = 8 × 45 instead, you still end with three 2s, two 3s, and one 5. Uniqueness guarantees that.
Why a factorization exists
Suppose some integers greater than 1 have no prime factorization, and let n be the smallest one. Then n is not prime, because a prime counts as its own one-factor factorization. So n = a × b with 1 < a, b < n. Both a and b are smaller than n, so each has a prime factorization. Putting those two factorizations together gives one for n, which contradicts the assumption. So no such n exists.
Why it is unique
Uniqueness depends on Euclid's lemma: if a prime p divides a product a × b, then p divides a or p divides b. Composite numbers do not have this property. For example, 6 divides 4 × 9 = 36, but 6 divides neither 4 nor 9.
Now suppose a number had two different prime factorizations, p₁p₂…pₖ = q₁q₂…qₘ. The prime p₁ divides the right-hand side, so by Euclid's lemma (applied repeatedly) it divides some qⱼ. A prime that divides another prime must equal it, so p₁ = qⱼ. Cancel that factor from both sides and repeat. The two lists run out at the same time and contain the same primes.
Computing a factorization
The function below divides by each candidate d in turn and removes every copy of d before moving on. A composite d can never divide what is left of n, because its smaller prime factors have already been removed. As a result, every recorded factor is prime.
from collections import Counter
def prime_factors(n):
factors = []
d = 2
while n > 1:
while n % d == 0:
factors.append(d)
n //= d
d += 1
return factors
def canonical(n):
return dict(Counter(prime_factors(n)))
print(prime_factors(360)) # [2, 2, 2, 3, 3, 5]
print(canonical(360)) # {2: 3, 3: 2, 5: 1}
print(canonical(84)) # {2: 2, 3: 1, 7: 1}The function is correct but slow when n has a large prime factor. Later chapters speed it up, first with the square root bound and then with a smallest-prime-factor table.
What uniqueness gives you
Because the exponents are fixed, you can read several properties straight from the canonical form:
| Quantity | Rule | Example with 360 = 2³·3²·5 and 84 = 2²·3·7 |
|---|---|---|
| Number of divisors | Multiply (exponent + 1) for each prime | 360: (3+1)(2+1)(1+1) = 24 |
| GCD | Smaller exponent of each prime | 2²·3 = 12 |
| LCM | Larger exponent of each prime | 2³·3²·5·7 = 2520 |
As a check, 12 × 2520 = 30240 = 360 × 84.
Common errors
- Counting 1 as a factor. Leave it out. The factorization of a prime p is just p.
- Assuming uniqueness holds in every number system. In numbers of the form a + b√−5, the number 6 = 2 × 3 = (1 + √−5)(1 − √−5) has two different factorizations. The theorem relies on Euclid's lemma, and Euclid's lemma holds for ordinary integers.
- Forgetting repeated factors. If you divide out d only once, you get 360 → [2, 3, 5], which loses the exponents.
Exercise
Use the canonical form to find how many positive divisors 1000 has.
Show answer
1000 = 2³ · 5³, so it has (3+1)(3+1) = 16 divisors.
Exercise
Why does prime_factors never append a composite number such as 4 or 6?
Show answer
By the time d reaches a composite number, every copy of its smaller prime factors (2 for 4; 2 and 3 for 6) has already been divided out of n, so n % d cannot be 0.