Primes and Unique Factorization
A prime is an integer whose only positive divisors are 1 and . Every other integer 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 can be written as a product of primes with , and this representation is unique.
Existence was proved by well ordering and strong induction. Uniqueness needs one more lemma.
Lemma (Euclid). If a prime divides a product , then or .
Proof. If , then (the only divisors of are 1 and ). By the coprime lemma of the GCD lesson, implies .
Uniqueness proof. Suppose some number has two different factorizations, and let be the smallest such number: . The prime divides , so by the lemma (applied repeatedly) it divides some , and since is prime, . Cancel it from both sides: is smaller and still has two different factorizations, contradicting minimality.
Uniqueness is not automatic. In the number system , factors in two genuinely different ways.
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 , write down ; repeatedly take the smallest unmarked number (it is prime) and cross out its multiples . Starting at is enough, because smaller multiples have a smaller prime factor and are already crossed out. And you can stop once : a composite has a prime factor at most .
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 , written , satisfies
meaning the ratio tends to 1. So near a number with decimal digits, roughly one integer in is prime. A random 1024-bit odd number is prime with probability about , which is why generating RSA primes by "pick random, test, repeat" is fast.
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 (), 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
, , .
Solution
. halves ten times to 1. .
Exercise 2: GCD from factorizations
Use and to find and .
Answer
, .
Solution
By unique factorization, the gcd takes the minimum exponent of each prime and the lcm the maximum:
Check: .
Exercise 3: Why is enough
Explain why trial division only needs to test divisors up to .
Answer
If with , then .
Solution
If both factors were greater than , their product would exceed . So a composite has a factor (and hence a prime factor) at most .
Exercise 4: A run of composites (code)
Find 10 consecutive composite numbers using , and find the first run of 10 consecutive composites by search.
Answer
all composite; the first run of at least 10 starts at 114.
Solution
is divisible by for .
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 divides , then…
- divides both and
- divides or
- or
- is prime
In the Sieve of Eratosthenes, why can crossing out start at ?
- Because is prime
- Smaller multiples of have a smaller prime factor and are already crossed out
- To save memory
- It cannot
Roughly how many primes are there below ?
- About 1,000
- About 78,000
- About 500,000
- About 10