Number

The row that proves a prime

Fermat's little theorem can be fooled: 561 passes it for every base and is not prime. Thread necklaces with a fixed number of black beads instead of any colours at all, and the count becomes a statement about a whole row of Pascal's triangle — every middle entry of row n is a multiple of n exactly when n is prime — which no composite can fake. Written as polynomials it is (x + a)ⁿ = xⁿ + a, and cut down to size it is the first proof that primes can be recognised in polynomial time.

Worth reading first: Necklaces that prove a theorem · The exponent that is smaller than Euler's.

Necklaces that prove a theorem counted the strings of pp beads in aa colours. The constant strings stand alone; every other string lies on a ring of exactly pp rotations, because pp is prime and no rotation short of a full turn can carry a mixed string onto itself. So pp divides ap−aa^p - a, which is Fermat’s little theorem.

The theorem is a test for primality with a famous flaw. The exponent that is smaller than Euler’s met the Carmichael numbers — 561561, 11051105, 17291729 and infinitely many more — which pass it for every base they share no factor with, although they are composite. The count that proved the theorem cannot tell them from primes, because it counts all the strings at once and the numbers happen to come out right.

This essay counts more carefully. Fix not the colours but how many beads are black, and the same necklace argument gives a statement that no composite number can fake.

The necklaces of 7 beads with 3 black. C(7, 3) = 35 arrangements in 5 rotation classes of sizes 7, 7, 7, 7, 7.
Fig. 1 The 35 ways to choose which 3 of 7 beads on a ring are black, grouped by rotation into 5 necklaces. Each stands for exactly 7 arrangements, because 7 is prime and no rotation short of a full turn carries a mixed arrangement onto itself — so 7 divides C(7, 3) = 35.

There are (73)=35\binom 73 = 35 ways to choose three black beads out of seven places on a ring. Group them by rotation and there are five necklaces, each standing for exactly seven arrangements. Seven divides thirty-five, and the reason is the same as in Fermat’s theorem: a prime number of places admits no partial symmetry.

One necklace that repeats too soon

For a composite number of beads, the argument can fail, and the failure is visible.

The necklaces of 6 beads with 2 black. C(6, 2) = 15 arrangements in 3 rotation classes of sizes 6, 6, 3.
Fig. 2 The 15 ways to choose 2 black beads out of 6 on a ring, grouped by rotation. Two necklaces stand for 6 arrangements each; the third, with its black beads opposite each other, repeats after 3 steps and stands for only 3. So C(6, 2) = 15 is not a multiple of 6.

With six beads and two black, there are fifteen arrangements. Two of the necklaces are full rings of six. The third — two black beads directly opposite each other — comes back to itself after three steps, so its ring has only three arrangements. Fifteen is six plus six plus three, and six does not divide fifteen.

That is the general picture. The size of every necklace’s ring divides the number of beads, and the ring is short exactly when the necklace has a rotational symmetry — when its pattern repeats with some period that divides nn. For a prime nn there is no such period except 11, which only the all-black and all-white necklaces have. For a composite nn, take kk to be its smallest prime factor qq and the necklace with qq black beads spaced evenly round the ring: it repeats after n/qn/q steps. That short ring is enough to spoil the divisibility, and a computation with the factor qq shows that (nq)\binom nq is never a multiple of nn.

A row that only a prime can fill

So the necklaces with a fixed number of black beads give a characterisation, not just a necessary condition.

Which rows of Pascal's triangle their own number divides. Rows 2 to 20: every middle entry is divisible by the row number exactly for the primes 2, 3, 5, 7, 11, 13, 17, 19.
Fig. 3 Rows 2 to 20 of Pascal’s triangle, one square for each entry C(n, k) with k from 1 to n − 1: filled when n divides it, open when it does not. The rows filled all the way across are exactly the prime rows.

A number nn greater than one is prime exactly when it divides every entry (nk)\binom nk with 0<k<n0 < k < n. The figure shows the rows of Pascal’s triangle from 22 to 2020 with each middle entry marked by whether the row’s number divides it. The prime rows are filled from end to end; every composite row has a gap. Row 66 has gaps at k=2k = 2, 33 and 44 — the opposite-beads necklace at 22 and 44, and at 33 the necklace of alternating colours, which repeats after two steps; row 99 at k=3k = 3 and k=6k = 6; row 1515 at the multiples of 33 and 55. The pattern of the gaps is Pascal’s triangle read modulo the row’s factors, where Lucas’s theorem computes each entry’s remainder from the digits of nn and kk in the base of a prime.

The Carmichael numbers cannot hide here. 561=3×11×17561 = 3 \times 11 \times 17 passes Fermat’s test for every base prime to it, but its row of Pascal’s triangle has gaps — at k=3k = 3, 1111 and 1717 among other places, where the evenly spaced necklaces repeat early. The test that counts all colours at once adds up the row in a way that makes the gaps cancel; the test that looks at each entry separately sees them.

Where the gaps fall

Which entries of a composite row fail has an exact answer, and it is written in the digits of the numbers involved.

Ernst Kummer proved in 1852 that the power of a prime qq dividing (nk)\binom nk is the number of carries that occur when kk and n−kn - k are added in base qq — the rule the carries decide the divisibility drew. So (nk)\binom nk is a multiple of nn exactly when, for every prime power qeq^e dividing nn, adding kk and n−kn - k in base qq produces at least ee carries. For a prime n=pn = p, adding kk and p−kp - k in base pp always carries once, since the last digits add up to pp; that is the necklace argument in the language of digits.

For 561=3×11×17561 = 3 \times 11 \times 17 the rule has to be satisfied in three bases at once, and it fails often: counting shows that 114114 of the 560560 middle entries of row 561561 are not multiples of 561561, starting at k=3,9,11,12,17,18k = 3, 9, 11, 12, 17, 18. Each is a place where some addition in base 33, 1111 or 1717 goes through without a carry — which is the digit form of a necklace repeating early. The Carmichael number has one hundred and fourteen necklace classes that betray it, and Fermat’s test consults none of them individually.

How the total hides the gaps

Fermat’s test is the sum of the row. Choosing each of the nn beads black or white, the 2n−22^n - 2 strings that are not constant are the necklaces with 1,2,…,n−11, 2, \dots, n - 1 black beads, so

2n−2=(n1)+(n2)+⋯+(nn−1).2^n - 2 = \binom n1 + \binom n2 + \cdots + \binom n{n-1}.

If every term is a multiple of nn, so is the sum, and that is the necklace proof of Fermat’s theorem for the base 22. A composite can make the sum a multiple of nn while leaving terms that are not. The number 341=11×31341 = 11 \times 31 does exactly that: 2341−22^{341} - 2 is a multiple of 341341, so 341341 passes Fermat’s test with the base 22 — it is the smallest number that does so without being prime — while 3838 of the 340340 middle entries of its row — beginning at k=11,22,31,33k = 11, 22, 31, 33 — are not multiples of 341341. Their remainders cancel in the total.

A test that sees only the sum cannot see the cancellation, and every pseudoprime is a row whose failures add up to nothing. The polynomial test refuses to add them up, and that is the whole of its advantage over Fermat’s.

The row as a polynomial

The row of Pascal’s triangle is the list of coefficients of (x+1)n(x + 1)^n, and the characterisation can be written as a single identity. Saying that nn divides every middle coefficient is saying that

(x+1)n≡xn+1(modn)(x + 1)^n \equiv x^n + 1 \pmod n

as polynomials, with every coefficient reduced modulo nn. More generally, for any aa sharing no factor with nn,

(x+a)n≡xn+a(modn)(x + a)^n \equiv x^n + a \pmod n

holds exactly when nn is prime. Setting x=0x = 0 gives an≡aa^n \equiv a, which is Fermat’s theorem; so the polynomial identity implies Fermat’s theorem and is strictly stronger, because a composite can make the constant term come out right while leaving other coefficients behind.

The coefficients of (x + 1)^91 that 91 does not divide. (x + 1)^91 modulo 91: 18 middle coefficients are non-zero (k = 7, 13, 14, 21, 26, 28, 35, 39, 42, 49, 52, 56); the Fermat test with base 3 passes.
Fig. 4 The 92 coefficients of (x+1)91(x + 1)^{91} reduced modulo 91, with a mark where a coefficient is not a multiple of 91. Although 390≡13^{90} \equiv 1 modulo 91, so that Fermat’s test with base 3 says 91 could be prime, eighteen middle coefficients survive — at the multiples of 7 and of 13 — and the polynomial test declares 91 composite.

The number 91=7×1391 = 7 \times 13 is a pseudoprime to the base 33: 390≡1(mod91)3^{90} \equiv 1 \pmod{91}, and Fermat’s test with that base is fooled. The polynomial (x+1)91(x + 1)^{91} reduced modulo 9191 is not fooled at all. Eighteen of its middle coefficients are not multiples of 9191, one at each multiple of 77 and each multiple of 1313 below 9191, and the identity fails in eighteen places at once. The polynomial form of Fermat’s theorem cannot be fooled, because it asks every necklace class separately rather than asking for their total.

The same fact as the freshman’s dream

The identity (x+y)p≡xp+yp(modp)(x + y)^p \equiv x^p + y^p \pmod p — the “freshman’s dream”, since it is the mistake beginners make with ordinary numbers — is the necklace count once more, and its reach goes far beyond primality.

It says that raising to the pp-th power is additive when arithmetic is done modulo a prime. That makes the map x↦xpx \mapsto x^p a symmetry of any number system built with arithmetic modulo pp — the Frobenius map — and the finite fields are organised around it. In the field with four elements squaring swaps the two elements that are not 00 or 11; in the plane of order 2h2^h the translation hyperovals of every power of x that draws a hyperoval are the graphs of powers of the Frobenius map, and they work because it is additive. A fact about necklaces with a prime number of beads is the reason a whole branch of algebra has a distinguished symmetry.

Too many coefficients, and how to cut them down

As a practical test, the polynomial identity is useless as it stands. It has n+1n + 1 coefficients, and for a number with a hundred digits nn is a hundred-digit number of coefficients — no better than trying every possible factor.

Manindra Agrawal, Neeraj Kayal and Nitin Saxena found in 2002 how to cut it down. Wrap the polynomial round: work modulo xr−1x^r - 1 as well as modulo nn, so that xrx^r is replaced by 11, xr+1x^{r+1} by xx, and so on. Every polynomial then has only rr coefficients, and (x+a)n(x + a)^n can be computed with about log⁡n\log n squarings of polynomials of that size. The identity still holds for every prime; the question is whether composites can now slip through, since wrapping loses information.

The wrapped polynomial test on three primes and three Carmichael numbers. 557: prime, r = 89, polynomial test passes; 561: composite, r = 89, polynomial test fails; 1103: prime, r = 109, polynomial test passes; 1105: composite, r = 131, polynomial test fails; 1723: prime, r = 137, polynomial test passes; 1729: composite, r = 127, polynomial test fails.
Fig. 5 Three primes and the three smallest Carmichael numbers. For each, the smallest rr for which nn has order greater than (log⁡2n)2(\log_2 n)^2 modulo rr, and whether (x+1)n(x + 1)^n equals xn+1x^n + 1 once powers of xx are wrapped modulo xr−1x^r - 1 and coefficients reduced modulo nn. The primes pass and the Carmichael numbers fail, with a = 1 alone.

Their theorem is that they cannot, provided rr is chosen so that nn has a large order modulo rr — larger than (log⁡2n)2(\log_2 n)^2 — and the identity is checked for enough values of aa, up to about rlog⁡n\sqrt r \log n. Then an nn that passes, is not a perfect power and has no factor below rr must be prime. Such an rr exists below about (log⁡n)5(\log n)^5, so the whole test takes a number of steps bounded by a power of the number of digits of nn. The figure runs the wrapped test on three primes and the three smallest Carmichael numbers: with rr between 8989 and 137137, the primes pass and every Carmichael number fails, for the first value of aa tried.

Why the test asks first about perfect powers

The wrapped test has a preliminary step that looks like a technicality and is not: before any polynomial is powered, it checks whether nn is a perfect power — a square, a cube, some mbm^b with b≥2b \ge 2 — and declares nn composite if so.

The reason is in the shape of the proof. Its argument shows that if the wrapped identity holds for enough values of aa, then nn has a prime factor pp for which a large collection of polynomials behave, modulo pp, as if nn were a power of pp; counting those polynomials in two ways then forces nn to be a power of pp. The count cannot distinguish pp from p2p^2 or p3p^3, so the proof ends at “nn is a power of a prime”, and the perfect-power check removes the powers. Checking whether a number is a perfect power is quick — for each exponent bb up to log⁡2n\log_2 n, compute an integer bb-th root by bisection and raise it back — and it closes the one gap the polynomial argument leaves.

The unwrapped identity has no such gap. (x+1)9(x + 1)^9 modulo 99 keeps the coefficient (93)=84\binom 93 = 84, which leaves 33, and prime powers fail the full row test exactly as other composites do. The cost of wrapping is the loss of that information, and the perfect-power check is what buys it back. Two primes where Fermat holds twice met the same distinction between a prime and its square from the other side: the squares of primes are where Fermat’s congruence can hold more strongly than it has to.

What was new about it

Fast primality tests existed long before 2002. The Miller–Rabin test, a strengthening of Fermat’s that checks square roots of one along the way, is fast and fooled by no composite for more than a quarter of bases, so forty random bases make an error astronomically unlikely. And an order that proves a prime showed that every prime has a short certificate anyone can check.

What neither supplies is a procedure that is fast, never wrong and needs no luck and no certificate. Miller–Rabin is fast and could in principle be wrong; made deterministic by trying every base up to a bound, it needs the generalised Riemann hypothesis to be sure the bound suffices. The polynomial test of Agrawal, Kayal and Saxena is unconditionally correct and deterministically fast, and its proof uses nothing deeper than finite fields and a counting argument about how many polynomials an impostor would have to satisfy. That it was found by a professor and two undergraduates, in a subject worked on for two thousand years, is part of why it was celebrated.

In practice it is not used. Its running time, even after improvements by Hendrik Lenstra and Carl Pomerance brought the exponent down to about the sixth power of the number of digits, is far slower than Miller–Rabin, and primes that need certifying are certified by elliptic-curve methods that produce short proofs. The polynomial test’s importance is that it settled a question — primality is decidable in polynomial time — not that it is how primes are found.

What the rings and rows cannot show

The necklaces are drawn for seven and six beads. The claim that prime lengths admit no short rings is general and simple; the claim that every composite length has a short ring is proved in words, with the evenly-spaced necklace, and illustrated at six.

Pascal’s triangle is drawn to row twenty. The figure checks the characterisation for every row it draws; the characterisation for every row is the necklace argument above.

The wrapped test is run with parameters chosen as the theorem requires, on six numbers below two thousand, and with one value of aa. The theorem’s guarantee needs many values of aa in general; that one sufficed for these Carmichael numbers is an observation about them, not a replacement for the theorem.

Still open: a wrapped test that needs one value of a

The polynomial test is slow because it must try many values of aa and use a large rr. A conjecture, going back to the work of Rajat Bhattacharjee and Prashant Pandey and stated in the paper of Agrawal, Kayal and Saxena, would remove both costs: if rr is a prime not dividing nn, and (x−1)n≡xn−1(x - 1)^n \equiv x^n - 1 modulo xr−1x^r - 1 and nn, then either nn is prime or n2≡1(modr)n^2 \equiv 1 \pmod r. It has been checked by computer for all nn below a very large bound and every small rr, and if true it would give a test with running time about the cube of the number of digits, faster than any proven deterministic test.

Lenstra and Pomerance gave a heuristic argument that counterexamples should exist, built from numbers with many prime factors of a special form, and none has been found. So the conjecture is supported by computation, opposed by a heuristic, and unproved — a small version of the relationship between Fermat’s test and the Carmichael numbers, one level up.

Counting each class, not the total

The habit worth keeping is the refinement of the count.

Fermat’s theorem came from counting all the strings of pp beads at once and noticing that the total, minus the constant strings, was a multiple of pp. A total can be right for the wrong reasons, and the Carmichael numbers are totals that are right for the wrong reasons. Counting the strings with each number of black beads separately — each entry of the row of Pascal’s triangle on its own — asks the same question n−1n - 1 times, and a composite number cannot answer all of them correctly. A sum of counts can be fooled; the counts themselves cannot, and the polynomial identity is the device that keeps them apart.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

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.

Binomial coefficientComplexityFermats little theoremModular arithmeticNecklacePolynomialPrimality testPrimes