TL;DR
A prime number is a whole number bigger than 1 that cannot be split into a product of two smaller whole numbers greater than 1. The numbers 2, 3, 5, 7, 11 and 13 are primes. The numbers 4, 6, 9 and 15 are not, because each one is a product of smaller pieces. Primes are the building blocks of all whole numbers: every whole number greater than 1 is a product of primes, and that product is unique apart from the order of the factors.
Where you'll see it: in public-key encryption such as RSA, in hash tables and random-number generators that use prime sizes, and in any problem that asks whether a number can be divided evenly.
Intuition
Atoms of multiplication
Addition has no atoms: every number is a sum of ones. Multiplication has atoms. Take 60 and break it into factors. It is , then , then , and at that point no further split is possible. The pieces are primes. Any other way of breaking 60 down ends with the same four pieces, possibly in a different order.
That is why primes matter. They are the irreducible part of the integers under multiplication, in the same way that chemical elements are the irreducible parts of matter under chemical reaction. If you know the primes of a number, you know everything about its divisors.
Why the list never ends
Primes do not run out. Euclid gave a simple argument that about 2300 years ago. Suppose someone hands you a complete list of primes. Multiply them all together and add 1. The new number leaves remainder 1 when divided by any prime on the list, so none of them divides it. Either the new number is prime, or it has a prime factor missing from the list. Either way the list was not complete. The worked examples below carry this out with numbers.
Primes also thin out. Up to 100 there are 25 of them. Up to 1,000 there are 168. The gaps between them are irregular, which is why finding the next prime, or proving a number is prime, is a real computational question.
The math
Definition
An integer is prime if its only positive divisors are and . An integer that is not prime is composite. Read " divides ", written , as " for some integer ."
By convention the number 1 is neither prime nor composite. Including it would break the uniqueness of factorization below, because would have two factorizations.
The fundamental theorem of arithmetic
Every integer can be written as a product of primes , and this representation is unique apart from the order of the factors.
Existence is proved by strong induction: if is prime, it is its own factorization. If not, with , and each factor has a prime factorization by the induction hypothesis. Uniqueness needs a further fact, Euclid's lemma: if a prime divides a product , then divides or divides . Proving that lemma uses the greatest common divisor and Bézout's identity, which is why the two topics are linked.
Testing for primality
If is composite, then with , and so . This gives a simple test. To check whether is prime, try dividing it by every prime up to . If none divides it, is prime. This is trial division. It takes time roughly proportional to , which is fine for numbers with a few digits and far too slow for numbers with hundreds of digits. Those larger numbers are checked with probabilistic tests that run in polynomial time, which is an essential ingredient of cryptography.
Modular view
Primes interact with Modular Arithmetic cleanly. If is prime and does not divide , then the remainders taken mod are all different and nonzero. This property is what makes the multiplicative structure of behave like ordinary arithmetic on nonzero numbers, and it is the basis of the methods used in RSA.
Worked examples
Use the sieve of Eratosthenes.
- Write the numbers from 2 to 30.
- Keep 2 and cross out every multiple of 2 after it: 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30.
- The next surviving number is 3. Cross out its multiples after it: 6, 9, 12, 15, 18, 21, 24, 27, 30.
- The next survivor is 5. Its multiples up to 30 that remain are 10, 15, 20, 25, 30, so cross those out.
- The next survivor is 7. Its multiples remaining are 14, 21, 28. Cross them out.
- Every number up to 30 that has a prime factor at most is now crossed out. The survivors are .
Interpretation: there are ten primes up to 30. The sieve works because any composite number has a factor no larger than its square root, so crossing out multiples of small primes removes all composites in range.
Suppose someone claims the primes up to 13 are a complete list: .
- Multiply them: .
- Add 1 to get .
- Divide by each listed prime. Each division leaves remainder 1, because is a multiple of every listed prime. So none of them divides .
- Try to find a factor of 30031 by trial division. Its square root is about 173.3, so you need to check primes up to 173. The first factor you find is .
- Divide: . Both 59 and 509 are primes, so 30031 is composite and its prime factors were missing from the list.
- The claimed list was incomplete. The same construction works for any finite list, so there are infinitely many primes.
Interpretation: the argument is a proof, not a search. Euclid never needed to find the missing prime. He only needed to show one must exist.
Tradeoffs and failure modes
Primality is easy to state and hard to exploit by hand.
| Situation | Problem | Better choice |
|---|---|---|
| Small number, say under | Trial division is fast enough | Sieve or trial division |
| Hundreds of digits | Trial division takes too long | Probabilistic tests such as Miller–Rabin |
| Need to factor a large composite | No fast general method is known classically | Accept the difficulty; cryptography depends on it |
| Confusing 1 with a prime | Breaks the uniqueness of factorization | Use the convention that 1 is neither prime nor composite |
A common mistake is to assume a number is prime because it has no small factors. Trial division has to go all the way up to , and most numbers fail early, which is misleading if you stop too soon.
History
The idea of primes goes back to ancient Greece. Euclid proved in Book IX of the Elements, around 300 BCE, that there are infinitely many primes, and he proved the lemma (now called Euclid's lemma) from which unique factorization follows, though the theorem was not stated in its modern form until later. The sieve is attributed to Eratosthenes, a Greek scholar of the third century BCE. Carl Friedrich Gauss in his Disquisitiones Arithmeticae in 1801 stated the uniqueness theorem explicitly and studied primes in detail. The prime number theorem, which says primes near have density about , was proved independently by Hadamard and de la Vallée Poussin in 1896.
Primes became central to computing in the late twentieth century, when public-key cryptography made it essential to generate large primes and to test them quickly. Their role in RSA is one of the most visible practical uses of pure number theory.
Check yourself
QuestionRecall: is 1 a prime number? Why or why not?Show answer
No. The definition requires a prime to have exactly two positive divisors, 1 and itself. The number 1 has only one positive divisor. It is excluded by convention so that prime factorizations are unique.
QuestionComputation: write 360 as a product of primes.Show answer
Start with . Every factor is prime, and the factorization is unique.
QuestionWhy does trial division only need to check primes up to ?Show answer
If is composite, write with . Then , so . The smaller factor is therefore at most , and if no number up to divides , then has no factorization into two smaller numbers, so it is prime.
QuestionApplied: a hash table has 100 buckets and keys are multiples of 10. Why might a table size that is a prime be a better choice than 100?Show answer
With 100 buckets, keys that are multiples of 10 all land in buckets 0, 10, 20, and so on, which leaves most buckets empty and clusters the keys. A prime table size such as 101 shares no common factor with 10, so the keys spread across all buckets. This is the reason prime sizes appear in many hashing schemes.