Mathematics / Prime numbers

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.

Beginner7 steps~7 min
Prime — divisible only by 1 and itself
Composite — a product of smaller numbers
Divisor on trial — does it divide with nothing left over?
// THE SHORT VERSION

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

STEPS01 / 07

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 01 / 07 What makes a number prime
THE MATH
prime: only 1 and itself 7 = 1 × 7 12 = 3 × 4 (composite)
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
VALUES SO FAR
primeonly 1 and nrule
up to 50····count
test to····rule
97····result
2·3·5+1····Euclid
primes····proof
near n····density
61 × 53····public
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?