Prime numbers
What a prime is, how to test for one without trying every divisor, why the primes never run out, how they thin out, and why secret codes are built on them.
In 30 seconds
A prime is a whole number above 1 that only 1 and itself divide exactly: 2, 3, 5, 7, 11 and so on. Every other number above 1 is a product of primes.
To test a number you only need to try divisors up to its square root. Euclid showed that there are infinitely many primes, though they become rarer as numbers grow.
Multiplying two large primes is easy and undoing it is very hard. Much of today’s encryption rests on that difference.
Prime numbers, step by step
Trial division suits small numbers. Primes for real keys are found with faster probabilistic tests. A large enough quantum computer could factor quickly, which is why newer schemes avoid factoring.
- Step 1: What makes a number prime. Twelve dots can be arranged as a 3 by 4 rectangle, so 12 is a product of smaller numbers: it is composite. Seven dots only ever make a single row. A prime is a whole number greater than 1 whose only divisors are 1 and itself; 1 is not counted as prime.
- Step 2: Sieve out the multiples. An old way to find the primes, the sieve of Eratosthenes: write the numbers down, then cross out every multiple of 2, of 3, of 5 and of 7 apart from those primes themselves. Whatever survives up to 50 is prime. They follow no simple pattern.
- Step 3: Divisors come in pairs. If a divides n, so does n ÷ a, and the two multiply back to n. They cannot both be larger than the square root of n, or their product would exceed n. So any number with a divisor besides 1 and itself has one no larger than its square root.
- Step 4: Test up to the square root. To check 97, trial division only has to go up to its square root, a little under 10, and only primes need trying. Dividing by 2, 3, 5 and 7 always leaves a remainder, and the next prime, 11, is already past the square root. So 97 is prime.
- Step 5: There is no last prime. Euclid’s argument: take any finite list of primes, multiply them all and add 1. Dividing the result by any prime on the list leaves remainder 1, so its prime factors are missing from the list. The result need not be prime itself, as 30031 shows. No finite list is complete.
- Step 6: Primes thin out. Primes never stop, but they get rarer. A quarter of the numbers up to 100 are prime, and fewer than 8 in 100 up to a million. The prime number theorem gives the rate: near a large n, about one number in ln(n), the natural logarithm of n, is prime.
- Step 7: Why cryptography cares. Multiplying two primes is quick; recovering them from the product is slow, and it gets hopeless as the primes grow. RSA publishes the product of two primes hundreds of digits long and keeps the primes secret. Finding such primes is practical because they are still fairly common.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Modular arithmeticArithmetic on a ring that wraps round like a clock, and why codes are built on it. Beginner
- Fermat’s little theoremOn a ring of prime size p, the power p − 1 of any number lands on 1, and RSA uses it. Intermediate
- RSA encryptionPublic keys, private keys, and why factoring big numbers is hard. Beginner
Spotted a mistake on this page?