TExcelTAD
Home › Number Theory & Cryptography › Unique Factorization

Unique Factorization

The Fundamental Theorem of Arithmetic says every integer n>1n > 1 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 n>1n > 1 can be written as

n=p1e1p2e2⋯pkek,p1<p2<⋯<pk prime, ei≥1n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}, \qquad p_1 < p_2 < \cdots < p_k \text{ prime},\ e_i \ge 1

and this representation is unique. For example 360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5.

Existence (strong induction): if nn is prime we are done; otherwise n=abn = ab with smaller factors, each of which is a product of primes.

Euclid's lemma

If pp is prime and p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b.

Proof. Suppose p∤ap \nmid a. Since pp is prime, gcd⁡(p,a)=1\gcd(p, a) = 1, so Bézout gives px+ay=1px + ay = 1. Multiply by bb: pbx+aby=bpbx + aby = b. Both terms on the left are divisible by pp (the second because p∣abp \mid ab), so p∣bp \mid b.

Uniqueness follows: if p1⋯pr=q1⋯qsp_1 \cdots p_r = q_1 \cdots q_s, then p1p_1 divides the right side, so by the lemma it divides some qjq_j, and being prime they are equal. Cancel and repeat.

The lemma fails for composites: 6∣4⋅96 \mid 4 \cdot 9 but 6∤46 \nmid 4 and 6∤96 \nmid 9.

Factorizations in code

python · try it yourself
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 a=∏pαpa = \prod p^{\alpha_p} and b=∏pβpb = \prod p^{\beta_p} (exponent 0 for missing primes):

gcd⁡(a,b)=∏pmin⁡(αp,βp),lcm⁡(a,b)=∏pmax⁡(αp,βp)\gcd(a, b) = \prod p^{\min(\alpha_p, \beta_p)}, \qquad \operatorname{lcm}(a, b) = \prod p^{\max(\alpha_p, \beta_p)}

and the number and sum of divisors of n=∏piein = \prod p_i^{e_i} are

τ(n)=∏i(ei+1),σ(n)=∏ipiei+1−1pi−1\tau(n) = \prod_i (e_i + 1), \qquad \sigma(n) = \prod_i \frac{p_i^{e_i + 1} - 1}{p_i - 1}
python · try it yourself
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 nn and you have broken RSA" meaningful: there is exactly one pair of primes to find.
  • Not every number system has unique factorization. In Z[−5]\mathbb{Z}[\sqrt{-5}], 6=2⋅3=(1+−5)(1−−5)6 = 2 \cdot 3 = (1 + \sqrt{-5})(1 - \sqrt{-5}) in two genuinely different ways; this failure drove 19th-century algebraic number theory.

Exercises

Exercise 1: Factor by hand

Factor 2310 and 4096.

Answer

2310=2⋅3⋅5⋅7⋅112310 = 2 \cdot 3 \cdot 5 \cdot 7 \cdot 11; 4096=2124096 = 2^{12}.

Solution

2310/2=11552310 / 2 = 1155, /3=385/ 3 = 385, /5=77=7⋅11/ 5 = 77 = 7 \cdot 11. And 4096=2124096 = 2^{12} because 210=10242^{10} = 1024 and 4⋅1024=40964 \cdot 1024 = 4096.

Exercise 2: Irrationality of 2\sqrt{2}

Use unique factorization to show a2=2b2a^2 = 2b^2 has no solution in positive integers.

Answer

The exponent of 2 is even on the left and odd on the right.

Solution

In a2a^2 every prime appears to an even power. In 2b22b^2 the prime 2 appears to the power 1+2k1 + 2k, which is odd. Unique factorization forbids the two sides from being equal. Hence 2=a/b\sqrt{2} = a/b is impossible.

Exercise 3: Divisor count (code)

Compute τ(10!)\tau(10!) from the factorization of 10!10! and check by counting.

Answer

10!=28⋅34⋅52⋅710! = 2^8 \cdot 3^4 \cdot 5^2 \cdot 7, so τ=9⋅5⋅3⋅2=270\tau = 9 \cdot 5 \cdot 3 \cdot 2 = 270.

Solution
python · try it yourself
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 pp divides abab, then…

  1. pp divides both
  2. pp divides aa or bb
  3. p=ap = a or p=bp = b
  4. p2∣abp^2 \mid ab

gcd⁡(23⋅32⋅7, 22⋅34⋅5)\gcd(2^3 \cdot 3^2 \cdot 7,\ 2^2 \cdot 3^4 \cdot 5) equals…

  1. 22⋅322^2 \cdot 3^2
  2. 23⋅34⋅5⋅72^3 \cdot 3^4 \cdot 5 \cdot 7
  3. 2⋅32 \cdot 3
  4. 25⋅362^5 \cdot 3^6

How many positive divisors does p4q2p^4 q^2 have (p,qp, q distinct primes)?

  1. 6
  2. 8
  3. 15
  4. 16