Kestrel Exams kestrel logo KESTREL EXAMSPRACTICE TO PASS
Discrete Math8 min read

Congruences and Fermat’s Little Theorem

Clock arithmetic with a formal name. You will learn to read a ≡ b (mod n), to add and multiply congruences safely, to find the remainder of a number like 3100 in three lines, and why the converse of Fermat’s theorem fails. Every number here was checked by computer.

Practice this topic in the free app →All Discrete Math guides

You already do this with a clock#

It is 9 o’clock. What time will it be in 5 hours? Not 14. It will be 2. On a clock, 14 and 2 are the same position, because they differ by a full lap of 12.

That is all a congruence is: two numbers that land in the same place once you wrap around every n. On a clock n is 12. In discrete math n can be any positive whole number, and it is called the modulus.

What a ≡ b (mod n) means#

Read a ≡ b (mod n) out loud as “a is congruent to b, mod n”. It means:

n divides a − b. In other words, a − b = n · k for some whole number k (k may be negative or zero).

An equivalent way to say it: a and b leave the same remainder when you divide by n. That is the clock picture.

StatementCheck the differenceTrue?
17 ≡ 2 (mod 5)17 − 2 = 15 = 5 · 3Yes
−3 ≡ 4 (mod 7)−3 − 4 = −7 = 7 · (−1)Yes
14 ≡ 3 (mod 5)14 − 3 = 11No, 11 is not a multiple of 5

The notation was introduced by Gauss because congruences behave so much like equations. The next section shows exactly how much.

What you are allowed to do with congruences#

Suppose a ≡ b and c ≡ d, both mod n. Then all of these are safe:

  • Add: a + c ≡ b + d (mod n).
  • Subtract: a − c ≡ b − d (mod n).
  • Multiply: ac ≡ bd (mod n).
  • Raise to a power: ak ≡ bk (mod n), because a power is repeated multiplication.

Why multiplication works: write a = b + nk and c = d + nm. Then ac = bd + n(bm + dk + nkm). The extra piece is a multiple of n, so ac and bd are congruent. Addition works the same way, with less algebra.

Division is the one that can fail. 6 ≡ 16 (mod 10) is true, since 16 − 6 = 10. Divide both sides by 2 and you get 3 ≡ 8 (mod 10), which is false, since 8 − 3 = 5.

The division rule

You may cancel a factor c from both sides only when c and the modulus n share no common factor (gcd(c, n) = 1, read “the greatest common divisor of c and n is 1”). Mod 10 you can cancel 3, but not 2 or 5.

Fermat’s little theorem#

Here is the theorem, in words first. Take a prime p. Take any whole number a that is not a multiple of p. Raise a to the power p − 1. The result is always one more than a multiple of p.

In symbols: if p is prime and p does not divide a, then ap−1 ≡ 1 (mod p).

Check it with a = 2, as in the lecture:

p2p−1Written as a multiple of p, plus 1
524 = 163 · 5 + 1
726 = 649 · 7 + 1
11210 = 102493 · 11 + 1

“Little” is there to tell it apart from Fermat’s last theorem, a different and much harder result. Both are called FLT, which causes confusion.

A second form needs no condition on a: ap ≡ a (mod p) for every whole number a. If p does not divide a you can cancel one a (the division rule allows it, since gcd(a, p) = 1) and get the first form back.

Huge powers made small#

This is where the theorem earns marks on a test. You are asked for the remainder of an enormous power. You never compute the power.

Example 1: 2100 divided by 11. Fermat with p = 11 says 210 ≡ 1. Then 2100 = (210)10 ≡ 110 = 1. The remainder is 1.

Example 2: 3100 divided by 7. Fermat with p = 7 says 36 ≡ 1. Divide the exponent by 6: 100 = 6 · 16 + 4. So

3100 = (36)16 · 34 ≡ 1 · 81 ≡ 4 (mod 7), since 81 = 77 + 4.

The recipe is always the same: find the power that gives 1, divide the exponent by it, and keep only the leftover.

Why p divides np − n: the induction proof#

The lecture proved the second form for p = 3 and p = 5 by induction on n. Here is p = 5. Let M(n) = n5 − n.

Basis step. M(1) = 1 − 1 = 0, and 5 divides 0.

Inductive step. Assume 5 divides M(n). Look at the difference M(n + 1) − M(n). Expanding (n + 1)5 uses row 5 of Pascal’s triangle, 1, 5, 10, 10, 5, 1:

(n + 1)5 − (n + 1) − n5 + n = 5n4 + 10n3 + 10n2 + 5n = 5(n4 + 2n3 + 2n2 + n).

That is a multiple of 5. So M(n + 1) = M(n) + (a multiple of 5), and M(n) is a multiple of 5 by the induction hypothesis. The sum of two multiples of 5 is a multiple of 5. By the principle of mathematical induction, 5 divides n5 − n for every n ≥ 1.

What made it work: every middle number in row 5 of Pascal’s triangle is a multiple of 5. The same is true for every prime row. Row 7 is 1, 7, 21, 35, 35, 21, 7, 1, and 7, 21, 35 are all multiples of 7. That is your homework case, and the proof is word for word the same.

The converse is false: 341 and 561#

The converse of a theorem swaps its hypothesis and conclusion. Here it would say: if 2n−1 ≡ 1 (mod n), then n is prime. People once hoped to use this as a primality test.

It fails. Take n = 341. It is not prime: 341 = 11 · 31. Yet 2340 ≡ 1 (mod 341). The hypothesis holds and the conclusion fails, and one such example is enough to kill an implication.

You might hope to repair it by testing every base a, not just 2. For 341 that works, since 3340 is not ≡ 1 (mod 341). But n = 561 = 3 · 11 · 17 defeats even that: a560 ≡ 1 (mod 561) for every a that shares no factor with 561. Numbers like 561 are called Carmichael numbers, and 561 is the smallest one.

One sentence to carry into the exam

Fermat goes one way only: prime p gives ap−1 ≡ 1, but an−1 ≡ 1 does not give prime n, and 341 is the counterexample.

Where this goes wrong#

  • Forgetting the condition on a. ap−1 ≡ 1 needs p not dividing a. For a = 7 and p = 7, 76 ≡ 0, not 1.
  • Using a composite modulus. The theorem is about primes. 28 = 256 leaves remainder 4 on division by 9, not 1.
  • Dividing a congruence by a shared factor. Check gcd(c, n) = 1 before cancelling c.
  • Reducing the exponent mod p instead of mod p − 1. For 3100 mod 7 you divide 100 by 6, not by 7.
  • Trying to prove the converse. If a question asks you to disprove it, the answer is one counterexample, such as 341 with base 2.

Test yourself in the free Kestrel Exams app

Topic-selectable Discrete Math practice — offline, no ads, no account.

Practice congruences and Fermat →

Frequently asked questions#

What does a ≡ b (mod n) mean?

It means n divides a − b, so a − b is a whole-number multiple of n. Equivalently, a and b leave the same remainder when divided by n. For example 17 ≡ 2 (mod 5) because 17 − 2 = 15 = 5 · 3.

What is Fermat's little theorem?

If p is prime and a is not a multiple of p, then a^(p−1) ≡ 1 (mod p). An equivalent form that holds for every whole number a is a^p ≡ a (mod p).

How do I find the remainder of a huge power like 3^100 divided by 7?

Use Fermat: 3^6 ≡ 1 (mod 7). Divide the exponent by 6: 100 = 6 · 16 + 4. So 3^100 = (3^6)^16 · 3^4 ≡ 3^4 = 81 ≡ 4 (mod 7). The remainder is 4.

Is the converse of Fermat's little theorem true?

No. 341 = 11 · 31 is composite, yet 2^340 ≡ 1 (mod 341). Worse, 561 = 3 · 11 · 17 satisfies a^560 ≡ 1 (mod 561) for every a relatively prime to 561. Such numbers are called Carmichael numbers.

Can you divide both sides of a congruence?

Only by a number that shares no factor with the modulus. 6 ≡ 16 (mod 10) is true, but dividing by 2 gives 3 ≡ 8 (mod 10), which is false, because 2 and 10 share the factor 2.

Suggest a change

Something here not clear? A topic you wish we covered? Tell us. We read every message, and a request is the fastest way to get a guide written — several of these exist because somebody asked.