Fermats Little Theorem and Eulers Theorem

Powers in Modular Arithmetic

In the last topic we dealt with linear congruences like . The natural next step is congruences involving higher powers of , such as ; but before we can say anything about those, we need to understand how powers behave in in the first place.

Consider the powers of every element of :

Notice how much structure is hiding in here:

  • In every row except , a eventually appears.
  • Column is (almost) all 's, and column is identical to column ; raising to the power does nothing in .
  • The first in each row appears in column or ; always a divisor of .
  • The cycle of entries in row is the reverse of the cycle in row .
  • Going down the rows, the entries of column cycle with length , and the entries of column cycle with length .

Running the same experiment in tells the same story: column is (almost) all 's, column repeats column , and the first of each row lands in column or — again, always a divisor of .

So in it looks like for every , and for absolutely every . This is not a coincidence.

Fermat's Little Theorem

Note

Fermat's Little Theorem
For any prime and any integer , we have

Similarly, for any prime and any integer such that , we have

Basically, modulo a prime, raising to the power is invisible, and every residue that is not a multiple of resets to after exactly steps; this is precisely the "column is all 's, column copies column " pattern from the tables. For the two forms are equivalent (multiply the second by to get the first), but they are not interchangeable in general. The second form requires ; if then and no power of it will ever be .

Proof.
If , then , so and the first form holds trivially.
So suppose that , and consider the numbers

None of these is modulo ; if then, since is prime, or , and both are impossible for . They are also pairwise distinct modulo ; if then , and since we must have , which forces as .

So the list above is just rearranged. Multiplying everything together,

Since is prime, it divides none of , so and is a unit modulo ; cancelling it from both sides gives

To see the rearrangement step in action, take and ;

which really is shuffled.

Example. Find .
Here is prime, so the first form of Fermat's Little Theorem applies to every integer with no coprimality check needed;

Therefore, ; the exponent matching the modulus means we get the answer for free.

Example. Find , and hence find .
Since is prime and , the second form gives

so . For the second power, we peel off as many blocks of from the exponent as possible; each block collapses to . Writing and noticing that ,

Therefore, . Notice how switching for turned an impossible-looking power into something you can do by hand.

Reducing Exponents Modulo a Prime

The "peel off blocks of " trick from the last example is worth stating as a theorem in its own right.

Note

Theorem
For any prime and integer such that , we have for all integers that

Proof. By the division theorem, write where , so . Then

Basically, when working modulo a prime , the bases live in but the exponents live in . It is important to remember that exponents get reduced modulo , not modulo ; reducing an exponent mod is the classic exam mistake.

Example. Find .
Here is prime and , so exponents can be reduced modulo ;

Therefore, .

Powers in Non-Prime Moduli

Fermat's Little Theorem is strictly about prime moduli. To see what breaks otherwise, here is the power table for :

We can observe some familiar patterns, and one new pathology:

  • A eventually appears in every row except , and .
  • Column is mostly 's, and column is similar to column .
  • The first in each row appears in column or .
  • The rows for and collapse to and never recover.

The rows that die are exactly the elements sharing a factor with ; indeed , and once you hit you stay there. The rows that do return to are exactly the units of , and there are of them — which explains why column plays the role that column played in the prime tables. What we need is a function that counts these "good" elements for an arbitrary modulus.

Euler's Totient Function

Note

Definition
The (Euler) totient function (or Euler's phi function) is the function that for any positive integer outputs the number of positive integers less than or equal to that are coprime with . That is,

Recall from Modular Rings and Units that ; so is exactly the number of units in ,

Basically, counts how many residues mod you are allowed to divide by; equivalently, how many rows of the power table will eventually return to .

Example. Find the following values.

  • ; since is prime, all of are coprime with it.
  • ; we only delete the multiples of , leaving — matching the six "surviving" rows in the table above.
  • ; the survivors are .
  • ; the survivors are .
  • For any prime number , we have ; every one of is coprime with .

Notice that the in Fermat's Little Theorem was really in disguise; this is the observation that Euler generalised.

Totients of Prime Powers

Computing straight from the definition means checking a gcd for every single , which is hopeless for large . The following two results reduce the whole computation to knowing the prime factorisation of .

Note

Theorem
For any prime and ,

Proof. The only prime factor of is , so if and only if . Hence we take all integers in and delete the multiples of , which are

there are exactly of them. Everything remaining is coprime with , so .

Basically, exactly in every consecutive integers is a multiple of , so a prime power keeps a share of its residues as units.

Example. Find and .

As a sanity check for the first one, the odd residues are precisely the units of ; there are indeed of them. Therefore, and .

The Totient is Multiplicative

Note

Theorem
For any with , we have

Proof. First, notice that if and only if and ; any prime dividing both and must divide or , and conversely any common prime factor of and (or ) also divides .

Now write the integers in a grid with columns, so that column contains

Every entry of column is congruent to modulo ; so the columns containing numbers coprime with are exactly the columns with , and there are of these.

Fix one such column and reduce its entries modulo . They are pairwise distinct; if then , and since we get , forcing as . So the entries of the column hit every residue modulo exactly once, and hence exactly of them are coprime with .

Counting: good columns, each contributing entries coprime with both and ; i.e. coprime with . Therefore .

To see this concretely with and , lay out to :

Column is dead (everything shares the factor ), while columns and each contain every residue mod once, hence units each. That gives units in total, and indeed .

It is important to note that is only multiplicative when ; for instance but . Always split into coprime pieces (i.e. into distinct prime powers), never arbitrary factors.

Example. Find .
Factorising and splitting into pairwise coprime prime powers,

Therefore, ; there are units in .

Combining the two theorems gives a one-line formula:

Note

Corollary
For any , given that the distinct prime factors of are , we have

Basically, each distinct prime factor independently deletes its own share of the residues. Notice that the exponents in the factorisation never appear in the product; only which primes divide matters, with the exponents hiding inside the leading factor of .

Example. Find .

Therefore, .

Euler's Theorem

We can now generalise Fermat's Little Theorem to any modulus, with stepping into the role of .

Note

Euler's Theorem
For any and any such that , we have

Equivalently, for any , we have that for all ,

Setting prime recovers Fermat's Little Theorem exactly, since and is the same condition as . Basically, the units of all return to after steps — which is why column worked in the table () even though is not prime. Unlike Fermat's Little Theorem, there is no "first form" here: if then Euler's Theorem says nothing at all about .

The proof is the same permutation trick as before, but run over the units instead of over all nonzero residues.

Proof. Let be the elements of ; by definition of there are exactly of them. Since , is itself a unit, and products of units are units, so each . Moreover the are pairwise distinct in ; if , multiplying both sides by (which exists as is a unit) gives , so . Hence is just rearranged. Multiplying everything together,

Since is a product of units, it is itself a unit; multiplying both sides by gives

For a concrete instance of the shuffle, take and ; the units of are , and multiplying each by gives

the same set shuffled.

Example. Find .
First, , so Euler's Theorem applies, and

so . Reducing the exponent,

Therefore, .

Example. Find and .
Here and

so and exponents can be reduced modulo . For the first power, , so

For the second, , so . Rather than grinding out , notice that ;

Therefore, and . Spotting a power congruent to is one of the best shortcuts available; it halves the effective cycle length.

Example. Find .
The tempting move is: and , "so" . This is wrong; , so Euler's Theorem does not apply and you are not allowed to reduce the exponent. Indeed every positive power of is even, so none of them can possibly be congruent to modulo .

The correct approach is to split into coprime pieces, , and study each separately;

Now we need the unique with satisfying and ; checking the candidates , only works. Therefore, . Notice that Euler was still useful on the coprime piece (as ); the trick is to quarantine the shared factor first.

Finding Inverses with Euler's Theorem

Euler's Theorem also hands us a formula for inverses. Since

we immediately get

This is an alternative to the extended Euclidean algorithm from Bezouts Identity and the Extended Euclidean Algorithm; it is usually slower by hand, but it is a one-line formula, which makes it useful in proofs (and in RSA-style computations later).

Example. Use Euler's Theorem to find in .
We have and

so . Computing by squaring,

Check: . Therefore, in .

Reducing Exponents in General

Just as with primes, the real day-to-day use of Euler's Theorem is collapsing huge exponents.

Note

Theorem
For any and such that , we have for all that

Proof. By the division theorem, write where , so . Then

So when , bases live in but exponents live in ;

Exponents are reduced modulo , never modulo itself.

Example. Find .
This is a tower of powers, so we work from the outside in. The outer modulus is prime with , so the exponent only matters modulo . That turns the problem into finding ; and since with , that exponent only matters modulo ;

Therefore, . Notice how each layer of the tower drops down one level: powers mod are governed by , and powers mod are governed by .

Fast Modular Exponentiation

Reducing the exponent with Euler's Theorem is only half the job; the reduced exponent can still be uncomfortably large (anything up to ), and multiplying one factor at a time takes forever. The fix is repeated squaring: square the base over and over, reducing modulo at every step, to obtain

and then multiply together the powers matching the binary expansion of the exponent. This needs roughly multiplications instead of , and because we reduce mod after every squaring, the numbers involved never get big.

Example. Find the last two digits of .
The last two digits of a number are exactly its remainder mod , so we want . First reduce the exponent using the totient function: and

and since , we have . Now , so we build up by repeated squaring;

Therefore, the last two digits of are .

For any "huge power mod " question, the routine is always the same: check first (if it is not , split into coprime pieces and quarantine the shared factor); factorise to compute ; reduce the exponent modulo — or modulo when is prime; and finish off whatever exponent remains with repeated squaring, keeping an eye out for a handy along the way.