The Riemann hypothesis explained for programmers

The Riemann hypothesis explained for programmers
$1,000,000Clay Institute prize, unclaimed since 2000
1859Riemann states it in a paper of about eight pages
10¹³zeros checked on the line (Gourdon, 2004), none off it
> 41%of the zeros are proved to be on the line; the hypothesis asks for 100%
The Riemann hypothesis explained for programmers
I also explain it in this video, on my Articulator EN channel. The figures in this post are frames from the video.

The atoms of numberslink

A prime number is one you can only divide by 1 and by itself: 2, 3, 5, 7, 11, 13… 12 isn't prime, because it's 2 × 2 × 3. And here's the key part: every whole number greater than 1 can be written as a product of primes, in exactly one way (the fundamental theorem of arithmetic). Primes are the atoms of arithmetic. If you think like a programmer, it's like a unique ID: every number has its own recipe, and nobody else shares it.

12 broken down into its primes: 2 × 2 × 3, its unique recipe
Every number has a single recipe of primes.

How many primes are there? Infinitely many, and Euclid proved it more than two thousand years ago with a proof that fits in one line. Suppose you have the complete list, multiply them all together and add one. That new number leaves a remainder of 1 when you divide it by anything on the list, so its prime factors aren't on it. With 2, 3, 5 and 7 you get 211, which is prime. With the first six you get 30,031, which is not prime, but it splits into 59 and 509, which weren't on the list. The list is never complete.

The problem is how they're spread out. Sometimes they come two apart, like 11 and 13; but between 113 and 127 there are thirteen numbers in a row with no prime at all. There's no simple formula that gives you the next one: they seem to show up at random, as if someone were rolling dice. And yet, behind that chaos there's a very fine order.

So why should you care as a programmer? Because primes are all over your code. RSA, which protects many connections and digital signatures, is built on two huge primes multiplied together. Many hash tables use a prime size to spread keys more evenly. And when you generate a key, the program hunts for random primes hundreds of digits long. For all of that, you need to know how many primes there are and how often they show up. That's exactly Riemann's question.

Counting primeslink

Let's change the question. Instead of asking what the next prime is, let's ask how many there are up to some number N. Mathematicians call it π(N), even though it has nothing to do with the number π. Up to 10 there are 4 primes; up to 100, 25; up to 1,000, 168; and up to a million, 78,498. If you plot it, you get a staircase that goes up one step every time you hit a prime.

The π(N) staircase: one step up at every prime
π(N), the prime staircase.

To count them we have an algorithm that's over two thousand years old, the Sieve of Eratosthenes: write down all the numbers, cross out the multiples of 2, then of 3, then of 5, and keep going. Whatever isn't crossed out is prime. In Python it counts the primes up to a million in under a second, and with it you can check every number in this post:

def sieve(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = is_prime[1] = False
    for p in range(2, int(n ** 0.5) + 1):
        if is_prime[p]:
            is_prime[p * p::p] = [False] * len(is_prime[p * p::p])
    return [i for i, v in enumerate(is_prime) if v]

primes = sieve(10 ** 6)
print(len(primes))  # 78498

Gauss, as a teenager, spent hours staring at tables of primes and spotted a pattern: near some value N, the chance that a randomly chosen number is prime is roughly 1 / ln N, one over the natural log of N.

Near…ln NRoughly one prime in every…
a thousand6.97
a million13.814
a billion20.721

Every time you multiply by a thousand, the average gap grows by about seven: primes get rarer, but very slowly. That hunch became the Prime Number Theorem, proved in 1896 by Hadamard and de la Vallée Poussin, each on his own: π(N) gets closer and closer to N / ln N. And there's an even better approximation, the logarithmic integral li(N), which keeps adding up those probabilities from Gauss. Compare them yourself with the list from before:

from math import log
from mpmath import li   # pip install mpmath

for n in (10 ** 3, 10 ** 4, 10 ** 5, 10 ** 6):
    pi_n = sum(1 for p in primes if p <= n)
    print(n, pi_n, round(n / log(n)), round(float(li(n))))

# 1000     168    145    178
# 10000    1229   1086   1246
# 100000   9592   8686   9630
# 1000000  78498  72382  78628

At a million, N / ln N falls short by about 6,000 primes; li(N) is off by only about 130.

The prime staircase against N / ln N and the logarithmic integral
The logarithmic integral barely leaves the staircase.

How far off is the estimate?link

Now think like someone who runs benchmarks: you have an estimate, and you want to know how far off it can be. At a billion, li(N) overshoots by about 1,700 primes, out of more than 50 million. Could the error grow out of control further on?

The Riemann hypothesis answers exactly that: the error never grows faster than √N, times a small logarithmic factor. It's what happens when you flip a coin: after N flips, you drift from half by roughly √N. In other words, the hypothesis says that primes are no messier than a coin toss.

Riemann and the zeta functionlink

In 1859, Bernhard Riemann, a 33-year-old German mathematician, publishes a paper of about eight pages on how many primes there are below a given size. It's the only thing he ever wrote about primes. In it, he doesn't study them one by one: he puts them inside a function, the zeta function, and finds that everything about them is hidden in its zeros.

The zeta function is easier than it looks. Pick a number s and add 1 + 1/2ˢ + 1/3ˢ + 1/4ˢ…, forever. It's an infinite for loop: sum(1 / n**s for n in range(1, ∞)). With s = 2 you get 1 + 1/4 + 1/9 + 1/16…, and Euler found that the total is π²/6 ≈ 1.6449. With s = 1, on the other hand, the sum grows without limit.

The trick that connects zeta to the primes is also Euler's: that infinite sum equals a product with one factor for each prime, (1 + 1/2ˢ + 1/4ˢ + …)(1 + 1/3ˢ + 1/9ˢ + …)(1 + 1/5ˢ + …)… If you multiply it all out, every number shows up exactly once, thanks to its unique recipe of primes. The sum over all whole numbers and the product over the primes are the same thing: zeta carries the primes inside it.

Riemann went one step further: he fed it complex numbers. A complex number is a point on a plane with two coordinates, a real part and an imaginary part. Python already has them: 3 + 4j. Adding them is adding vectors, and multiplying them means rotating and stretching. So zeta stops being a curve and becomes a landscape that maps every point of the plane to another point.

There's a catch: the sum only gives an answer when the real part of s is greater than 1. Riemann found the only smooth way to extend the function to the whole plane, which is called analytic continuation: like a piece of a curve that can only continue one way without breaking. That's where the famous meme comes from, that 1 + 2 + 3 + … = −1/12. It isn't a real sum, which grows without limit: it's the value of the extended zeta at s = −1.

Now let's look for where that landscape hits zero. There are boring zeros, the trivial ones, at −2, −4, −6 and every negative even number. The interesting ones live in a vertical strip where the real part goes from 0 to 1, the critical strip, and there are infinitely many in there: the first one at a height of 14.13; the next ones at 21.02, 25.01, 30.42 and 32.94.

The hypothesis

All the non-trivial zeros of the zeta function have a real part of exactly ½: they all lie on a single vertical line, the critical line.

The critical strip and the first zeros, all on the line with real part one half
The critical line, with the first zeros.

Riemann saw it in a few zeros he worked out by hand and wrote that it was "very likely". He added that he had set aside the search for a proof "after a few fleeting attempts". 167 years later, we're still in the same place.

The music of the primeslink

So what do those zeros have to do with primes? Riemann found an exact formula: the prime staircase can be built from a smooth curve plus one wave for each zero. (Strictly speaking, the formula is for a weighted version of the staircase, but that's the idea.) It's like digital audio, where any sound breaks down into simple waves: the primes are the song, and the zeros are its notes. The height of each zero sets the frequency of its wave. With ten waves, the staircase starts to show; with a hundred, the steps appear; with a thousand, you can almost see every prime.

The prime staircase rebuilt by adding waves, one for each zero of zeta
The more zeros, the sharper the staircase.

And the real part of the zero tells you how fast its wave grows: like N to the power of that real part. If they all have real part ½, every wave grows like √N and the error stays small. But if there were a single zero with real part 0.8, its wave would grow like N⁰·⁸, much faster, and swamp all the others: the primes would be wildly disordered. The hypothesis says there's no note out of tune.

That's why it isn't just a curiosity about some weird function. In 1901, Helge von Koch proved that the hypothesis is equivalent to this statement about primes: the difference between π(N) and li(N) grows no faster than √N · ln N. If one is true, so is the other. Knowing where the zeros are means knowing how evenly the primes are spread.

The evidencelink

People have been computing zeros since the 1930s, and every single one lands right on the line. In 2004, Xavier Gourdon checked the first ten trillion (10¹³). Andrew Odlyzko computed billions of zeros much higher up, near zero number 10²³. Not one has strayed off the line.

But in math that's not enough. For every number anyone has computed, li(N) is always bigger than π(N); it looked like a rule. In 1914, Littlewood proved that they cross infinitely many times, and the first crossing is believed to be near 10³¹⁶, a number with more than three hundred digits. It's what Dijkstra said: testing can show the presence of bugs, never their absence. Ten trillion passing test cases are not a proof.

What has been proved comes in pieces. In 1914, Hardy showed there are infinitely many zeros on the line. Selberg proved a positive fraction of them are; Levinson pushed it to a third, Conrey to two fifths, and today we know that more than 41% of the zeros are on it. But the hypothesis demands 100%, and nobody knows how to close that gap.

Proportion of zeros proved to be on the line: from Hardy to more than 41%, against the 100% of the hypothesis
More than 41% proved; the rest is missing.

And the strangest part: in 1972, Hugh Montgomery was studying how far apart the zeros are from each other. At afternoon tea in Princeton, he mentioned it to the physicist Freeman Dyson, who recognized the formula right away: it was the same one used for the energy levels of heavy atomic nuclei. Odlyzko later confirmed it with millions of zeros. Many believe there's a physical system behind them, though nobody has found it.

Why does it matter?link

There are hundreds of theorems that begin like this: assuming the Riemann hypothesis… It's like code that depends on a library nobody has released yet: it compiles and the tests pass, but if that library turned out to be false, all of it would come crashing down. If someone proves it, hundreds of results become true at once.

  • Primality testing: your computer almost always uses Miller-Rabin, which is probabilistic (it can be wrong, with a tiny probability). In 1976, Gary Miller proved that, if a generalized version of the hypothesis holds, the test can be made deterministic and fast. It took until 2002 for Agrawal, Kayal and Saxena, with the AKS test, to pull it off without assuming anything.
  • Generating keys: for an RSA key, the program picks a huge random number and walks upward until it finds a prime. Thanks to Gauss, we know that on average it takes a few hundred tries. With the hypothesis, we know something stronger: the next prime is never farther than roughly √N · ln N. A worst-case guarantee, the kind we programmers love.
A myth: "proving it would break RSA"

Nothing suggests that. RSA is secure because factoring big numbers is hard, and the hypothesis says nothing about how to factor anything.

What it's really about

How primes are spread out overall, not where each one is. It would be huge news for mathematics, with no reason to think your keys would stop being secure.

The prize and the attemptslink

In 1900, Hilbert put it on his list of the 23 great problems of the century (it's number eight). He's credited with this line: if he woke up after sleeping for a thousand years, his first question would be whether the Riemann hypothesis has been proved. In 2000, the Clay Institute picked it as one of the seven Millennium Prize Problems, with a million dollars for each. Of the seven, only the Poincaré conjecture has been solved, and Perelman turned down the money.

There's been no shortage of attempts. John Nash worked on it; Louis de Branges has announced proofs several times; and in 2018 Michael Atiyah, one of the great mathematicians of the twentieth century, presented a proof in Heidelberg that nobody accepted. To claim the prize, publishing isn't enough: it has to appear in a qualifying journal and survive two years of scrutiny from the community.

The funny thing is that disproving it would be very easy to explain: you'd just need to find one zero off the line, a single failing test case. That's why computers keep searching higher and higher. If it exists, it's very, very far away. And if it doesn't, we'll need a new idea, because more than a century of tools hasn't been enough.

Try it yourselflink

If you want to get your hands on it, install mpmath and ask it for zeta and its zeros. Ask for zero number one thousand and you'll see its real part is still ½:

from mpmath import zeta, zetazero

print(zeta(2))          # 1.64493406684823  (= pi² / 6)
print(zeta(-1))         # -0.0833333333333333  (= -1/12)
print(zetazero(1))      # (0.5 + 14.1347251417347j)
print(zetazero(1000))   # (0.5 + 1419.422480946j)

With the sieve and li from above, you can compare the real primes with the estimate yourself. It's the fastest way for the hypothesis to stop being a formula and become something you can see.

In shortlink

Primes look chaotic, but how many there are up to any number can be estimated with amazing precision. The Riemann hypothesis says the error of that estimate is as small as it can be, and that's equivalent to all the interesting zeros of the zeta function lying on a single line. Ten trillion zeros back it up, but nobody has proved it.

In one sentence

Primes are no messier than a coin toss… if someone manages to prove it.

Do you think it's true, or will a zero off the line show up someday?

Sourceslink

  • "The Riemann Hypothesis", among the Millennium Prize Problems of the Clay Mathematics Institute, with the official statement by Enrico Bombieri.
  • Bernhard Riemann, "Über die Anzahl der Primzahlen unter einer gegebenen Grösse" (1859).
  • H. von Koch (1901), on the equivalence with the error in π(N); J. E. Littlewood (1914), on the crossings of π(N) and li(N).
  • G. H. Hardy (1914), A. Selberg (1942), N. Levinson (1974), J. B. Conrey (1989) and Pratt, Robles, Zaharescu and Zeindler (2020), on the proportion of zeros on the line.
  • X. Gourdon (2004) and A. Odlyzko, on computing zeros; H. Montgomery (1972), on their spacing.
  • G. Miller (1976) and M. Agrawal, N. Kayal and N. Saxena, "PRIMES is in P" (2002).
  • The video, on my channels: English and Spanish.

Continue reading

Last posts -->

Have you seen these projects?

Gymnasia

Gymnasia Gymnasia
Expo
React Native
TypeScript
OpenAI
Anthropic

Fitness app with two agents that run entirely on the device, with no backend, so the user's data never leaves the phone. A BYOK conversational coach with local tools and a remote system prompt with offline fallback, plus a food estimator that pulls macronutrients out of a photo of the plate, with barcode scanning against OpenFoodFacts.

LangGraph Deep Researcher

LangGraph Deep Researcher LangGraph Deep Researcher
Python
LangGraph
FastAPI
React
TypeScript
Docker

Multi-agent research system built with LangGraph. A supervisor breaks your question down into topics and launches search sub-agents in parallel; each one compresses its findings before handing them to a writer agent that produces the final sourced markdown report. Live streaming over WebSockets, a configurable model per role and bring-your-own API keys that are never persisted server-side.

Tau

Tau Tau
Python
LangChain

Multi-agent tutoring system for secondary school students, with one agent per subject and course material written and validated by a team of teachers. It was used with real students at a private school in Spain and at a secondary school in Colombia.

View all projects -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to watch any talk?

Last talks -->

Do you want to improve with these tips?

Last tips -->

Use this locally

Hugging Face spaces allow us to run models with very simple demos, but what if the demo breaks? Or if the user deletes it? That's why I've created docker containers with some interesting spaces, to be able to use them locally, whatever happens. In fact, if you click on any project view button, it may take you to a space that doesn't work.

Flow edit

Flow edit Flow edit

FLUX.1-RealismLora

FLUX.1-RealismLora FLUX.1-RealismLora
View all containers -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to train your model with these datasets?

short-jokes-dataset

HuggingFace

Dataset with jokes in English

Use: Fine-tuning text generation models for humor

231K rows 2 columns 45 MB
View on HuggingFace →

opus100

HuggingFace

Dataset with translations from English to Spanish

Use: Training English-Spanish translation models

1M rows 2 columns 210 MB
View on HuggingFace →

netflix_titles

HuggingFace

Dataset with Netflix movies and series

Use: Netflix catalog analysis and recommendation systems

8.8K rows 12 columns 3.5 MB
View on HuggingFace →
View more datasets -->