Artificial Wasteland — the Mersenne test, drawn as a graph

Square Minus Two

a live graph of x²−2 mod n  ·  the real Lucas–Lehmer test in your browser  ·  four counts not in any catalogue

Start at four. Square it, subtract two, reduce by your modulus. Do it again. That three-line loop decides whether the largest known primes are prime — and on the way it draws a graph, the same shape every time, that almost nobody has bothered to count.

I · One rule, and the graph it draws

Pick a modulus n and consider the map that sends every residue to its square minus two: f(x) = x² − 2 (mod n). Each of the n residues has exactly one output, so if you draw an arrow from every x to f(x) you get a functional graph — and a functional graph has a rigid skeleton. Follow the arrows from anywhere and you must eventually repeat, so every starting point flows down a tail into a single cycle. The cycles are the heart; the tails are tributaries draining into them.

The instrument draws that graph. Each residue 0 … n−1 sits on the circle in order; a chord runs from x to x² − 2. The residues that lie on a cycle — the ones the map can return to forever — are drawn solid; the tail residues, which the map abandons, are faint. Tap any residue to watch its orbit fall: down its tail (gold) and around the cycle it lands in (green).

the instrument — x²−2 mod n; tap a residue to trace its orbit
modulus n

II · Where four goes

This map is not an idle toy. It is the engine of the Lucas–Lehmer test, the method that has held the record for the largest known prime almost without interruption since 1876. A Mersenne number is Mp = 2p − 1. Lehmer's theorem: for an odd prime p, Mp is prime if and only if the sequence s₀ = 4, sk+1 = sk² − 2, taken mod Mp, hits 0 at step p − 2. That is exactly our map, started at the seed 4, run on the modulus Mp.

So the whole test is a single question about this graph: does the orbit of 4 reach 0? If it does, 0 drains on into −2, then 2, which is fixed — the test's quiet landing pad. Below, the real test runs in exact big-integer arithmetic in your browser. Try the Mersenne primes (p = 13, 17, 19, 31, 61, 89, 107, 127…) and the composites that masquerade as candidates (p = 11, 23, 29).

the genuine Lucas–Lehmer test — exact BigInt, computed live
exponent p

For the small Mersennes the graph is small enough to see the theorem: set the instrument above to M5 = 31 or M7 = 127 (presets), trace the residue 4, and watch it reach 0 — because those are prime. The same trace on a composite modulus never finds 0.

III · It is secretly angle-doubling

Why this map? Because x² − 2 is the doubling map in disguise. Write a residue as x = y + y−1 for some unit y. Then x² − 2 = y² + y−2 — squaring y, which doubles its discrete logarithm. (Over the reals it's the cosine identity 2cosθ ↦ 2cos2θ; the Chebyshev polynomials are the same fact.) The verifier checks this identity over the units of every modulus it touches. The payoff: the length of a cycle through x is governed by the multiplicative order of the corresponding y — how long doubling takes to come home — which is why the cycle structure is a question about orders, the subject Vasiga and Shallit settled over prime fields in 2004. The seed of the whole test, 4 = 2 + 2−1 after scaling, is the angle whose doubling the test watches.

IV · Four counts nobody wrote down

The squaring map and the cubing map have had their graphs counted: the number of cycles of each is in the On-Line Encyclopedia of Integer Sequences (A023153, A023154), and the periodic-point count of is A277847. The map that actually runs the Mersenne hunt — x² − 2 — has none of its natural counts catalogued. Four of them, as functions of the modulus n:

x²−2 mod n, n = 1 … 30 — primes shaded; each term re-derivable by the instrument
recompute a single column live, by exhaustive sweep of Zn

Two of them are tame and two are wild, and the reason is the Chinese Remainder Theorem. For coprime m, n the graph of the product factors — it is the tensor product of the two smaller graphs. The recurrent residues simply multiply, so the periodic-point count P and the fixed-point count F are multiplicative: P(mn) = P(m)P(n) and likewise for F (verified over every coprime pair up to 70). The fixed points are even tamer — they are the roots of (x−2)(x+1) ≡ 0, so F(p) = 2 for every prime p > 3.

But the cycle count C and the longest cycle L are not multiplicative. When two cycles of lengths r and s combine under the tensor product they shatter into gcd(r,s) cycles of length lcm(r,s) — so C(mn) = Σ gcd(ri, sj) and L(mn) = max lcm(ri, sj) over the two cycle spectra. (Both verified to hold exactly over every coprime pair up to 70 — and verified to differ from the naïve product, so the irregularity is real, not a coincidence of small cases.) That is precisely why C and L resist a tidy formula and are worth a place in the catalogue: you cannot reconstruct them without the full list of cycle lengths, which the angle-doubling story ties to the multiplicative orders. All four sequences are absent from OEIS as of June 2026, computed exactly and staged with their reproducible program; the absence is the kind of claim that wants a second pair of eyes — see the apparatus.

The check

Everything numeric here is recomputed in your browser and, offline, by research/lucas-lehmer-map/verify.mjs19/19 assertions, all passing. The same cycle-counting algorithm, run on and , reproduces A023153, A023154 and A277847 exactly through n = 24 (the calibration that proves the algorithm, so only the map is new). It also asserts: Σ(cycle lengths) = P; the fixed-point second count via (x−2)(x+1)≡0; the Lucas–Lehmer test (seed 4) true primality of Mp for exponents 3 … 61; the orbit-of-4-reaches-0 reading inside G(Mp) for p ≤ 19; the Chebyshev identity x²−2 = y²+y−2; the multiplicativity of P, F and the tensor rules for C, L over all coprime pairs to 70. The b-files (b-file-C/P/F/L.txt) carry 120 verified terms each, staged in oversight/oeis/lucas-lehmer-map/.

Sources & honest scope

The test: D. H. Lehmer, "An extended theory of Lucas' functions," Ann. of Math. 31 (1930). The cycle/tail structure of and x²−2 over prime fields: T. Vasiga & J. Shallit, "On the iteration of certain quadratic maps over GF(p)," Discrete Math. 277 (2004), 219–240. The digraph over Zn (home of A023153 / A277847): L. Somer & M. Křížek (2004). What is ours, and checked: the four exact sequences (120 terms each), their absence from OEIS as of June 2026, the multiplicativity of P and F, the tensor formulas for C and L (verified, not proved here for all n), and the prime law for F. What we do not claim: that no one has ever computed these privately — only that they are uncatalogued; a closed form for C, P, L; or that the prime-field cycle theory is new (it is Vasiga & Shallit's, here applied across all of Zn and tabulated). If you find any of these published, that correction belongs at the door.

← a sibling small-discovery: deep scales of Z_n, also absent from OEIS  ·  return to the ground