Explore the Mertens Function: Do Primes Follow a Drunkard’s Path?

Try adjusting the parameters yourself:

Why does the path of the primes look like a drunkard’s walk?

Look at the blue curve above: it goes up and down, first rising, then falling, like a drunkard staggering along the road.

But it is not drawn at random. Every step of the curve is determined by the prime factor structure of an integer. This curve is called the Mertens function.

Even more interestingly, mathematicians have found that this seemingly messy path is deeply connected to one of mathematics’ most famous unsolved problems: the Riemann hypothesis.

We do not need to start with complicated formulas. Give every integer a simple label and add those labels up as we go, and we can draw this curve.

Step 1: Give every number a label

Starting with 1, we give each positive integer n one of three labels: +1, -1, or 0. The rule is simple.

If n is divisible by the square of a prime, label it 0. For example, 4=2², 9=3², and 25=5²; 12 is divisible by 2²=4, so its label is also 0. In other words, if any prime appears more than once in a number’s prime factorization, its label is 0.

If the number has no square factor, count its distinct prime factors: an even number gets +1; an odd number gets -1.

For example: 2 has one prime factor, so it gets -1; 6 = 2 × 3 has two distinct prime factors, so it gets +1; 30 = 2 × 3 × 5 has three, so it gets -1. In particular, 1 has no prime factors, so we define its label to be +1.

Mathematicians call this label the Möbius function, written μ(n). It turns the prime factor structure of an integer into a simple number: +1, -1, or 0.

Step 2: Add them up, like keeping a running score

Now imagine someone standing at the starting point. Beginning with 1, they read each number’s label in order:

For +1, they move up one step; for -1, they move down one step; for 0, they stay where they are.

After reaching 10, their position after each number is, in order: 1, 0, -1, -1, -2, -1, -2, -2, -2, -1.

Their height after reaching x is the Mertens function M(x). In mathematical language, it is the sum of all Möbius function values from 1 to x: M(x) = ∑(n=1 到 x)μ(n). Here x is a positive integer; if x is not an integer, the upper limit means the greatest integer not exceeding x.

So the Mertens function is not another mysterious rule. It simply adds up those +1, -1, and 0 labels and records how high the path ends up.

Remember, this person is just a way to help us picture the process. Every step is already determined by the factors of the integer; the direction is not chosen randomly.

How far does a drunkard usually walk?

Now imagine someone else: they flip a coin at every step. Heads means one step up; tails means one step down. This is the classic mathematical random walk.

A random walk has an interesting property: if every step is an independent, fair coin flip, the standard deviation of the position after n steps is √n. So the typical distance from the starting point is on the order of √n, not n.

For example, after ten thousand coin flips, the final position typically fluctuates at a distance on the order of 100 steps from the start, rather than reaching 10000. √n is a typical scale, not a strict boundary; sometimes a random walk goes farther.

The dashed lines ±√x in the graph are a reference scale for comparison. They are not a wall the drunkard can never cross; they show roughly how much an ordinary random path tends to fluctuate.

One small detail: where the Möbius function is 0, the orange path does not flip a coin either. So by the time it reaches x, it has made fewer than x actual coin flips. The graph still uses ±√x as a simple reference scale to make it easier to compare the overall paths; it is not an exact statistical boundary for the orange path.

Which is wilder: the real coin-flipping drunkard or the Möbius path?

The orange curve on this page is a real coin-flipping drunkard. To make the comparison more direct, it flips a coin only where the Möbius function is nonzero: when μ(n)=0, it skips that position; where μ(n)=+1 or -1, it flips once to choose a direction.

This means both paths move at the same positions. There is just one difference: the direction of the blue path is determined by the prime factor structure of the integers, while the orange path’s direction is determined by random coin flips.

Try it a few times (click “Next” or change the random seed), and you may notice something interesting: the orange curve sometimes crosses the dashed lines, while the blue curve usually looks much calmer within the range shown.

Within the range this graph can display, the Mertens function has not crossed ±√x, and it is often much closer to the starting point than the random path. This is an observation about the range shown, not a conclusion that holds for every x.

This does not mean we have proved that the blue path will never cross the dashed lines. It only shows that, in the range observed so far, the plus and minus signs determined by prime factor structure follow a pattern different from genuinely random coin flips.

Integers are not independent of one another. They share prime factors and are constrained by the rules of factorization. These invisible connections are what make this deterministic path look like a random walk, yet not quite like one.

What does this have to do with the Riemann hypothesis?

At this point, a natural question comes up: why should a path made by adding +1, -1, and 0 have anything to do with the Riemann hypothesis?

The Möbius function is not used to study the Riemann hypothesis by coincidence. Its values appear directly in the expression for the reciprocal of the ζ function: 1/ζ(s) = ∑(n=1 到无穷)μ(n)/nˢ(当 s 的实部大于 1 时). The Riemann hypothesis concerns the locations of the nontrivial zeros of the ζ function.

Mathematicians have also proved a more direct statement: The Riemann hypothesis is true if and only if, for every ε>0, M(x)=O(x^(1/2+ε)).

This may sound mathematical. Intuitively, it means the Mertens function can exceed √x and can fluctuate somewhat more than √x. But however small we choose ε, its growth cannot exceed the scale x^(1/2+ε) (where O means we ignore a fixed constant factor).

This may sound mathematical. Intuitively, it means the Mertens function can exceed √x and can fluctuate somewhat more than √x. But however small an extra allowance we permit, it cannot ultimately grow faster than x^(1/2+ε).

Notice that this is not the same as “never exceeding √x.” The Riemann hypothesis allows the curve to cross ±√x; it only requires that its growth not be much faster than √x.

We can connect the Riemann hypothesis to this path this way: if the hypothesis is true, the growth of the Mertens function is tightly constrained. But this is a mathematical statement about growth arbitrarily far out, not something we can confirm from a graph covering a finite range.

The fact that the graph has not crossed the dashed line by one million is an interesting observation, not a proof of the Riemann hypothesis. The Riemann hypothesis remains unproved.

A famous story of a conjecture going wrong

In 1897, mathematician Franz Mertens published a table of values of μ(n) and M(n) from 1 to 10000. The calculations showed that, throughout the range he checked, the Mertens function always satisfied |M(x)| < √x.

Based on these values, Mertens thought the inequality was “very likely” to hold for every x>1. This claim later became known as the Mertens conjecture. A similar conjecture had appeared earlier in correspondence by mathematician Stieltjes; Mertens’ calculations and discussion in 1897 helped make it widely known.

The conjecture was striking because mathematicians already knew that if the Mertens conjecture were true, then the Riemann hypothesis would also have to be true. It is a stronger requirement: it not only limits the growth rate, but also requires the curve to stay within the √x scale at all times.

However, in 1985, mathematicians Andrew Odlyzko and Herman te Riele proved that the Mertens conjecture is false. Using an indirect mathematical method, they proved that the Mertens function must eventually exceed the ±√x range, but did not directly give the first specific counterexample.

Later, mathematician János Pintz proved that at least one counterexample is smaller than exp(3.21×10⁶⁴), an unimaginably large number. The location of the smallest counterexample is still unknown.

This failure does not mean the Riemann hypothesis is false too. The Riemann hypothesis only requires that M(x) not grow much faster than √x; it does not require M(x) to stay below √x forever. The disproof of the Mertens conjecture and the truth of the Riemann hypothesis are separate questions.

This story also reminds us: a pattern that seems to hold throughout a finite range may fail unimaginably far away. In mathematics, “it has always looked that way” is a long way from “it has been proved to hold forever.”

Try it yourself: adjust the graph

Now you can change the conditions of this walking experiment yourself. Click “Next” below the graph to switch views, or change the parameters below and click “Try it”:

Steps: The total number of steps (100 to 1000000). More steps show a longer path.

Random seed: Determines how the orange curve flips coins. Changing the number gives a different set of random outcomes.

Number of comparison paths: How many coin-flipping paths to draw at once (0 to 10). Set it to 0 to see only the blue Mertens function.

Bounds: Show or hide the ±√x reference lines.

You can drag left and right with the mouse and zoom with the scroll wheel. Zoom in far enough and you will see that the Mertens function is actually a staircase: whenever an integer has a label of +1 or -1, the curve rises or falls by one step; for 0, it stays in place.

Try a few random seeds and watch how the orange path changes. Then compare it with the blue path and see how these movements, which look similar but come from completely different sources, differ.

Keep exploring the world of mathematics

The Mertens function shows us that when we add up a sequence determined by prime factor structure, it can form a curve that looks like a random walk. It has a random appearance, yet hides deterministic mathematical rules.

If you are interested in the connection between primes and the Riemann hypothesis, explore topics related to prime distribution and the ζ function. If you enjoy complex patterns generated by simple rules, try the Julia set, the Mandelbrot set, and the Newton fractal.

You can also explore more fascinating mathematical images or read the Wikipedia article about the Mertens function.

Further Reading

  • Ulam Prime Spiral From a geometric perspective: why do prime numbers form diagonal lines?
  • Prime Number Theorem From a statistical perspective: how does the number of primes grow?
  • Euler Product From an algebraic perspective: why can prime numbers construct the sum of reciprocal powers of all integers?
  • A Color Map of the ζ Function From a function-plot perspective: what colors and structures does the ζ function reveal in the complex plane?
  • Riemann Hypothesis From a zero-distribution perspective: why do all nontrivial zeros of the ζ function lie on the critical line?
  • Weaving Zeros From a complex-analysis perspective: how do the zeros of the ζ function shape the distribution of prime numbers?
  • Mertens Function From a probability-and-statistics perspective: why does the cumulative sum of the Möbius function resemble a random walk?
  • Robin's Criterion From a divisor-sum perspective: how can a single inequality decide the Riemann Hypothesis?
  • Zeros and Atomic Nucleus Energy Levels From a random-matrix perspective: why do the world of primes and the quantum world look so strikingly similar?

Want to see these stunning mathematical visuals every time you open a new tab? Install the Chrome extension now and set "Science Wonder Box" as your new tab page.🚀

Learn More
↑