Prime Numbers
Primes are the atoms of multiplication: every integer above 1 is built from them, and the cryptography of the last fifty years depends on how easy they are to find and how hard they are to recover from a product.
Definition
An integer is prime if its only positive divisors are 1 and . An integer that is not prime is composite: it can be written with . The number 1 is neither (making it prime would break unique factorization).
The primes below 50 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 and 47.
Trial division
If with , then , so . Therefore:
A composite always has a prime factor .
So to test it is enough to try divisors up to :
from math import isqrt
def is_prime(n):
if n < 2:
return False
for d in range(2, isqrt(n) + 1):
if n % d == 0:
return False
return True
print([n for n in range(50) if is_prime(n)])
print(is_prime(1_000_003), is_prime(1_000_001), 1_000_001 % 101)[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
True False 0
Trial division costs about steps, which is for a -bit number: fine for 40 bits, hopeless for 1024 bits. Large numbers are tested with Miller-Rabin instead.
There are infinitely many primes
Euclid's proof. Suppose were all the primes. Let . has some prime factor . But dividing by any leaves remainder 1, so is none of the : a contradiction.
itself need not be prime; it just has a new prime factor:
from math import prod
from sympy import factorint
primes = [2, 3, 5, 7, 11, 13]
for k in range(1, len(primes) + 1):
N = prod(primes[:k]) + 1
print(k, N, factorint(N))1 3 {3: 1}
2 7 {7: 1}
3 31 {31: 1}
4 211 {211: 1}
5 2311 {2311: 1}
6 30031 {59: 1, 509: 1}
is composite, but both factors are new primes.
Special forms
- Mersenne primes (possible only when is prime). The largest known primes are Mersenne primes, found by GIMPS with the Lucas-Lehmer test. and are prime and are used as moduli in fast hash functions.
- Fermat numbers : 3, 5, 17, 257, 65537 are prime; is not. The prime 65537 is the standard RSA public exponent.
from sympy import isprime
print("Mersenne exponents below 130:", [p for p in range(2, 130) if isprime(p) and isprime(2**p - 1)])
print([2**(2**k) + 1 for k in range(5)], (2**32 + 1) % 641)Mersenne exponents below 130: [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127]
[3, 5, 17, 257, 65537] 0
In software
- RSA, Diffie-Hellman and elliptic-curve cryptography all start by finding large primes (Generating Large Primes).
- Fast non-cryptographic hashes and pseudorandom generators use arithmetic modulo Mersenne primes such as or , because reducing modulo is just an add and a shift.
Exercises
Exercise 1: Prime or not
Decide by hand whether 221 and 223 are prime.
Answer
is composite; 223 is prime.
Solution
, so test 2, 3, 5, 7, 11 and 13. None divides 223 (). For 221, the test reaches 13: .
Exercise 2: Why needs a prime exponent
Show that if with , then divides .
Answer
with .
Solution
Put . Then , and is a factor strictly between 1 and . For example is divisible by and .
Exercise 3: Prime gaps (code)
Find the largest gap between consecutive primes below 10000.
Answer
36, between 9551 and 9587.
Solution
from sympy import primerange
ps = list(primerange(2, 10000))
gap, p = max((b - a, a) for a, b in zip(ps, ps[1:]))
print(gap, p, p + gap)36 9551 9587
Gaps grow slowly, on average like (The Prime Number Theorem), but there are arbitrarily long gaps: are all composite.
Test yourself
To test whether is prime by trial division, you only need divisors up to…
In Euclid's proof, the number is…
- always prime
- divisible by some prime not in the list
- divisible by
- even
Which number is the usual RSA public exponent?
- 3
- 65537
- 641