TExcelTAD
Home › Number Theory & Cryptography › Prime Numbers

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 p>1p > 1 is prime if its only positive divisors are 1 and pp. An integer n>1n > 1 that is not prime is composite: it can be written n=abn = ab with 1<a,b<n1 < a, b < n. 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 n=abn = ab with a≤ba \le b, then a2≤ab=na^2 \le ab = n, so a≤na \le \sqrt{n}. Therefore:

A composite nn always has a prime factor p≤np \le \sqrt{n}.

So to test nn it is enough to try divisors up to n\sqrt{n}:

python · try it yourself
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 n\sqrt{n} steps, which is 2k/22^{k/2} for a kk-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 p1,…,pkp_1, \ldots, p_k were all the primes. Let N=p1p2⋯pk+1N = p_1 p_2 \cdots p_k + 1. N>1N > 1 has some prime factor pp. But dividing NN by any pip_i leaves remainder 1, so pp is none of the pip_i: a contradiction.

NN itself need not be prime; it just has a new prime factor:

python · try it yourself
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}

2⋅3⋅5⋅7⋅11⋅13+1=30031=59⋅5092 \cdot 3 \cdot 5 \cdot 7 \cdot 11 \cdot 13 + 1 = 30031 = 59 \cdot 509 is composite, but both factors are new primes.

Special forms

  • Mersenne primes 2p−12^p - 1 (possible only when pp is prime). The largest known primes are Mersenne primes, found by GIMPS with the Lucas-Lehmer test. 261−12^{61} - 1 and 2127−12^{127} - 1 are prime and are used as moduli in fast hash functions.
  • Fermat numbers 22k+12^{2^k} + 1: 3, 5, 17, 257, 65537 are prime; 232+1=641⋅67004172^{32} + 1 = 641 \cdot 6700417 is not. The prime 65537 is the standard RSA public exponent.
python · try it yourself
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 231−12^{31} - 1 or 261−12^{61} - 1, because reducing modulo 2k−12^k - 1 is just an add and a shift.

Exercises

Exercise 1: Prime or not

Decide by hand whether 221 and 223 are prime.

Answer

221=13⋅17221 = 13 \cdot 17 is composite; 223 is prime.

Solution

223<15\sqrt{223} < 15, so test 2, 3, 5, 7, 11 and 13. None divides 223 (223=17⋅13+2223 = 17 \cdot 13 + 2). For 221, the test reaches 13: 13⋅17=22113 \cdot 17 = 221.

Exercise 2: Why 2n−12^n - 1 needs a prime exponent

Show that if n=abn = ab with a,b>1a, b > 1, then 2a−12^a - 1 divides 2n−12^n - 1.

Answer

xb−1=(x−1)(xb−1+⋯+1)x^b - 1 = (x - 1)(x^{b-1} + \cdots + 1) with x=2ax = 2^a.

Solution

Put x=2ax = 2^a. Then 2n−1=xb−1=(x−1)(xb−1+xb−2+⋯+1)2^n - 1 = x^b - 1 = (x - 1)(x^{b-1} + x^{b-2} + \cdots + 1), and x−1=2a−1x - 1 = 2^a - 1 is a factor strictly between 1 and 2n−12^n - 1. For example 26−1=632^6 - 1 = 63 is divisible by 22−1=32^2 - 1 = 3 and 23−1=72^3 - 1 = 7.

Exercise 3: Prime gaps (code)

Find the largest gap between consecutive primes below 10000.

Answer

36, between 9551 and 9587.

Solution
python · try it yourself
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 ln⁡p\ln p (The Prime Number Theorem), but there are arbitrarily long gaps: n!+2,…,n!+nn! + 2, \ldots, n! + n are all composite.

Test yourself

To test whether nn is prime by trial division, you only need divisors up to…

  1. n/2n/2
  2. n\sqrt{n}
  3. log⁡2n\log_2 n
  4. n−1n - 1

In Euclid's proof, the number p1p2⋯pk+1p_1 p_2 \cdots p_k + 1 is…

  1. always prime
  2. divisible by some prime not in the list
  3. divisible by p1p_1
  4. even

Which number is the usual RSA public exponent?

  1. 3
  2. 65537
  3. 261−12^{61} - 1
  4. 641