Unique Factorization
The Fundamental Theorem of Arithmetic says every integer is a product of primes in exactly one way, apart from the order of the factors. Existence is easy; uniqueness needs a real idea, Euclid's lemma, and that lemma is the extended Euclidean algorithm in disguise.
Statement
Every integer can be written as
and this representation is unique. For example .
Existence (strong induction): if is prime we are done; otherwise with smaller factors, each of which is a product of primes.
Euclid's lemma
If is prime and , then or .
Proof. Suppose . Since is prime, , so Bézout gives . Multiply by : . Both terms on the left are divisible by (the second because ), so .
Uniqueness follows: if , then divides the right side, so by the lemma it divides some , and being prime they are equal. Cancel and repeat.
The lemma fails for composites: but and .
Factorizations in code
def factor(n):
"""Trial-division factorization: {prime: exponent}."""
f, d = {}, 2
while d * d <= n:
while n % d == 0:
f[d] = f.get(d, 0) + 1
n //= d
d += 1 if d == 2 else 2
if n > 1:
f[n] = f.get(n, 0) + 1
return f
for n in (360, 1001, 2**10 * 3**5, 999_999_000_001):
print(n, factor(n))360 {2: 3, 3: 2, 5: 1}
1001 {7: 1, 11: 1, 13: 1}
248832 {2: 10, 3: 5}
999999000001 {999999000001: 1}
What factorizations give you
With and (exponent 0 for missing primes):
and the number and sum of divisors of are
from sympy import factorint
from math import gcd, prod
a, b = 360, 756
fa, fb = factorint(a), factorint(b)
ps = sorted(set(fa) | set(fb))
g = prod(p ** min(fa.get(p, 0), fb.get(p, 0)) for p in ps)
l = prod(p ** max(fa.get(p, 0), fb.get(p, 0)) for p in ps)
print(fa, fb, "gcd:", g, gcd(a, b), " lcm:", l)
tau = prod(e + 1 for e in fa.values())
sigma = prod((p ** (e + 1) - 1) // (p - 1) for p, e in fa.items())
print("tau(360) =", tau, " sigma(360) =", sigma){2: 3, 3: 2, 5: 1} {2: 2, 3: 3, 7: 1} gcd: 36 36 lcm: 7560
tau(360) = 24 sigma(360) = 1170
These formulas are fast only if you already know the factorization. Euclid's algorithm finds gcds without factoring, which is why it, and not factoring, is used in practice.
In software
- Unique factorization is what makes "factor and you have broken RSA" meaningful: there is exactly one pair of primes to find.
- Not every number system has unique factorization. In , in two genuinely different ways; this failure drove 19th-century algebraic number theory.
Exercises
Exercise 1: Factor by hand
Factor 2310 and 4096.
Answer
; .
Solution
, , . And because and .
Exercise 2: Irrationality of
Use unique factorization to show has no solution in positive integers.
Answer
The exponent of 2 is even on the left and odd on the right.
Solution
In every prime appears to an even power. In the prime 2 appears to the power , which is odd. Unique factorization forbids the two sides from being equal. Hence is impossible.
Exercise 3: Divisor count (code)
Compute from the factorization of and check by counting.
Answer
, so .
Solution
from math import factorial, prod
from sympy import factorint, divisor_count
n = factorial(10)
f = factorint(n)
print(f, prod(e + 1 for e in f.values()), divisor_count(n)){2: 8, 3: 4, 5: 2, 7: 1} 270 270
Test yourself
Euclid's lemma states that if a prime divides , then…
- divides both
- divides or
- or
equals…
How many positive divisors does have ( distinct primes)?
- 6
- 8
- 15
- 16