How Prime Numbers Work: The Math Behind Internet Security

Photo: www.kaboompics.com / Pexels
A prime number is a whole number greater than 1 that can only be divided evenly by 1 and itself. That's it. The definition fits on a Post-it note. Yet prime numbers have captivated mathematicians for over 2,500 years, remain the subject of the most famous unsolved problem in mathematics, and underpin virtually every secure digital communication on the planet. Their simplicity conceals extraordinary depth.
Key takeaways
- Primes are integers greater than 1 with no divisors other than 1 and themselves: 2, 3, 5, 7, 11, 13…
- There are infinitely many primes — Euclid proved this around 300 BCE in one of the most elegant proofs in mathematics.
- RSA encryption secures internet transactions by exploiting the fact that multiplying two large primes is easy, but factoring the result back is computationally infeasible.
- The Riemann hypothesis — the deepest unsolved problem in mathematics — makes a precise prediction about how primes are distributed among the integers.
- Prime gaps grow on average as primes get larger, but are irregular in ways that still surprise mathematicians.
What makes a prime number special
Every integer greater than 1 is either prime or can be uniquely factored into primes. This is the Fundamental Theorem of Arithmetic, and it makes primes the atomic building blocks of all numbers. Just as every molecule is built from atoms, every number is built from primes. 60 = 2 × 2 × 3 × 5. 1,001 = 7 × 11 × 13. There is exactly one way to write any integer as a product of primes (ignoring order).
The first primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37… Notice that 2 is the only even prime — every other even number is divisible by 2 and therefore composite.
As numbers get larger, primes become less dense. Around the number N, roughly 1 in every ln(N) integers is prime (where ln is the natural logarithm). Around 1,000, about 1 in 7 numbers is prime. Around 1,000,000, about 1 in 14. Around 10²³, about 1 in 53. The primes thin out — but they never stop.
Euclid's proof: there are infinitely many primes
Around 300 BCE, Euclid proved that the primes go on forever. His proof is a masterpiece of mathematical reasoning, requiring no advanced tools.
Suppose, for contradiction, that there are finitely many primes: p₁, p₂, p₃,... pₙ. Multiply them all together and add 1: N = (p₁ × p₂ × p₃ ×... × pₙ) + 1. Now, N is either prime or composite. If it's prime, we've found a prime not on our list — contradiction. If it's composite, it must have a prime factor. But that prime factor can't be any of p₁ through pₙ, because dividing N by any of them leaves remainder 1. So there's a prime factor not on our list — contradiction either way. Therefore, there cannot be a finite list of all primes. QED.
This proof doesn't find new primes — it just shows that the list can never be complete. It remains one of the most elegant arguments in all of mathematics.
How primes secure the internet: RSA encryption
In 1977, Ron Rivest, Adi Shamir, and Leonard Adleman published a cryptographic scheme — now called RSA — that exploits a profound asymmetry in prime number arithmetic. Multiplying two numbers is easy. Factoring a number back into its prime components is hard. Extraordinarily hard for large numbers.
Here is the core idea. Suppose you pick two large prime numbers, p and q — each perhaps 1,000 digits long. You multiply them: n = p × q. This takes a fraction of a second. Now give n to anyone and ask them to find p and q. With the best known algorithms on the fastest computers, this would take longer than the age of the universe for sufficiently large primes.
This asymmetry — fast to multiply, effectively impossible to reverse — is the foundation of public-key cryptography. RSA works roughly as follows:
- Generate two large primes p and q; compute n = p × q.
- Publish n (the public key). Keep p and q secret (the private key).
- Anyone can encrypt a message using n and a mathematical formula.
- Only someone who knows p and q can decrypt it.
Every time you see "https://" in your browser, a version of this logic is protecting your data. The security of online banking, email, and e-commerce rests on the computational difficulty of factoring large numbers — a difficulty that comes directly from the strange, irregular distribution of primes.
The prime number theorem
How many primes are there below a given number N? The Prime Number Theorem, proved independently by Jacques Hadamard and Charles-Jean de la Vallée Poussin in 1896, gives the answer: approximately N / ln(N).
This is a remarkable result. The primes distribute themselves among the integers in a way that can be described by the simple function N / ln(N). The actual count, denoted π(N), tracks this approximation with increasing relative accuracy as N grows.
A sharper estimate, due to Carl Friedrich Gauss, uses the logarithmic integral: Li(N) = ∫₂ᴺ dt/ln(t). This approximation is even better, and Gauss computed it by hand in his teens just by studying prime tables.
The Riemann hypothesis: the deepest unsolved problem
In 1859, Bernhard Riemann published a paper connecting the distribution of prime numbers to a complex-valued function now called the Riemann zeta function, ζ(s). This function can be computed for complex numbers s, and its zeros — the input values where ζ(s) = 0 — turn out to control exactly how the primes deviate from the smooth Prime Number Theorem approximation.
Riemann noticed that all the non-trivial zeros he computed had real part equal to exactly 1/2. He hypothesized that this is always true. This is the Riemann Hypothesis: all non-trivial zeros of ζ(s) lie on the line Re(s) = 1/2, called the critical line.
More than 10 trillion zeros have been computed, and every single one lies exactly on the critical line. Yet the hypothesis remains unproven after 165 years. It is one of the seven Millennium Prize Problems — a correct proof (or disproof) carries a $1 million reward from the Clay Mathematics Institute.
Why does it matter? The Riemann Hypothesis is equivalent to the strongest possible statement about how regular the distribution of primes is. A proof would not just answer an abstract question — it would unlock dozens of theorems in number theory that currently assume the hypothesis as a condition.
Prime gaps: how far apart can primes be?
Between any two consecutive primes, there is a gap. After 2 and 3, the minimum gap is 2 (these are called twin primes: 11 and 13, 17 and 19, 41 and 43…). The Twin Prime Conjecture holds that there are infinitely many such pairs, but this remains unproven.
Prime gaps can also be arbitrarily large. For any number N, you can construct N consecutive composite numbers: (N+1)! + 2, (N+1)! + 3,..., (N+1)! + (N+1). So the primes can be as spread out as you like. In 2014, Yitang Zhang proved a landmark result: there are infinitely many pairs of primes within 70 million of each other. Subsequent work by James Maynard and the Polymath project reduced this bound to 246.
On average, the gap between consecutive primes near N grows as ln(N). But the distribution of these gaps has rich structure. Unexpectedly large gaps appear — "prime deserts." And unexpectedly small ones — pairs of primes like (1,000,000,007 and 1,000,000,009).
Primes in nature and mathematics
Primes appear in unexpected places beyond pure mathematics. Cicadas of the genus Magicicada emerge from the ground in cycles of 13 or 17 years — both prime numbers. One hypothesis is that this prime-number life cycle makes it harder for predators to synchronize with cicada emergence. If cicadas emerged every 12 years, any predator with a 2-, 3-, 4-, or 6-year cycle would coincide regularly. Prime-year cycles minimize such coincidences.
In physics, prime numbers have appeared in studies of quantum energy levels in disordered systems. The spacings between energy levels in certain quantum chaotic systems follow the same statistical distribution as the spacings between Riemann zeros — a mysterious and still not fully understood connection between quantum mechanics and number theory.
How to find large primes: the Lucas-Lehmer test
How do we know a 1,000-digit number is prime? Testing all possible divisors up to its square root is completely infeasible — there are more candidates than atoms in the observable universe. Instead, mathematicians use probabilistic and deterministic primality tests.
The Miller-Rabin test can rapidly determine whether a number is almost certainly prime (with error probability reducible to any desired level). For numbers of special form, exact tests exist: the Lucas-Lehmer test efficiently proves primality for Mersenne primes — primes of the form 2ⁿ − 1.
The largest known prime (as of 2024) is a Mersenne prime: 2^136,279,841 − 1, with over 41 million digits. It was found by the Great Internet Mersenne Prime Search (GIMPS), a distributed computing project.
FAQ
Q: Is 1 a prime number? A: No. The definition of prime requires factors exactly 1 and itself — and if 1 were prime, the Fundamental Theorem of Arithmetic (unique prime factorization) would break down, since you could always multiply a factorization by extra 1s. Mathematicians explicitly exclude 1 from the primes.
Q: Will quantum computers break RSA encryption? A: Shor's algorithm, developed in 1994, allows a quantum computer to factor large numbers in polynomial time — which would break RSA. However, building quantum computers powerful enough to attack current RSA key sizes remains a major engineering challenge. Cryptographers are already developing "post-quantum" encryption schemes that don't rely on factoring difficulty.
Q: Are there patterns in the primes? A: Yes and no. There are deep statistical regularities (the Prime Number Theorem, the Riemann Hypothesis, the Hardy-Littlewood conjectures) but no simple formula that generates all primes. Dirichlet proved that primes are distributed equally among arithmetic progressions with coprime differences, a beautiful regularity. But individual primes remain locally irregular and surprising.
Q: What is a Mersenne prime? A: A prime of the form 2ⁿ − 1. The first few are 3 (2²-1), 7 (2³-1), 31 (2⁵-1), 127 (2⁷-1). Not every number of this form is prime — n itself must be prime, and even then 2ⁿ − 1 may be composite. All the largest known primes are Mersenne primes because the Lucas-Lehmer test makes them especially easy to verify.
Q: Why haven't we found a formula for all primes? A: There is no simple polynomial formula that produces all and only primes. There are formulas that always produce primes (like Wilson's theorem) but they are computationally impractical. The irregular distribution of primes reflects deep, not-yet-fully-understood structure in the number line. This mystery is part of what makes prime numbers endlessly fascinating to mathematicians.
Read next:
Follow new articles
Add SciHub101 to your feed reader to see new science and technology stories.
About the Author
SciHub101 is an independent science and technology publication founded and edited by Buğra Sözeri, a software engineer and app developer based in Türkiye. AI tools assist with drafting and research. Source documentation in the existing archive is uneven, and articles are being reviewed and corrected. The Editorial Team byline identifies editorial responsibility; it does not certify specialist or independent human review. Where we compare products, anything built by our own team is labelled as such — see the editorial guidelines and About page for how corrections and disclosures work.
Related Articles

How Preservatives Keep Food Safe: The Chemistry Explained
Before refrigerators, salt and vinegar kept people alive. Today, a few molecules in the parts-per-million range prevent millions of cases of food poisoning a year. Here's how.

How Thermodynamics Explains Everything: From Steam Engines to the Heat Death of the Universe
Four laws govern how energy moves, transforms, and degrades, from the steam engine to every living cell. Thermodynamics explains why engines have limits and why time has a direction.

What Is the Largest Known Prime Number — And Why Do Mathematicians Keep Searching?
The largest known prime number has 41,024,320 digits. Finding it took a rented cloud of data-center GPUs and a decades-old search project. But what is the point of a number nobody will ever write out?

Infinity in Mathematics: How Some Infinities Are Bigger Than Others
Not all infinities are equal. Georg Cantor proved that some infinite sets are larger than others — a discovery so strange it drove mathematicians mad.

Fermat's Last Theorem: The 358-Year Math Mystery Finally Solved
A margin note scribbled in the 1630s baffled the world's greatest mathematicians for 358 years. Here's how it was finally cracked.

The Fibonacci Sequence and the Golden Ratio in Nature
Sunflowers, nautilus shells, pinecones, and galaxies — the Fibonacci sequence seems to appear everywhere in nature. Some of it is real mathematics; some is pattern-matching wishful thinking. Here's the honest story.