Number

A prime test that squares and subtracts two

Start at 4, square, subtract 2, and keep only the remainder on division by 2^p − 1. After p − 2 steps the remainder is nought exactly when 2^p − 1 is prime. The rule looks like numerology, and it is Fermat's little theorem moved onto a circle: the points of x² − 3y² = 1 modulo a Mersenne prime number one more than the prime, a power of two, and the test checks that 2 + √3 walks all the way round.

Worth reading first: An order that proves a prime · One residue whose powers are all of them.

Every record prime of the last seventy years but a handful has had the form 2p−12^p - 1. The reason is not that such numbers are especially likely to be prime. It is that for them, and essentially only for them, there is a test that settles primality outright in about pp steps, each a squaring of a pp-bit number. The test is this. Set s0=4s_0 = 4, and repeatedly replace ss by s2−2s^2 - 2, keeping only the remainder on division by M=2p−1M = 2^p - 1. Then MM is prime exactly when sp−2=0s_{p-2} = 0.

The opening figure runs it twice. For p=7p = 7 the numbers are 4, 14, 67, 42, 111 and then nought, and 127 is prime. For p=11p = 11 they are 4, 14, 194, 788 and on to 1,736 at the end — not nought, and 2,047=23×892{,}047 = 23 \times 89. The verdict comes without trying a single divisor, and in the second case without even revealing what the factors are.

Square, subtract two, and look for nought. p=7: 4,14,67,42,111,0; p=11: 4,14,194,788,701,119,1877,240,282,1736
Fig. 1 Start at 4, square, subtract 2 and keep the remainder: for 27−1=1272^7 - 1 = 127 the last number is 0 and 127 is prime; for 211−1=2,0472^{11} - 1 = 2{,}047 it is 1,736 and 2,047 is composite.

The rule seems to come from nowhere. Nothing in “square and subtract two” mentions primes, and the starting value 4 looks arbitrary. Édouard Lucas found the idea in 1876, using it to prove that 2127−12^{127} - 1 is prime, a record that stood for seventy-five years, and Derrick Lehmer gave the test its final form in 1930. What follows explains why it works. The explanation is Fermat’s little theorem, not on the remainders themselves but on a circle drawn among them, where the circle has one more point than the prime — and for a Mersenne number, one more than the prime is a power of two.

The test as Lucas left it

The earlier test in this sequence, an order that proves a prime, certified nn as prime by exhibiting an element whose powers run through all n−1n - 1 nonzero remainders before returning to 1. Such an element exists only when nn is prime, because only then are there n−1n - 1 remainders with inverses. The certificate needs the prime factors of n−1n - 1: to show the order is exactly n−1n - 1 one must check that no proper divisor of it works, and that means knowing its divisors.

For M=2p−1M = 2^p - 1 the number M−1=2p−2=2(2p−1−1)M - 1 = 2^p - 2 = 2(2^{p-1} - 1) is usually hard to factor. But M+1=2pM + 1 = 2^p is factored already, completely and trivially. Lucas’s insight was to find a group of size M+1M + 1 rather than M−1M - 1, so that the factorisation needed for the certificate comes for free. The group of nonzero remainders is the wrong one; it has M−1M - 1 elements whatever happens. The right group lives in a larger number system.

Adjoin to the remainders modulo MM a square root of 3, and work with numbers a+b3a + b\sqrt 3, multiplied in the obvious way. If MM is prime and 3 is not a square modulo MM, this is a field with M2M^2 elements, exactly as adjoining ii to the real numbers gives the complex numbers. And 3 is never a square modulo a prime Mersenne number: MM leaves remainder 3 on division by 4 and remainder 1 on division by 3, and by quadratic reciprocity those two facts force 3 to be a non-square. The element at the centre of the test is ω=2+3\omega = 2 + \sqrt 3.

A circle of thirty-two points modulo 31

A circle of thirty-two points modulo 31. x^2 - 3y^2 = 1 mod 31: 32 points; squarings of 2+sqrt3: (2,1) (7,4) (4,25) (0,14) (30,0)
Fig. 2 The pairs of remainders modulo 31 with x2−3y2≡1x^2 - 3y^2 \equiv 1: thirty-two of them, one more than 31. Multiplying by ω=2+3\omega = 2 + \sqrt 3 walks round all 32; the orange path follows the squarings ω,ω2,ω4,ω8\omega, \omega^2, \omega^4, \omega^8 to ω16=−1\omega^{16} = -1.

Every number a+b3a + b\sqrt 3 has a norm, a2−3b2a^2 - 3b^2, the product of the number and its conjugate a−b3a - b\sqrt 3. Norms multiply, so the numbers of norm 1 form a group under multiplication, the analogue of the unit circle among complex numbers. Over the real numbers x2−3y2=1x^2 - 3y^2 = 1 is a hyperbola; over the remainders modulo a prime it is a finite set of points, and the figure draws it for the prime 31, which is 25−12^5 - 1.

There are 32 points, one more than 31. The count is the heart of the matter, and it comes from Fermat’s theorem in the big field. In the field of M2M^2 elements, raising to the power MM swaps a number with its conjugate — it fixes the ordinary remainders, by Fermat’s theorem, and it sends 3\sqrt 3 to −3-\sqrt 3, because 3 is not a square. So xM+1=x⋅xM=xxˉx^{M+1} = x \cdot x^M = x \bar x, the norm. For an element of norm 1, that says xM+1=1x^{M+1} = 1: every point of the circle has order dividing M+1M + 1. And the circle’s points are exactly the solutions of xM+1=1x^{M+1} = 1 in a field, of which there are at most M+1M + 1, and a counting argument shows there are exactly that many. The circle modulo a prime MM with 3 a non-square has M+1M + 1 points, and they form a cyclic group, as the nonzero remainders do — the theorem of one residue whose powers are all of them, transplanted.

The number ω=2+3\omega = 2 + \sqrt 3 has norm 4−3=14 - 3 = 1, so it lies on the circle. Modulo 31, its powers visit all 32 points before returning to 1, so it generates the group. The figure follows its successive squares: ω=(2,1)\omega = (2, 1), ω2=(7,4)\omega^2 = (7, 4), ω4=(4,25)\omega^4 = (4, 25), ω8=(0,14)\omega^8 = (0, 14), and ω16=(30,0)\omega^{16} = (30, 0), which is −1-1. One more squaring gives 1. An element whose sixteenth power is −1-1 cannot have order dividing 16, so its order is 32.

The circle as a necklace

The circle as a necklace of thirty-two beads. traces 2a of omega^k mod 31: 2,4,14,21,8,11,5,9,0,22,26,20,23,10,17,27,29,27,17,10,23,20,26,22,0,9,5,11,8,21,14,4
Fig. 3 The 32 points laid out as a necklace in the order ω0,ω1,ω2,…\omega^0, \omega^1, \omega^2, \ldots. Squaring doubles the position, so the squarings of ω\omega jump 1, 2, 4, 8, 16; sks_k is a bead plus its mirror image, and it is nought at the bead a quarter of the way round.

Because the group is cyclic, it can be drawn as a necklace: put ωk\omega^k at position kk round a ring of 32 beads. Multiplication becomes rotation, and squaring an element doubles its position. The squarings of ω\omega then sit at positions 1, 2, 4, 8 and 16 — halfway round, which is −1-1 — and position 32 is position 0, home.

Now the test’s numbers appear. Since ω\omega has norm 1, its inverse is its conjugate, 2−32 - \sqrt 3, and

sk=ω2k+ω−2k.s_k = \omega^{2^k} + \omega^{-2^k}.

At k=0k = 0 this is (2+3)+(2−3)=4(2 + \sqrt 3) + (2 - \sqrt 3) = 4, the mysterious starting value. And squaring gives the recurrence directly: (ω2k+ω−2k)2=ω2k+1+2+ω−2k+1(\omega^{2^k} + \omega^{-2^k})^2 = \omega^{2^{k+1}} + 2 + \omega^{-2^{k+1}}, so sk+1=sk2−2s_{k+1} = s_k^2 - 2. The test is not computing anything new. It is tracking the sum of a bead and its mirror image while the bead’s position doubles — and that sum is an ordinary remainder, with no 3\sqrt 3 in it, which is why the test can be run without ever mentioning the larger field.

When is sks_k nought? Exactly when ω2k=−ω−2k\omega^{2^k} = -\omega^{-2^k}, that is, when ω2k+1=−1\omega^{2^{k+1}} = -1: when the bead at position 2k2^k is a quarter of the way round, so that its mirror image is its negative. In the necklace that is position 8 for M=31M = 31, and s3=0s_3 = 0. In general, sp−2=0s_{p-2} = 0 says that ω2p−1=−1\omega^{2^{p-1}} = -1, so ω\omega has order exactly 2p=M+12^p = M + 1.

Where squaring and subtracting two sends every remainder

Where x² − 2 sends every remainder modulo 31. orbit of 4: 4,14,8,0,29,2,2; 17 residues reach 2; cycles 1,3,6,7,12,13,15,16,18,19,24,25,28,30
Fig. 4 Every remainder modulo 31 with an arrow to its image under x↦x2−2x \mapsto x^2 - 2, arranged by the number of steps to reach 2, which maps to itself. From the start, 4 runs 4, 14, 8, 0, then 29, which is −2-2 modulo 31, and 2.

The recurrence s↦s2−2s \mapsto s^2 - 2 is a map from the remainders to themselves, and like any such map it can be drawn as a graph, each remainder pointing to its image. The figure draws it modulo 31. Seventeen remainders flow, in at most five steps, into 2, which is a fixed point since 22−2=22^2 - 2 = 2; the other fourteen lie on cycles and never get there. The test’s starting value 4 runs 4, 14, 8, 0, 29, 2: reaching nought at step 3, exactly when it should.

The map is not arbitrary. Over the real numbers, x2−2x^2 - 2 is the map that doubles an angle when x=2cos⁡θx = 2\cos\theta, since (2cos⁡θ)2−2=2cos⁡2θ(2\cos\theta)^2 - 2 = 2\cos 2\theta — the cosine doubling map, whose orbits are chaotic on [−2,2][-2, 2] and exactly solvable. The Lucas–Lehmer recurrence is the same map with the angle replaced by a position on the finite necklace: sk=ω2k+ω−2ks_k = \omega^{2^k} + \omega^{-2^k} is the analogue of 2cos⁡(2kθ)2\cos(2^k\theta). Nought corresponds to a right angle, −2-2 to a half-turn and 2 to a full turn, which is the path 0, 29, 2 in the figure.

Read this way, the graph shows why the test is a certificate rather than a statistical hint. A remainder reaches nought at step p−2p - 2 only if it began at a point whose doubling-order is exactly 2p2^p. The starting value 4 is the trace of ω\omega, and the only question is whether ω\omega has the full order. The graph’s other branches — remainders that never reach nought, or reach it too soon — are traces of elements of smaller order, and modulo a prime Mersenne number ω\omega is never one of them.

Why 2,047 cannot pass

Why 2,047 cannot pass. mod 23: group 22, ord 11; mod 89: group 90, ord 90; s9=1736
Fig. 5 Modulo 23, where 3 is a square, the elements of norm one form a group of only 22, and ω\omega has order 11; modulo 89 the group has 90 elements and ω\omega has order 90. Neither has room for an element of order 2,048.

The converse is what makes the test a proof. Suppose sp−2≡0s_{p-2} \equiv 0 modulo MM, and let rr be any prime factor of MM. The congruence holds modulo rr too, so modulo rr the element ω\omega satisfies ω2p−1=−1\omega^{2^{p-1}} = -1 and has order exactly 2p2^p. But modulo rr, the norm-one elements form a group of r−1r - 1 elements if 3 is a square modulo rr, and r+1r + 1 if it is not — and an element’s order divides the size of its group. So 2p≤r+12^p \le r + 1, which means r≥2p−1=Mr \ge 2^p - 1 = M. The only prime factor of MM is MM itself.

The figure shows the argument failing for 2,047=23×892{,}047 = 23 \times 89, which is why 2,047 fails. Modulo 23, 3 is a square (72=49=2×23+37^2 = 49 = 2 \times 23 + 3), so 3\sqrt 3 is an ordinary remainder, the norm-one group is a copy of the 22 nonzero remainders, and ω\omega has order 11. Modulo 89, 3 is not a square, the group has 90 elements, and ω\omega has order 90. An element of order 2,048 fits in neither. So s9s_9 cannot be nought modulo 2,047, and it is not: it is 1,736.

The factors themselves never had to be found. The test learns that a group is too small without learning which group, much as Fermat’s test can declare a number composite without factoring it. Unlike Fermat’s test, though, it can never be fooled: there are no Carmichael numbers for Lucas–Lehmer, because passing forces an element of order M+1M + 1, and that forces primality.

Every prime exponent to 2,300

Every prime exponent to 2,300, tested. 341 exponents; zero at 3,5,7,13,17,19,31,61,89,107,127,521,607,1279,2203,2281
Fig. 6 All 341 odd prime exponents up to 2,300, with the test run on 2p−12^p - 1 for each: it reaches nought for exactly sixteen of them, from 3 to 2,281, the Mersenne primes of this range, and nowhere else.

The exponent must be prime, since 2ab−12^{ab} - 1 is divisible by 2a−12^a - 1, and the figure runs the test on every odd prime exponent up to 2,300 — 341 of them — to the end. Sixteen pass: 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1,279, 2,203 and 2,281. They are exactly the known Mersenne primes in this range. The largest, 22281−12^{2281} - 1, has 687 digits, and its test is 2,279 squarings of a 2,281-bit number: well under a second here.

The last five were found together. In 1952 Raphael Robinson ran the test on SWAC, one of the first stored-program computers, and found 521 and 607 on the first evening and 1,279, 2,203 and 2,281 within months. Lucas’s 2127−12^{127} - 1, with 39 digits, had been the largest known prime from 1876 until 1951, when hand calculation and then the EDSAC computer in Cambridge pushed past it with numbers of other forms. Since 1952 the largest known prime has almost always been a Mersenne prime, and the Great Internet Mersenne Prime Search has run this same recurrence on exponents in the tens of millions. The largest known prime, found in 2024, is 2136,279,841−12^{136{,}279{,}841} - 1, with over 41 million digits.

The figure shows the distribution too. The prime exponents thin out logarithmically, and the Mersenne primes among them thin out further, at a rate a heuristic of Lenstra, Pomerance and Wagstaff predicts: about eγ≈1.78e^\gamma \approx 1.78 Mersenne primes for each doubling of the exponent, where γ\gamma is Euler’s constant. That predicts around twenty with exponent up to 2,300; sixteen is within the scatter such counts show, and the heuristic, if right, means there are infinitely many — though, as with prime repunits, nobody can prove it.

Without remainders the numbers explode

Without remainders the numbers explode. digits of s₀ to s₁₀: 1,2,3,5,10,19,37,74,147,293,586
Fig. 7 The numbers 4, 14, 194, 37,634, … without remainders: each is the square of the last minus 2, so the number of digits doubles at every step, to 586 by s10s_{10} — about 2k×0.5722^k \times 0.572 digits.

The remainders are what make the test practical. Without them, sk=ω2k+ω−2ks_k = \omega^{2^k} + \omega^{-2^k} is an ordinary whole number — 4, 14, 194, 37,634, 1,416,317,954 — and since ω−1=2−3\omega^{-1} = 2 - \sqrt 3 is small, sks_k is very nearly (2+3)2k(2 + \sqrt 3)^{2^k}. Its number of digits is 2klog⁡10(2+3)≈0.572×2k2^k \log_{10}(2 + \sqrt 3) \approx 0.572 \times 2^k, doubling with every step, and the figure confirms it up to s10s_{10}, which has 586 digits. For p=127p = 127 the unreduced s125s_{125} would have some 2×10372 \times 10^{37} digits, more digits than there are stars in the observable universe.

Reducing modulo MM keeps every number below 2p2^p, so each step is one squaring of a pp-bit number followed by a reduction — and the reduction modulo 2p−12^p - 1 is nearly free, since 2p≡12^p \equiv 1 means the high half of a number can simply be added to the low half. This is the practical reason the largest known primes are Mersenne primes: the test is about pp multiplications of pp-bit numbers, the same cost as a single Fermat test, but it proves rather than suggests.

What the circle does not show

Every circle drawn here is small. The figures show the group for M=31M = 31, where 32 points can be plotted and ω\omega walked round by hand; for 2127−12^{127} - 1 the circle has 21272^{127} points and nothing about its shape can be drawn. What transfers is the structure — a cyclic group of size M+1M + 1, a generator, positions that double — and the transfer is a theorem rather than something the pictures demonstrate.

The pictures also say nothing about why Mersenne primes are rare or common. The test decides each candidate; it gives no information about the next. Whether infinitely many Mersenne numbers are prime, whether infinitely many are composite with a prime exponent, and whether every Mersenne number with prime exponent is squarefree are all open. It is not even known that the exponents of Mersenne primes have density nought among the primes, though every heuristic says so.

And the method is narrower than it looks. It needs a group whose size is fully factored, which for 2p−12^p - 1 comes free. For other numbers of the form k⋅2n−1k \cdot 2^n - 1 the same idea works with a different starting value, as Hans Riesel showed, and numbers k⋅2n+1k \cdot 2^n + 1 have Proth’s test on the ordinary side, through n−1n - 1; but for a number with no special shape, neither n−1n - 1 nor n+1n + 1 is usually factorable, and certifying it requires the slower general methods — elliptic curves, or the polynomial identity of the row that proves a prime.

Still open: Mersenne primes forever

Euclid showed that 2p−1(2p−1)2^{p-1}(2^p - 1) is a perfect number whenever 2p−12^p - 1 is prime, and Euler that every even perfect number has this form; so the question whether there are infinitely many Mersenne primes is the question whether there are infinitely many even perfect numbers. It has been open since antiquity in the second form and since Mersenne’s list of 1644 in the first. The heuristic says yes, with about 1.781.78 new ones each time the exponent doubles; no proof is in sight.

A second question sits beside it. The Lucas–Lehmer test runs in about pp squarings, and it is not known whether a fundamentally faster certificate exists for numbers of this form — whether, for instance, primality of 2p−12^p - 1 could be decided with a number of multiplications that grows more slowly than pp. All the known proofs of primality, for special forms or general ones, rest on exhibiting an element of large order in some group, and the group must be large enough to force primality. Whether that is the only way is not known.

One more than a prime

The test squares and subtracts two because that is what the trace of an element does when the element is squared. It starts at 4 because 4 is the trace of 2+32 + \sqrt 3. It looks for nought because nought is the trace of an element a quarter of the way round a cyclic necklace. And it proves primality because the necklace has M+1M + 1 beads when MM is prime — Fermat’s theorem in a field of M2M^2 elements, where raising to the MM-th power is conjugation and the norm is the (M+1)(M + 1)-th power. For a Mersenne number that size is a power of two, so the only way to have order M+1M + 1 is to keep doubling until −1-1, and the only way to get that far is for MM to be prime.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

Fermats little theoremFinite fieldMersenne primeMultiplicative orderPrimality testQuadratic residue