Fermat’s little theorem
By: Cédric Bellet
Tags: Bitwise series, Rsa, Bitwise, Encryption, Mathematics

Part 1: Progressions and Powers

Every prime number measures infallibly one of the powers minus one of any progression

Fermat calls a “progression” what is nowadays known in French known as a “suite géométrique”, and in English still known as a “geometric progression”, that is to say, a “sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio.” For example, “2, 4, 8, 16, 32, etc.” is a progression of ratio 2, “3, 9, 27, 81, etc.” is a progression of ratio 3.

If we use the convention that progressions start from their ratio r, we can see that the n-th element of any such progression is r to the power n, or the n-th power of ratio r. For example, the third element of the progression of ratio 2 is 8 = 2³ or the third power of 2, and for the progression of ratio 3, it is 27 = 3³, the third power of 3.

When Fermat mentions a given n-th “power of any progression”, he therefore means a given n-th “element” of a geometric progression.

If we stack progressions vertically in ascending ratio order, we can get a better sense of what Fermat’s little theorem’s first fragment means:

A table of geometric progressions. Each row reads the first 6 powers of one of the first 10 geometric progressions, each column reads the n-th power of all first 10 progressions. Fermat states that for each prime number, there exists a column of this table such that the column’s values minus one are multiples of that prime

Fermat states that “every prime number measures one of the powers minus one of any progression”, that is to say, for every prime number, there exists a column of this table such that all numbers of that column minus one are multiples of that prime. For instance, in column 6, the sixth powers minus one are: 63 — 728 — 4,095 — 15,624 — 46,655 — 117,648 — 262,143 — 531,440 — 999,999, which are multiples of prime number 7 — with the exception of the sixth power minus one from the progression of 7 (117,648) (see NB). Hence for prime 7, there does exist a column of the table such that 7 divides almost all powers minus one.

NB: the accurate statement that Fermat should have made, is that “every prime number measures one of the powers minus one of any progression”… with the exceptions of progressions whose ratios are a multiple of the given prime,(e.g. for prime 7, progressions 7, 14, 21, etc.). This is “fixed” in the modern formulation, specifying that “if p is a prime and a is any integer not divisible by p, then a ^ (p − 1) − 1 is divisible by p.”

Part 2: Primes minus one

…the exponent of said power is a sub-multiple of the given prime minus one; and, after one has found the first power that satisfies the question, all powers whose exponents are multiples of the exponent of the first one satisfy similarly the question.

In the second half of his proposition, Fermat posits that powers which exhibit this behaviour are sub-multiples of the prime minus one. For example, for prime 7, candidate powers are 2, 3, or 6. Powers 2 and 3 do not verify the property of having their values minus 1 divisible by 7 (e.g. 9²-1=80 is not divisible by 7), but power 6 does (as shown in Part 1). Power 6 is therefore “the first power that satisfies the question”, and Fermat goes on to say that as a result, all multiples of 6 also verify the property that their values minus one are divisible by 7 (e.g., n¹²-1 should be divisible by 7 for all n other than multiples of 7).

This means that, according to Fermat, the prime minus one always verifies the proposition: indeed, if at least one sub-multiple of the prime minus one is valid, and the multiples of that solution are all valid too, then the prime minus one is either the first valid exponent, or a multiple of the first valid exponent, hence valid itself.

As a result, the theorem can be reformulated as:

(A) Given a prime p, the (p-1)-th power minus one of any progression (except those whose ratio is a multiple of p) is divisible by p. (B) If p-1 is not prime, then one or more of p-1’s sub-multiples may share the same property. (C) Multiples of exponents found in (A) and (B) also share the property that their powers are divisible by p.

Several proofs of (A) exist for example on Wikipedia. We can on the other hand easily prove (C) here: if aⁿ-1 ≡ 0 [mod p] for all integers a not divisible by p, then given a multiple n ·m of n, we have (aᵐ)ⁿ-1≡ 0 [mod p] too, since aᵐ is another integer not divisible by p. (B) requires that we find one and only one instance where sub-multiples of the prime minus one indeed verify the same property… but, I am unable to find or build such an example. Nevertheless, the fact that Fermat thought it important to mention that possibility seems in my mind to nicely foreshadow the work of Euler more than a century later and that of Carmichael in the 20th century (more on this thought in Part 4).

At this point we are mostly done deciphering Fermat’s original statement. One might wonder however, whether the relationship identified in (A) is anything special, that is, whether it only occurs for primes, or in fact happens for most numbers, which would make the proposition a lot less interesting. With a computer, we can empirically answer that question by calculating a table of the first n powers modulo n+1 of the first m progressions, similar to what we did in Part 1.

This is the same table as the one from Part 1, modulo the column’s (power + 1). For example, line 4, column 5, contains 4⁵ mod 6 = 1024 mod 6 = 4.

Patterns emerge: in each column n, we see the same sequence of n numbers repeated vertically, separated by zeros every n+1 rows: those zeros appear where the progression’s ratio is a multiple of the power + 1. As a result, the bloc of numbers under the first border of zeros (coloured yellow) is repeated between the first and second wall of zeros (coloured green), and again between second and third one, etc. In the columns for powers 1, 2, 4, 6, the only value other than 0 is always 1. They are the columns for the primes 2, 3, 5, 7, an encouraging indication that proposition (A) is predominantly true for primes indeed.

Replicating the above chart with more powers and progressions, with a coloured scale to represent numbers, we get the following chart: