Search

Search papers and lessons

Prime Number

Level 1 · 8 min read

A prime is a whole number greater than 1 whose only positive divisors are 1 and itself, and every whole number factors into primes in exactly one way.

On this page

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 2×302 \times 30, then 2×2×152 \times 2 \times 15, then 2×2×3×52 \times 2 \times 3 \times 5, and at that point no further split is possible. The pieces 2,2,3,52, 2, 3, 5 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

Definition: Prime number

An integer p>1p \gt 1 is prime if its only positive divisors are 11 and pp. An integer n>1n \gt 1 that is not prime is composite. Read "dd divides nn", written d∣nd \mid n, as "n=d⋅qn = d \cdot q for some integer qq."

By convention the number 1 is neither prime nor composite. Including it would break the uniqueness of factorization below, because 6=2⋅3=1⋅2⋅36 = 2 \cdot 3 = 1 \cdot 2 \cdot 3 would have two factorizations.

The fundamental theorem of arithmetic

Definition: Fundamental theorem of arithmetic

Every integer n>1n \gt 1 can be written as a product of primes n=p1p2⋯pkn = p_1 p_2 \cdots p_k, and this representation is unique apart from the order of the factors.

Existence is proved by strong induction: if nn is prime, it is its own factorization. If not, n=abn = ab with 1<a,b<n1 \lt a, b \lt n, and each factor has a prime factorization by the induction hypothesis. Uniqueness needs a further fact, Euclid's lemma: if a prime pp divides a product abab, then pp divides aa or pp divides bb. 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 nn is composite, then n=abn = ab with 1<a≤b<n1 \lt a \le b \lt n, and so a≤na \le \sqrt{n}. This gives a simple test. To check whether nn is prime, try dividing it by every prime up to n\sqrt{n}. If none divides it, nn is prime. This is trial division. It takes time roughly proportional to n\sqrt{n}, 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 pp is prime and pp does not divide aa, then the remainders a,2a,…,(p−1)aa, 2a, \dots, (p-1)a taken mod pp are all different and nonzero. This property is what makes the multiplicative structure of Z/pZ\mathbb{Z}/p\mathbb{Z} behave like ordinary arithmetic on nonzero numbers, and it is the basis of the methods used in RSA.

Worked examples

Example
Finding the primes up to 30

Use the sieve of Eratosthenes.

  1. Write the numbers from 2 to 30.
  2. 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.
  3. The next surviving number is 3. Cross out its multiples after it: 6, 9, 12, 15, 18, 21, 24, 27, 30.
  4. The next survivor is 5. Its multiples up to 30 that remain are 10, 15, 20, 25, 30, so cross those out.
  5. The next survivor is 7. Its multiples remaining are 14, 21, 28. Cross them out.
  6. Every number up to 30 that has a prime factor at most 30≈5.5\sqrt{30} \approx 5.5 is now crossed out. The survivors are 2,3,5,7,11,13,17,19,23,292, 3, 5, 7, 11, 13, 17, 19, 23, 29.

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.

Example
Euclid's construction with real numbers

Suppose someone claims the primes up to 13 are a complete list: 2,3,5,7,11,132, 3, 5, 7, 11, 13.

  1. Multiply them: 2⋅3⋅5⋅7⋅11⋅13=300302 \cdot 3 \cdot 5 \cdot 7 \cdot 11 \cdot 13 = 30030.
  2. Add 1 to get N=30031N = 30031.
  3. Divide NN by each listed prime. Each division leaves remainder 1, because 3003030030 is a multiple of every listed prime. So none of them divides NN.
  4. 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 5959.
  5. Divide: 30031=59×50930031 = 59 \times 509. Both 59 and 509 are primes, so 30031 is composite and its prime factors were missing from the list.
  6. 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.

SituationProblemBetter choice
Small number, say under 10610^6Trial division is fast enoughSieve or trial division
Hundreds of digitsTrial division takes too longProbabilistic tests such as Miller–Rabin
Need to factor a large compositeNo fast general method is known classicallyAccept the difficulty; cryptography depends on it
Confusing 1 with a primeBreaks the uniqueness of factorizationUse 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 n\sqrt{n}, 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 xx have density about 1/ln⁡x1/\ln x, 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 360=2⋅180=2⋅2⋅90=2⋅2⋅2⋅45=2⋅2⋅2⋅3⋅15=23⋅32⋅5360 = 2 \cdot 180 = 2 \cdot 2 \cdot 90 = 2 \cdot 2 \cdot 2 \cdot 45 = 2 \cdot 2 \cdot 2 \cdot 3 \cdot 15 = 2^3 \cdot 3^2 \cdot 5. Every factor is prime, and the factorization is unique.

QuestionWhy does trial division only need to check primes up to n\sqrt{n}?Show answer

If nn is composite, write n=abn = ab with 1<a≤b1 \lt a \le b. Then a2≤ab=na^2 \le ab = n, so a≤na \le \sqrt{n}. The smaller factor is therefore at most n\sqrt{n}, and if no number up to n\sqrt{n} divides nn, then nn 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.

Sources