Number

Necklaces that prove a theorem

Thread five beads in two colours, thirty-two ways. Two of them are all one colour; the other thirty fall into rings of five. That count, and nothing else, is Fermat's little theorem.

Worth reading first: Numbers that wrap.

Fermat’s little theorem says that for a prime pp and any whole number aa, the quantity apaa^p - a is divisible by pp. Stated that way it is a fact about remainders and there is no obvious reason for it. Stated as a count, it is a fact about jewellery.

Necklaces of 5 beads in 2 coloursEvery string of beads, grouped by the rotations that carry one onto another.1 string1 string5 strings5 strings5 strings5 strings5 strings5 strings32 strings fall into 8 necklaces32 strings in all: 2 constant ones, and 6 rings of 5so 32 − 2 = 5 × 6, and p divides a^p − a with nothing left over
Fig. 1 Every string of five beads in two colours, grouped by rotation. Two of the groups hold a single string — the all-black and the all-white — and the other six hold five each, since rotating any mixed string five times returns it and never earlier. The generator does the grouping and counts the classes rather than quoting the formula.

There are 25=322^5 = 32 strings. Two of them are constant. The remaining thirty are sorted into six groups of five. So 322=3032 - 2 = 30 is a multiple of 55, which is apaa^p - a divisible by pp for a=2a = 2 and p=5p = 5.

Nothing in that argument used a property of 22 or of 55 except that 55 is prime, and that is the theorem.

Why the groups have exactly pp members

Rotating a string of pp beads gives pp strings, possibly with repeats. The question is when repeats happen.

Suppose rotating a string by kk places returns it, for some kk between 11 and p1p-1. Then rotating by kk again returns it, and again; so rotating by any multiple of kk returns it. The multiples of kk modulo pp run through every residue, because pp is prime and kk is not a multiple of it — that is what makes a prime dial different. In particular rotating by one place returns the string, which means every bead equals its neighbour, which means the string is constant.

So a non-constant string has no rotation that fixes it except the trivial one, its group has exactly pp members, and the apaa^p - a non-constant strings divide evenly into groups of pp.

Every string of 5 beads in 2 coloursAll the strings, sorted so that rotations of the same necklace sit together.sorted by rotation class32 strings, sorted into 8 classes by rotationthe 2 single rows are the constant strings; every other class is exactly 5 rows
Fig. 2 The same thirty-two strings laid out in rows and sorted so that rotations of one necklace sit together. The two shaded rows are the constant strings; everything else comes in blocks of five. Nothing has been rearranged that was not already there — this is the previous figure unrolled.

The primality is doing exactly one job, and it is worth isolating. With p=4p = 4 the string 1010 is fixed by a rotation of two, so its group has two members instead of four, and the count breaks: 242=142^4 - 2 = 14 is not a multiple of 44. The theorem is false for composite exponents and the drawing shows precisely where.

Counting one collection two ways

The argument is an instance of the most productive move in this field, which is to count a set by two methods and set the answers equal.

Count one: the strings, directly. There are aa choices for each of pp positions, so apa^p of them.

Count two: by group. Each constant string is its own group, giving aa groups of size one. Every other group has size pp. So the total is a+p(number of mixed groups)a + p \cdot (\text{number of mixed groups}).

Equating, ap=a+pNa^p = a + p N, hence papap \mid a^p - a. The number N=(apa)/pN = (a^p - a)/p is not a leftover; it is the number of genuinely distinct necklaces, and its being a whole number is the theorem.

Necklaces of 3 beads in 3 coloursEvery string of beads, grouped by the rotations that carry one onto another.1 string1 string1 string3 strings3 strings3 strings3 strings3 strings3 strings3 strings3 strings27 strings fall into 11 necklaces27 strings in all: 3 constant ones, and 8 rings of 3so 27 − 3 = 3 × 8, and p divides a^p − a with nothing left over
Fig. 3 Three beads in three colours: 2727 strings, three constant, eight rings of three. So 273=24=3×827 - 3 = 24 = 3 \times 8, and there are eight distinct necklaces. The generator checks the class sizes and the arithmetic on every case it draws.

That both counts are of the same objects is the entire proof. There is no algebra, no induction, and no modular arithmetic in it — the divisibility appears because a set has been partitioned into pieces of equal size, which is what divisibility means.

The other proof, and what it costs

Fermat’s little theorem is usually proved a different way, and comparing the two is instructive.

The standard argument: for aa not divisible by pp, the multiples a,2a,3a,,(p1)aa, 2a, 3a, \ldots, (p-1)a are, modulo pp, a rearrangement of 1,2,,p11, 2, \ldots, p-1. Multiplying them together gives ap1(p1)!(p1)!(modp)a^{p-1}(p-1)! \equiv (p-1)! \pmod p, and cancelling the factorial — legitimate because pp is prime, so nothing below pp shares a factor with it — gives ap11a^{p-1} \equiv 1.

Multiplication on a dial of 7Multiplication on a dial of 7. The modulus is prime, so every non-zero row is a rearrangement of all the residues.000000001234560246135036251404152630531642065432100112233445566×7 is prime6 of 7 rows are rearrangementsof every residue
Fig. 4 Multiplication modulo seven. Every row is a rearrangement of the same seven residues, because seven is prime — which is the step the standard proof needs, drawn as a Latin square.

That proof is shorter and it hides where the primality enters. The necklace argument puts it in one place: a rotation class has pp members unless the string is constant. The algebraic argument puts it in two: the multiples permute the residues, and the factorial cancels. Both are correct; only one of them can be drawn.

Multiplying by 2, over and over, on a dial of 13Starting at 1 and multiplying by 2 each time closes after 12 steps, visiting 12 of the 12 non-zero residues. 12 divides 12, as it must.0123456789101112order of 212 steps to return2^12 = 1 (mod 13)12 divides 12
Fig. 5 Multiplying by two, repeatedly, on a dial of thirteen. The orbit closes after twelve steps, and twelve divides 13113 - 1 — which is Fermat’s theorem in its multiplicative form, and the orbit’s length is the order of two modulo thirteen.

The generalisation, and where it leads

Once the theorem is seen as a count of orbits, the generalisation writes itself: the argument never really needed pp to be prime, only that every non-trivial rotation moves every non-constant string.

Replacing rotations by an arbitrary finite group acting on a finite set gives the orbit-counting theorem, and the special case where all the orbits have equal size is the one above. Replacing the prime modulus by any modulus gives Euler’s theorem, aφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod n for aa coprime to nn, where φ\varphi counts the residues coprime to nn — and φ(p)=p1\varphi(p) = p - 1 recovers Fermat’s.

Lagrange’s theorem, that the order of a subgroup divides the order of the group, is the same statement again with the necklaces removed: the orbits are cosets, they have equal size, and the total is a multiple. The chain from a picture of beads to a theorem about groups is short, and every step of it is the observation that equal pieces divide a whole.

Euler’s version is the one that gets used, and it is the reason RSA works. Encryption raises a message to a public exponent ee modulo n=pqn = pq; decryption raises the result to a second exponent dd chosen so that ed1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}. Then med=m1+kφ(n)=m(mφ(n))kmm^{ed} = m^{1 + k\varphi(n)} = m \cdot (m^{\varphi(n)})^k \equiv m, by Euler’s theorem, and the message comes back. Computing φ(n)=(p1)(q1)\varphi(n) = (p-1)(q-1) needs the factorisation; anybody who cannot factor nn cannot find dd.

So the entire security argument rests on a theorem whose proof is thirty-two strings of beads sorted into eight piles. That is not a rhetorical flourish — the necklace argument, generalised by Euler’s count of coprime residues, is the theorem being invoked, and no deeper fact has been used anywhere in the scheme.

Counting the necklaces themselves

The theorem drops out of the count as a by-product; the count itself is worth having, and asking for it in general leads somewhere the theorem does not.

How many distinct necklaces of nn beads in aa colours are there, for nn not prime? Now the orbits have different sizes — a string with period dd dividing nn has an orbit of size dd — so the strings cannot simply be divided by nn. Sorting the strings by their exact period and counting each class gives

necklaces=1ndnφ(d)an/d,\text{necklaces} = \frac{1}{n}\sum_{d \mid n} \varphi(d)\, a^{n/d},

with φ\varphi Euler’s totient. For nn prime the sum has two terms and collapses to (apa)/p+a(a^p - a)/p + a, which is the count above.

Necklaces of 7 beads in 2 coloursEvery string of beads, grouped by the rotations that carry one onto another.1 string1 string7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings7 strings128 strings fall into 20 necklaces128 strings in all: 2 constant ones, and 18 rings of 7so 128 − 2 = 7 × 18, and p divides a^p − a with nothing left over
Fig. 6 Seven beads in two colours: 128128 strings, two constant, eighteen rings of seven. Seven being prime is what keeps every mixed class the same size — with eight beads the classes would have sizes eight, four, two and one, and no single division would sort them.

That formula is Burnside’s lemma applied to the rotation group, and it is the general form of the argument this essay makes in its special case: count the strings fixed by each rotation, average over the rotations, and the answer is the number of orbits. The necklace count is the standard first example of Pólya’s enumeration theory, which counts coloured objects up to symmetry and is the tool behind such questions as how many distinct dice faces, chemical isomers or tilings of a board exist.

So a proof of a theorem about remainders is also the first case of a general counting method, and the reason both exist is that dividing by a symmetry group is the same operation whether the aim is a count or a divisibility.

What the theorem is actually used for

Fermat’s little theorem is the basis of essentially every practical test for primality, and its use has an unusual logical shape.

If nn is prime, an11(modn)a^{n-1} \equiv 1 \pmod n for every aa coprime to nn. Turning that around: if an1≢1a^{n-1} \not\equiv 1 for some aa, then nn is definitely composite. The computation takes a few hundred modular multiplications by repeated squaring, whatever the size of nn, and it produces a certificate of compositeness without producing a single factor.

That asymmetry — proving a number composite while learning nothing about its factors — is what makes modern cryptography possible. Generating a thousand-digit prime means testing candidates until one passes, and the test never factors anything. The sieve, which is the only other way to establish primality, is hopeless at that size.

The converse fails, and it fails in an interesting way. Some composite numbers pass the test for a particular base — 341=11×31341 = 11 \times 31 satisfies 23401(mod341)2^{340} \equiv 1 \pmod{341} — and are called pseudoprimes to that base. Most are caught by trying another base. The Carmichael numbers, of which 561561 is the smallest, pass for every base coprime to them, and there are infinitely many; they are the reason the naive test is not sufficient and the Miller–Rabin refinement exists.

Miller–Rabin patches the leak with one extra observation, and the observation is again about what a prime modulus permits. If pp is prime, the only square roots of 11 modulo pp are ±1\pm 1 — because x21=(x1)(x+1)x^2 - 1 = (x-1)(x+1) and a prime dividing a product divides a factor. So while computing an1a^{n-1} by repeated squaring, watch the intermediate values: if a square root of 11 turns up that is neither 11 nor 1-1, the modulus is composite, and it is composite even if the final answer comes out right. Carmichael numbers cannot survive that, and each round has at most a one-in-four chance of being fooled by any composite, so forty rounds leave an error probability far below the chance of the hardware being wrong.

Note what that costs and what it does not. The test is probabilistic, it never produces a factor, and a number that passes is called a probable prime — deliberately, because the test proves compositeness and only makes primality overwhelmingly likely. Deterministic polynomial-time primality testing exists, since the AKS algorithm of 2002, and is slower in practice than doing the probabilistic test forty times.

What the picture cannot show

The figures draw every string, so they are limited to what can be drawn: five beads in two colours is thirty-two strings, and seven beads in three colours is two thousand one hundred and eighty-seven.

That is not a presentational limit but a structural one. The theorem is applied to nn of a thousand digits, where the number of strings is a number with 10100010^{1000} digits, and no counting argument is being performed — the modular exponentiation is. The picture explains why the theorem is true and gives no hint that it could be computed with.

There is a subtler gap. The figures show the orbits of a particular small case and the theorem quantifies over all aa and all primes pp. What a figure can do is make the mechanism visible — a class has pp members unless the string is constant — and the generalisation from that to all cases is done in the prose, as it is everywhere in this collection. The drawing is a proof of the mechanism and an illustration of the theorem, and the distinction between those is worth keeping.

One more thing is deliberately not drawn. A necklace, as an object, can be turned over as well as rotated, and reflections would merge some of the classes in the figures — the strings 10110 and 01101 are reflections and sit in different rotation classes here. The figures show the rotation classes only, because reflection is not what the theorem counts: including it would change the orbit sizes, break the equal division, and destroy the proof. A picture of beads on a ring invites the reader to pick it up and flip it, and the argument requires that nobody does.

That is a small illustration of a general hazard in figure-first work. A drawing carries connotations that the mathematics has not agreed to, and the more physical the drawing the more it carries. The safest defence is to say which operations are in play, which this caption now does.

Fermat, and the letter

Fermat stated the theorem in a letter to Frénicle de Bessy in October 1640, added that he would send the proof if it were not too long, and did not send it. That is entirely characteristic: of the many results he announced, he wrote out one complete proof in his life.

Euler published the first proof in 1736, using induction on aa and the binomial theorem — the key step being that the binomial coefficients (pk)\binom{p}{k} for 0<k<p0 < k < p are all divisible by pp, which is visible in Pascal’s triangle mod pp as a row of blanks. Euler’s proof is essentially the necklace argument with the necklaces replaced by algebra, since the binomial coefficient counts the strings with kk beads of one colour and the divisibility is the same orbit count.

The necklace proof itself is much later — it is usually credited to Julius Petersen in 1872 — and its arrival is a good illustration of a pattern. A theorem gets an algebraic proof first, because algebra is what the people who state theorems are holding; the combinatorial proof, which explains rather than verifies, tends to arrive a century afterwards when somebody asks what the algebra was counting.

The same delay shows up throughout this collection. The dissection proof of Pythagoras is older than the algebra, but the bijection behind Catalan numbers, the necklace count here, and the involution behind the two-squares theorem all arrived long after the results they explain. A combinatorial proof is harder to find and easier to understand, which is a poor trade for the person doing the finding and an excellent one for everybody afterwards.

Where the ladder goes next

The necklaces made one theorem about a prime modulus visible. The natural next question is what happens with two moduli at once, which is the Chinese remainder theorem — and its picture is a grid rather than a ring, with the same property that a walk fills it exactly when a coprimality condition holds.

Further along, the multiplicative structure this essay touched — orders, and the fact that they divide p1p-1 — is the beginning of the question of which residues are squares, and that question’s answer is quadratic reciprocity, which is again a count of one collection in two directions.

What links here

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

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.

Counting two waysCyclic groupFermats little theoremGroup actionModular arithmeticOrbitOrderPrimality testPrimes