A prime test that squares and subtracts two
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 . 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 steps, each a squaring of a -bit number. The test is this. Set , and repeatedly replace by , keeping only the remainder on division by . Then is prime exactly when .
The opening figure runs it twice. For the numbers are 4, 14, 67, 42, 111 and then nought, and 127 is prime. For they are 4, 14, 194, 788 and on to 1,736 at the end — not nought, and . The verdict comes without trying a single divisor, and in the second case without even revealing what the factors are.
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 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 as prime by exhibiting an element whose powers run through all nonzero remainders before returning to 1. Such an element exists only when is prime, because only then are there remainders with inverses. The certificate needs the prime factors of : to show the order is exactly one must check that no proper divisor of it works, and that means knowing its divisors.
For the number is usually hard to factor. But is factored already, completely and trivially. Lucas’s insight was to find a group of size rather than , so that the factorisation needed for the certificate comes for free. The group of nonzero remainders is the wrong one; it has elements whatever happens. The right group lives in a larger number system.
Adjoin to the remainders modulo a square root of 3, and work with numbers , multiplied in the obvious way. If is prime and 3 is not a square modulo , this is a field with elements, exactly as adjoining to the real numbers gives the complex numbers. And 3 is never a square modulo a prime Mersenne number: 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 .
A circle of thirty-two points modulo 31
Every number has a norm, , the product of the number and its conjugate . 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 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 .
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 elements, raising to the power swaps a number with its conjugate — it fixes the ordinary remainders, by Fermat’s theorem, and it sends to , because 3 is not a square. So , the norm. For an element of norm 1, that says : every point of the circle has order dividing . And the circle’s points are exactly the solutions of in a field, of which there are at most , and a counting argument shows there are exactly that many. The circle modulo a prime with 3 a non-square has 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 has norm , 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: , , , , and , which is . One more squaring gives 1. An element whose sixteenth power is cannot have order dividing 16, so its order is 32.
The circle as a necklace
Because the group is cyclic, it can be drawn as a necklace: put at position round a ring of 32 beads. Multiplication becomes rotation, and squaring an element doubles its position. The squarings of then sit at positions 1, 2, 4, 8 and 16 — halfway round, which is — and position 32 is position 0, home.
Now the test’s numbers appear. Since has norm 1, its inverse is its conjugate, , and
At this is , the mysterious starting value. And squaring gives the recurrence directly: , so . 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 in it, which is why the test can be run without ever mentioning the larger field.
When is nought? Exactly when , that is, when : when the bead at position is a quarter of the way round, so that its mirror image is its negative. In the necklace that is position 8 for , and . In general, says that , so has order exactly .
Where squaring and subtracting two sends every remainder
The recurrence 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 ; 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, is the map that doubles an angle when , since — the cosine doubling map, whose orbits are chaotic on and exactly solvable. The Lucas–Lehmer recurrence is the same map with the angle replaced by a position on the finite necklace: is the analogue of . Nought corresponds to a right angle, 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 only if it began at a point whose doubling-order is exactly . The starting value 4 is the trace of , and the only question is whether 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 is never one of them.
Why 2,047 cannot pass
The converse is what makes the test a proof. Suppose modulo , and let be any prime factor of . The congruence holds modulo too, so modulo the element satisfies and has order exactly . But modulo , the norm-one elements form a group of elements if 3 is a square modulo , and if it is not — and an element’s order divides the size of its group. So , which means . The only prime factor of is itself.
The figure shows the argument failing for , which is why 2,047 fails. Modulo 23, 3 is a square (), so is an ordinary remainder, the norm-one group is a copy of the 22 nonzero remainders, and has order 11. Modulo 89, 3 is not a square, the group has 90 elements, and has order 90. An element of order 2,048 fits in neither. So 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 , and that forces primality.
Every prime exponent to 2,300
The exponent must be prime, since is divisible by , 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, , 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 , 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 , 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 Mersenne primes for each doubling of the exponent, where 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
The remainders are what make the test practical. Without them, is an ordinary whole number — 4, 14, 194, 37,634, 1,416,317,954 — and since is small, is very nearly . Its number of digits is , doubling with every step, and the figure confirms it up to , which has 586 digits. For the unreduced would have some digits, more digits than there are stars in the observable universe.
Reducing modulo keeps every number below , so each step is one squaring of a -bit number followed by a reduction — and the reduction modulo is nearly free, since 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 multiplications of -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 , where 32 points can be plotted and walked round by hand; for the circle has points and nothing about its shape can be drawn. What transfers is the structure — a cyclic group of size , 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 comes free. For other numbers of the form the same idea works with a different starting value, as Hans Riesel showed, and numbers have Proth’s test on the ordinary side, through ; but for a number with no special shape, neither nor 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 is a perfect number whenever 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 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 squarings, and it is not known whether a fundamentally faster certificate exists for numbers of this form — whether, for instance, primality of could be decided with a number of multiplications that grows more slowly than . 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 . 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 beads when is prime — Fermat’s theorem in a field of elements, where raising to the -th power is conjugation and the norm is the -th power. For a Mersenne number that size is a power of two, so the only way to have order is to keep doubling until , and the only way to get that far is for to be prime.
Shares its objects with
Essays that name at least two of the same things, and that neither author linked.
- A quarter of the bases at most — both name fermats little theorem, primality test
- Every element is a power of one of them — both name fermats little theorem, finite field
- Every power sum, from the coefficients alone — both name fermats little theorem, primality test
- Give or take twice the square root — both name finite field, quadratic residue
- How often two generates every remainder — both name fermats little theorem, quadratic residue
- No set with a line in every direction is small — both name finite field, quadratic residue
Named objects
A dashed tag is an object no other essay names yet.
Fermats little theoremFinite fieldMersenne primeMultiplicative orderPrimality testQuadratic residue