TExcelTAD
Home › Discrete Mathematics › Primes and Unique Factorization

Primes and Unique Factorization

A prime is an integer p≥2p \ge 2 whose only positive divisors are 1 and pp. Every other integer n≥2n \ge 2 is composite. Primes are the atoms of multiplication: every integer is a product of primes in exactly one way. This lesson proves that, looks at how primes are spread out, and finds them with the Sieve of Eratosthenes.

The Fundamental Theorem of Arithmetic

Theorem. Every integer n≥2n \ge 2 can be written as a product of primes n=p1p2⋯pkn = p_1 p_2 \cdots p_k with p1≤p2≤⋯≤pkp_1 \le p_2 \le \cdots \le p_k, and this representation is unique.

Existence was proved by well ordering and strong induction. Uniqueness needs one more lemma.

Lemma (Euclid). If a prime pp divides a product abab, then p∣ap \mid a or p∣bp \mid b.

Proof. If p∤ap \nmid a, then gcd⁡(p,a)=1\gcd(p, a) = 1 (the only divisors of pp are 1 and pp). By the coprime lemma of the GCD lesson, p∣abp \mid ab implies p∣bp \mid b. ■\blacksquare

Uniqueness proof. Suppose some number has two different factorizations, and let nn be the smallest such number: n=p1⋯pj=q1⋯qkn = p_1 \cdots p_j = q_1 \cdots q_k. The prime p1p_1 divides q1⋯qkq_1 \cdots q_k, so by the lemma (applied repeatedly) it divides some qiq_i, and since qiq_i is prime, p1=qip_1 = q_i. Cancel it from both sides: np1\frac{n}{p_1} is smaller and still has two different factorizations, contradicting minimality. ■\blacksquare

Uniqueness is not automatic. In the number system {a+b−5}\{a + b\sqrt{-5}\}, 6=2⋅3=(1+−5)(1−−5)6 = 2 \cdot 3 = (1 + \sqrt{-5})(1 - \sqrt{-5}) factors in two genuinely different ways.

python · try it yourself
from sympy import factorint

for n in [360, 9973, 2 ** 32 + 1, 600851475143, 2 ** 64 - 1]:
    f = factorint(n)
    print(n, "=", " * ".join(f"{p}^{e}" if e > 1 else str(p) for p, e in f.items()))
360 = 2^3 * 3^2 * 5
9973 = 9973
4294967297 = 641 * 6700417
600851475143 = 71 * 839 * 1471 * 6857
18446744073709551615 = 3 * 5 * 17 * 257 * 641 * 65537 * 6700417

Finding primes: the sieve

To list all primes up to NN, write down 2,…,N2, \ldots, N; repeatedly take the smallest unmarked number pp (it is prime) and cross out its multiples p2,p2+p,…p^2, p^2 + p, \ldots. Starting at p2p^2 is enough, because smaller multiples have a smaller prime factor and are already crossed out. And you can stop once p2>Np^2 > N: a composite n≤Nn \le N has a prime factor at most n\sqrt{n}.

python · try it yourself
def sieve(N):
    is_p = [True] * (N + 1)
    is_p[0] = is_p[1] = False
    p = 2
    while p * p <= N:
        if is_p[p]:
            for m in range(p * p, N + 1, p):
                is_p[m] = False
        p += 1
    return [i for i, v in enumerate(is_p) if v]

ps = sieve(100)
print(len(ps), "primes below 100:", ps)
print("primes below 10^6:", len(sieve(10 ** 6)))
25 primes below 100: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
primes below 10^6: 78498

How many primes are there?

Euclid proved there are infinitely many. The Prime Number Theorem says how dense they are: the number of primes up to xx, written π(x)\pi(x), satisfies

π(x)∼xln⁡x\pi(x) \sim \frac{x}{\ln x}

meaning the ratio tends to 1. So near a number with dd decimal digits, roughly one integer in 2.3d2.3d is prime. A random 1024-bit odd number is prime with probability about 21024ln⁡2≈1355\frac{2}{1024 \ln 2} \approx \frac{1}{355}, which is why generating RSA primes by "pick random, test, repeat" is fast.

python · try it yourself
import math
from sympy import primepi

for k in range(2, 9):
    x = 10 ** k
    p = int(primepi(x))
    print(f"x=10^{k}: pi(x)={p:9d}  x/ln x={x / math.log(x):12.0f}  ratio={p / (x / math.log(x)):.4f}")
x=10^2: pi(x)=       25  x/ln x=          22  ratio=1.1513
x=10^3: pi(x)=      168  x/ln x=         145  ratio=1.1605
x=10^4: pi(x)=     1229  x/ln x=        1086  ratio=1.1320
x=10^5: pi(x)=     9592  x/ln x=        8686  ratio=1.1043
x=10^6: pi(x)=    78498  x/ln x=       72382  ratio=1.0845
x=10^7: pi(x)=   664579  x/ln x=      620421  ratio=1.0712
x=10^8: pi(x)=  5761455  x/ln x=     5428681  ratio=1.0613

The ratio approaches 1 slowly. The gaps between primes vary a lot: there are arbitrarily long runs of composites (n!+2,…,n!+nn! + 2, \ldots, n! + n), and it is still unknown whether there are infinitely many twin primes like 11, 13.

In software

Primality testing of huge numbers uses probabilistic tests (Miller-Rabin), which are fast, while factoring the product of two large primes has no known fast algorithm: that asymmetry is the basis of RSA. Hash tables often use a prime number of buckets so that key % size spreads patterned keys evenly.

Exercises

Exercise 1: Factorize

Write 1001, 1024 and 2310 as products of primes.

Answer

1001=7⋅11⋅131001 = 7 \cdot 11 \cdot 13, 1024=2101024 = 2^{10}, 2310=2⋅3⋅5⋅7⋅112310 = 2 \cdot 3 \cdot 5 \cdot 7 \cdot 11.

Solution

1001/7=143=11⋅131001 / 7 = 143 = 11 \cdot 13. 10241024 halves ten times to 1. 2310=10⋅231=2⋅5⋅3⋅77=2⋅3⋅5⋅7⋅112310 = 10 \cdot 231 = 2 \cdot 5 \cdot 3 \cdot 77 = 2 \cdot 3 \cdot 5 \cdot 7 \cdot 11.

Exercise 2: GCD from factorizations

Use 360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5 and 756=22⋅33⋅7756 = 2^2 \cdot 3^3 \cdot 7 to find gcd⁡\gcd and lcm⁡\operatorname{lcm}.

Answer

gcd⁡=36\gcd = 36, lcm⁡=7560\operatorname{lcm} = 7560.

Solution

By unique factorization, the gcd takes the minimum exponent of each prime and the lcm the maximum:

gcd⁡=22⋅32=36,lcm⁡=23⋅33⋅5⋅7=7560\gcd = 2^2 \cdot 3^2 = 36, \qquad \operatorname{lcm} = 2^3 \cdot 3^3 \cdot 5 \cdot 7 = 7560

Check: 36⋅7560=272160=360⋅75636 \cdot 7560 = 272160 = 360 \cdot 756.

Exercise 3: Why n\sqrt{n} is enough

Explain why trial division only needs to test divisors up to n\sqrt{n}.

Answer

If n=abn = ab with 1<a≤b1 < a \le b, then a≤na \le \sqrt{n}.

Solution

If both factors were greater than n\sqrt{n}, their product would exceed nn. So a composite nn has a factor (and hence a prime factor) at most n\sqrt{n}.

Exercise 4: A run of composites (code)

Find 10 consecutive composite numbers using 11!+2,…,11!+1111! + 2, \ldots, 11! + 11, and find the first run of 10 consecutive composites by search.

Answer

39916802,…,3991681139916802, \ldots, 39916811 all composite; the first run of at least 10 starts at 114.

Solution

11!+k11! + k is divisible by kk for 2≤k≤112 \le k \le 11.

python · try it yourself
from math import factorial
from sympy import isprime

f = factorial(11)
print(all(not isprime(f + k) for k in range(2, 12)))
n = 2
while not all(not isprime(n + i) for i in range(10)):
    n += 1
print("first run of 10 composites starts at", n, list(range(n, n + 10)))
True
first run of 10 composites starts at 114 [114, 115, 116, 117, 118, 119, 120, 121, 122, 123]

Test yourself

Euclid's lemma says: if a prime pp divides abab, then…

  1. pp divides both aa and bb
  2. pp divides aa or bb
  3. p=ap = a or p=bp = b
  4. abab is prime

In the Sieve of Eratosthenes, why can crossing out start at p2p^2?

  1. Because p2p^2 is prime
  2. Smaller multiples of pp have a smaller prime factor and are already crossed out
  3. To save memory
  4. It cannot

Roughly how many primes are there below 10610^6?

  1. About 1,000
  2. About 78,000
  3. About 500,000
  4. About 10