Maths › Further Pure 2 › Modular arithmetic and Fermat's little theorem

Modular arithmetic and Fermat's little theorem

Modular arithmetic works with remainders rather than the numbers themselves, which brings enormous powers within reach. Fermat's little theorem then reduces such powers still further.

Builds on The Euclidean algorithm and Bézout's identity and Induction: divisibility.

IN THIS TOPIC

  • Use the congruence laws for addition, subtraction, multiplication and powers.
  • Apply the standard divisibility tests, including for composite divisors, and justify them with congruences.
  • Solve linear congruence equations by inverting the coefficient, and systems of them by substitution.
  • Decide which numbers are squares to a given modulus, and solve a quadratic congruence or prove it insoluble.
  • Use both forms of Fermat's little theorem, and say why passing its test does not make a number prime.
  • Reduce a sum raised to a prime power using the binomial coefficients that the prime divides.

COMMON MISCONCEPTION

To find the remainder when 4 to the power 20 is divided by 7 you must first compute the number.

Congruences may be added, subtracted, multiplied and raised to powers, so reducing at every step keeps the numbers small. Here 4² = 16 ≡ 2 (mod 7), and squaring on from there gives 4²⁰ ≡ 2 with nothing larger than 16 ever handled. Fermat's little theorem is shorter still: 4⁶ ≡ 1, and 20 = 3 × 6 + 2, so 4²⁰ ≡ 4² ≡ 2.

Arithmetic with remainders

a ≡ b (mod n) means that n divides a − b. Congruences may be added, subtracted and multiplied like equations, and multiplying a congruence by itself gives the power law, so a ≡ b forces ak ≡ bk. Reducing at every step keeps the numbers small, and no giant power ever gets computed.

Division is the one operation that does not simply carry over. You cannot cancel a common factor unless it is coprime to the modulus, and 2 × 3 ≡ 2 × 6 (mod 6) with 3 not congruent to 6 shows why.

WORKED EXAMPLE

A huge power, reduced twice over

Find the least positive residue of 4²⁰ modulo 7.

Directly: 4² = 16 ≡ 2, so 4⁴ ≡ 4, 4⁸ ≡ 2 and 4¹⁶ ≡ 4. Then 4²⁰ = 4¹⁶ × 4⁴ ≡ 4 × 4 = 16 ≡ 2.

Fermat's route is shorter still, as the section on Fermat's little theorem shows. Either way the largest number handled is 16.

Where the digit tests come from

The elementary divisibility tests are congruences with the notation stripped out. Since 10 ≡ 1 (mod 9), every power of 10 is too, so a number is congruent to its digit sum modulo 9, and modulo 3 for the same reason. Since 10 ≡ −1 (mod 11), the powers of 10 alternate in sign, which produces the alternating digit sum test for 11.

An exam question will usually want the justification as well as the test. Write the number as a sum of digits times powers of ten, replace each power by its residue, and the rule follows in two lines.

Why the digit tests work: 10 is 1 modulo 9, so every place counts once, and 10 is −1 modulo 11, so the places alternate
Describe the drawing

Two columns either side of a dividing line. On the left, ten is congruent to one modulo nine, so the digits of nine one eight are simply added to eighteen, which is a multiple of nine. On the right, ten is congruent to minus one modulo eleven, so the digits are added and subtracted alternately to minus twenty-two, a multiple of eleven.

FIG. 1Why the digit tests work: 10 ≡ 1 modulo 9 makes every place value count once, while 10 ≡ −1 modulo 11 makes them alternate.

The remaining standard tests read the end of the number rather than all of it, for the same reason seen from the other side: 100 = 4 × 25 and 1000 = 8 × 125, so every place value from the hundreds up is 0 modulo 4, and from the thousands up is 0 modulo 8. A number is divisible by 2 or by 5 according to its last digit, by 4 according to the number made by its last two digits, and by 8 according to its last three.

That is seven tests, for 2, 3, 4, 5, 8, 9 and 11, and a composite divisor needs no eighth. Split it into coprime factors and test each in turn: 6 as 2 and 3, 12 as 4 and 3, 72 as 8 and 9. The factors have to be coprime for the two tests to combine, since 6 passes the tests for 2 and for 6 without being a multiple of 12.

The strip below reduces one seven-digit number place by place, and both halves of the test for 72 can be read off it.

Place values of 3419208 reduced modulo 4 and modulo 8: every place from the hundreds up vanishes mod 4 and from the thousands up mod 8, so only the end of the number is read
Describe the drawing

The seven place values of three million four hundred and nineteen thousand two hundred and eight, each with its power of ten reduced modulo four and modulo eight underneath. From the hundreds up every residue is nought modulo four, and from the thousands up every residue is nought modulo eight, so those places are greyed out and contribute nothing. A dashed line splits the number after the thousands: only the last three digits, two hundred and eight, which is eight times twenty-six, decide the test for eight, while the digit sum of twenty-seven decides the test for nine.

FIG. 2The place values of 3419208 reduced modulo 4 and modulo 8: everything from the hundreds up is 0 modulo 4 and everything from the thousands up is 0 modulo 8, so the last three digits carry the test for 8 while the digit sum carries the test for 9.

WORKED EXAMPLE

One number, two tests

Is 3419208 divisible by 72?

72 = 8 × 9, and 8 and 9 are coprime, so test each and combine.

Last three digits: 208 = 8 × 26, so 8 divides the number.

Digit sum: 3 + 4 + 1 + 9 + 2 + 0 + 8 = 27, which 9 divides, so 9 divides the number.

Both hold, so 72 divides it, and the quotient is 47489. Neither test needed the seven-digit number to be divided.

Fermat's little theorem

For a prime p and any a not divisible by p:

ap-1≡1(mod p)a^{p-1} \equiv 1 \, (\text{mod } p)

Powers therefore repeat with a period dividing p − 1, so an exponent can be reduced modulo p − 1 before anything is computed. Take 4²⁰ modulo 7. Since 4⁶ ≡ 1, write 20 = 3 × 6 + 2, and 4²⁰ ≡ 4² = 16 ≡ 2. One line, and no proof of the theorem is required here.

There is a second form that asks nothing of the base. Multiply the first by a and it becomes ap ≡ a (mod p), which now holds for a divisible by p as well, both sides being 0 there. Use the first form to shrink an exponent, and the second when the base might be a multiple of the prime, as it is whenever the base is a letter rather than a number.

The implication runs one way. Every prime satisfies these congruences, but a number satisfying one for some base need not be prime: 2³⁴⁰ ≡ 1 (mod 341) while 341 = 11 × 31. A composite that passes is called a pseudo-prime to that base. This one fails the test at base 3, where 3³⁴⁰ ≡ 56 rather than 1, which proves 341 composite; passing the test at one base does not prove a number prime.

Six boxes in a row holding the residues four, two, one, four, two, one for the first six powers of four modulo seven, with the ones highlighted. A coral arrow underneath runs back to the start to show the cycle repeating, and the twentieth power is reduced to two in a single line.
FIG. 3Powers of 4 modulo 7 falling into a short repeating cycle, with Fermat guaranteeing that the sixth power comes back to 1.

WORKED EXAMPLE

Solving a congruence equation

Solve 7x ≡ 3 (mod 11).

First invert 7. Since 7 × 8 = 56 = 55 + 1 ≡ 1, the inverse of 7 is 8, and Bézout on 7 and 11 would find the same number.

Multiply both sides: x ≡ 8 × 3 = 24 ≡ 2 (mod 11).

Checking, 7 × 2 = 14 ≡ 3. A linear congruence has a solution exactly when the highest common factor of the coefficient and the modulus divides the right-hand side.

GUIDED PRACTICE

A power and a test

Find the least positive residue of 3¹⁰⁰ modulo 5, and decide whether 918082 is divisible by 11.

Show the working

By Fermat, 3⁴ ≡ 1 (mod 5), and 100 = 25 × 4, so 3¹⁰⁰ ≡ 1.

For 11, take the alternating digit sum from the right: 2 − 8 + 0 − 8 + 1 − 9 = −22.

Since 11 divides −22, the number is divisible by 11. Long division confirms it and gives 83462, but the test needed no division.

Squares to a modulus

A quadratic residue modulo n is a number congruent to a square. Which numbers those are is settled by squaring the residues and reading the list off, and the list is shorter than it looks, since x and n − x have the same square. Modulo 7 the squares run 0, 1, 4, 2, 2, 4, 1, so the non-zero residues are 1, 2 and 4 and the non-residues are 3, 5 and 6. For an odd prime p that pairing leaves (p − 1)/2 of each.

The list settles a congruence in x² at once. Modulo 7, x² ≡ 2 has the solutions x ≡ 3 and x ≡ 4, read straight off the squares, while x² ≡ 3 has no solution, since 3 is not among them; quoting the list beside that sentence is the whole proof.

A general quadratic reduces to that shape by completing the square, with the awkward coefficient cleared by multiplying rather than by dividing. Multiply ax² + bx + c ≡ 0 through by 4a, which is reversible to an odd prime modulus that does not divide a, and the congruence becomes (2ax + b)² ≡ b² − 4ac. So the discriminant decides it: the congruence is soluble when b² − 4ac is a quadratic residue and insoluble when it is not, and the square roots of a residue are read from the table.

The squares to a modulus answer questions that do not look like congruences at all. Modulo 10 they are 0, 1, 4, 9, 6, 5, 6, 9, 4, 1, so a square ends in 0, 1, 4, 5, 6 or 9, and a number ending in 2, 3, 7 or 8 is not one.

x012345
x² modulo 11014953

WORKED EXAMPLE

Two quadratic congruences

Solve x² + 3x + 1 ≡ 0 (mod 11), and show that x² + 5x + 2 ≡ 0 (mod 11) has no solution.

The table gives the squares for x up to 5, and x and 11 − x agree, so the non-zero quadratic residues modulo 11 are 1, 3, 4, 5 and 9.

For the first, multiply by 4: (2x + 3)² ≡ 9 − 4 = 5 (mod 11). Now 5 is a residue, with 4² = 16 ≡ 5, so 2x + 3 ≡ 4 or 2x + 3 ≡ −4 ≡ 7.

The inverse of 2 modulo 11 is 6, since 12 ≡ 1. From 2x ≡ 1 comes x ≡ 6, and from 2x ≡ 4 comes x ≡ 24 ≡ 2, so the solutions are x ≡ 2 and x ≡ 6 (mod 11).

Checking, 4 + 6 + 1 = 11 and 36 + 18 + 1 = 55, both multiples of 11.

For the second, multiplying by 4 gives (2x + 5)² ≡ 25 − 8 = 17 ≡ 6 (mod 11). But 6 is not one of the residues 1, 3, 4, 5, 9, so no x satisfies it. Naming the discriminant and the table it is missing from is the argument in full.

A prime exponent and the binomial theorem

Look at row p of Pascal's triangle for a prime p. The inner coefficient pCr = p!/(r!(p − r)!) has a factor of p upstairs, while downstairs r! and (p − r)! are products of numbers below p. A prime dividing a product divides one of its factors, which is Euclid's lemma from the previous lesson, so p divides neither factorial and the factor of p survives the cancelling. Row 5 reads 1, 5, 10, 10, 5, 1, and its four inner entries are multiples of 5.

The argument needs p to be prime: in row 4 the middle entry 6 is not a multiple of 4.

Rows 4, 5 and 7 of Pascal's triangle: every inner entry of a prime row is a multiple of that prime, while row 4 keeps a 6 that 4 does not divide
Describe the drawing

Three rows of Pascal's triangle drawn as strips of boxes. Row four reads one, four, six, four, one, and its middle entry of six is ringed in coral because four does not divide it. Row five reads one, five, ten, ten, five, one and row seven reads one, seven, twenty-one, thirty-five, thirty-five, twenty-one, seven, one; every inner entry of both is ringed in amber, since five divides all four of its inner entries and seven divides all six of its own. The outer ones are left plain, and the failure in row four is why the argument is stated for a prime.

FIG. 4Rows 4, 5 and 7 of Pascal's triangle with the inner entries their row number divides marked: every one of them in rows 5 and 7, but not the 6 in the middle of row 4.

So p divides pCr for every r strictly between 0 and p. Expand (a + b)p by the binomial theorem, reduce modulo p, and the whole middle of the expansion goes to 0, leaving (a + b)p ≡ ap + bp (mod p). Applying it repeatedly extends it to a sum of any length, and applied to a copies of 1 it delivers Fermat's second form, which is one reason a proof of that form is not required here.

WORKED EXAMPLE

A seventh power of a sum

Show that (x + 2)⁷ ≡ x + 2 (mod 7) for every integer x, and check the result at x = 3.

The binomial result splits the bracket: (x + 2)⁷ ≡ x⁷ + 2⁷ (mod 7).

Fermat's second form cuts each piece down. x⁷ ≡ x for any x, and 2⁷ = 128 = 7 × 18 + 2 ≡ 2.

So (x + 2)⁷ ≡ x + 2 (mod 7).

At x = 3 that predicts 5⁷ ≡ 5. Indeed 5⁷ = 78125 = 7 × 11160 + 5. The second form is the one to quote here, since x may be a multiple of 7.

Two congruences at once

A system asks which numbers satisfy several congruences together. Solve the first on its own, write its solution as a general form, substitute that into the second, and solve the smaller congruence that comes out. Each round turns two conditions into one, so three congruences take two rounds. With coprime moduli the answer is a single residue class to their product, which is why 5, 7 and 4 below collapse to a modulus of 140. Ticking off what each congruence allows shows the same thing from the outside.

Three congruences ticked off along 0 to 140: the columns coincide at 17 alone, and the next agreement is 157, a full period of 140 later
Describe the drawing

Three rows of ticks laid over the whole numbers from nought to one hundred and forty. The top row ticks every fifth number starting at two, the middle row every seventh starting at three, and the bottom row every fourth starting at one. Only one column carries a tick in all three rows, at seventeen, which is marked by a dashed line running down through the three. The moduli are coprime, so the pattern repeats after five times seven times four, and the next such column is at one hundred and fifty-seven, off the right-hand end of the strip.

FIG. 5Three congruences ticked along the numbers from 0 to 140: the three rows agree in one column only, at 17, and the coprime moduli set the period at 5 × 7 × 4 = 140, so the next agreement is 157.

Moduli sharing a factor may still agree, but they may also clash, and the clash appears as a congruence with no solution. Nothing is both 1 more than a multiple of 4 and 2 more than a multiple of 6, since the first is odd and the second even. The Chinese remainder theorem writes the answer down directly for coprime moduli, and may be quoted; the substitution above quotes nothing.

WORKED EXAMPLE

Three conditions, one class

Solve x ≡ 2 (mod 5), x ≡ 3 (mod 7) and x ≡ 1 (mod 4).

From the first, x = 5t + 2. Substituting into the second, 5t + 2 ≡ 3 (mod 7), so 5t ≡ 1.

The inverse of 5 modulo 7 is 3, since 15 ≡ 1, so t ≡ 3 (mod 7) and t = 7s + 3. Then x = 35s + 17, that is x ≡ 17 (mod 35).

Now fold in the third. 35s + 17 ≡ 1 (mod 4) becomes 3s + 1 ≡ 1, so 3s ≡ 0 and, 3 being coprime to 4, s ≡ 0 (mod 4).

So s = 4u and x = 140u + 17, giving x ≡ 17 (mod 140). Checking, 17 leaves 2, 3 and 1 to the three moduli.

INDEPENDENT PRACTICE

A pair with coefficients

Solve 3x ≡ 2 (mod 5) and 4x ≡ 3 (mod 7) simultaneously.

Show the working

Clear the coefficients first. Since 3 × 2 = 6 ≡ 1 (mod 5), the inverse of 3 is 2 and x ≡ 2 × 2 = 4 (mod 5). Since 4 × 2 = 8 ≡ 1 (mod 7), the inverse of 4 is 2 and x ≡ 2 × 3 = 6 (mod 7).

Then x = 5t + 4, and 5t + 4 ≡ 6 (mod 7) gives 5t ≡ 2, so t ≡ 3 × 2 = 6 (mod 7) and t = 7s + 6.

So x = 35s + 34, that is x ≡ 34 (mod 35). Check both: 3 × 34 = 102 ≡ 2 (mod 5) and 4 × 34 = 136 ≡ 3 (mod 7).

Inverting the coefficients before combining keeps the substitution step free of stray multipliers.

ASSESSMENT FOCUS

  • Reduce at every step. Carrying large numbers forward wastes time and invites arithmetic slips.
  • Quote Fermat's little theorem by name, and check the base is not a multiple of the prime before using it.
  • To solve a linear congruence, find the inverse of the coefficient instead of trying to divide.
  • For a divisibility test, quote the congruence that justifies the rule as well as the rule itself.
  • Test a composite divisor through coprime factors, and say that they are coprime before combining the results.
  • In a system of congruences, solve one, write x in the general form it gives, and substitute. Do not guess a common solution and hope.
  • State the answer as a least positive residue unless the question says otherwise.
  • For a quadratic congruence, multiply by 4a to complete the square, then look the discriminant up among the squares to that modulus. If it is not there, say which residues are squares and stop.

CHECK YOURSELF

Use Fermat's little theorem to find the least positive residue of 2¹⁰⁰ modulo 13.

Show a hint

The theorem gives a power that is congruent to 1.

Show the answer

2¹² ≡ 1 (mod 13), and 100 = 8 × 12 + 4, so 2¹⁰⁰ ≡ 2⁴ = 16 ≡ 3.

Congruences add, subtract, multiply and take powers; reduce at every step to keep the numbers small.

Cancelling is only safe when the common factor is coprime to the modulus; a quadratic congruence to an odd prime modulus is soluble when its discriminant is a square to that modulus.

Fermat: for prime p and a not divisible by p, a to the power p − 1 is 1 modulo p, so exponents reduce mod p − 1. The second form, a to the p is a, holds for every base, and composites can pass it.

For a system, solve one congruence, substitute its general form into the next; for a prime power of a sum, the middle of the binomial expansion vanishes.

WORKBOOK

Printable practice for this topic: original exam-style questions with room to work, and a fully worked answer book. Free to use; please do not redistribute or sell.

15 questions on this topicAnswer them one at a time and mark yourself against the worked answer.Practise this topic

Or read them with their worked answers on the modular arithmetic and Fermat's little theorem questions page.

CHECK YOUR PROGRESS

Rate how confident you feel with each objective for this lesson. Ratings are saved in this browser, on this device, unless you sign in.

  • Use the congruence laws for addition, subtraction, multiplication and powers.
  • Apply the standard divisibility tests, including for composite divisors, and justify them with congruences.
  • Solve linear congruence equations by inverting the coefficient, and systems of them by substitution.
  • Decide which numbers are squares to a given modulus, and solve a quadratic congruence or prove it insoluble.
  • Use both forms of Fermat's little theorem, and say why passing its test does not make a number prime.
  • Reduce a sum raised to a prime power using the binomial coefficients that the prime divides.

Open the full revision checklist to see every objective in the course in one place.