Number TheoryFermat, Euler and Wilson

Fermat Tests and Pseudoprimes

If aⁿ⁻¹ ≢ 1 (mod n) for some a with 1 < a < n, then n is composite; passing the test does not prove that n is prime.

Not started Jump to practice

Fermat's theorem gives a fast way to show a number is composite without finding a factor. That is how computers screen the huge numbers used in cryptography. But the test has a blind spot, and knowing exactly what it proves is the lesson here.

The Fermat test. To test $n$, pick a base $a$ with $1 < a < n - 1$ and compute $a^{n-1} \bmod n$ by repeated squaring.

  • If $a^{n-1} \not\equiv 1 \pmod{n}$, then $n$ is certainly composite.
  • If $a^{n-1} \equiv 1 \pmod{n}$, then $n$ passes: it is probably prime, but not proven prime.

Why it works

Fermat's theorem says: if $n$ is prime (and $n \nmid a$), then $a^{n-1} \equiv 1$. Its contrapositive is just as true: if $a^{n-1} \not\equiv 1$, then $n$ is not prime. But the converse, "if $a^{n-1} \equiv 1$ then $n$ is prime," is a different statement, and it is false. A composite $n$ with $a^{n-1} \equiv 1 \pmod{n}$ is called a pseudoprime to base $a$.

Worked example: Caught by the test

Run the base-2 test on $n = 91$.
  1. $90 = 64 + 16 + 8 + 2$. Squares mod 91: $2^{2} = 4$, $2^{4} = 16$, $2^{8} = 256 \equiv 74$, $2^{16} \equiv 74^{2} = 5476 \equiv 16$, $2^{32} \equiv 74$, $2^{64} \equiv 16$.
  2. $2^{90} \equiv 16 \cdot 16 \cdot 74 \cdot 4$: $16 \cdot 16 = 256 \equiv 74$, then $74 \cdot 74 \equiv 16$, then $16 \cdot 4 = 64$.
  3. $2^{90} \equiv 64 \not\equiv 1$, so $91$ is composite. (Indeed $91 = 7 \cdot 13$, but the test never needed that.)

Answer: Composite.

Worked example: A pseudoprime

Show that $341 = 11 \cdot 31$ passes the base-2 test.
  1. $2^{10} = 1024 = 3 \cdot 341 + 1$, so $2^{10} \equiv 1 \pmod{341}$.
  2. $340 = 10 \cdot 34$, so $2^{340} = (2^{10})^{34} \equiv 1 \pmod{341}$.
  3. $341$ passes, yet it is composite: it is the smallest pseudoprime to base 2.

Answer: $2^{340} \equiv 1 \pmod{341}$.

Changing the base often exposes a pseudoprime: $3^{340} \bmod 341 = 56$, so base 3 catches 341. But $91$ fools base 3 ($3^{6} = 729 = 8 \cdot 91 + 1$, so $3^{90} \equiv 1$), and some composites fool every base coprime to them. These are the Carmichael numbers.

Worked example: A Carmichael number

Korselt's criterion: a composite $n$ is a Carmichael number exactly when it is squarefree and $p - 1 \mid n - 1$ for every prime $p \mid n$. Check $561 = 3 \cdot 11 \cdot 17$.
  1. $561$ is squarefree, and $n - 1 = 560$.
  2. $2 \mid 560$, $10 \mid 560$ and $16 \mid 560$ (since $560 = 16 \cdot 35$).
  3. So $561$ passes the Fermat test for every base coprime to it, though it is composite.

Answer: Yes, $561$ is a Carmichael number (the smallest).

Common mistake

Treating the converse of Fermat's theorem as true: "$2^{340} \equiv 1 \pmod{341}$, so 341 is prime." But $341 = 11 \cdot 31$. A result of 1 only means "not caught yet." A result other than 1 is a proof of compositeness. Real primality tests (like Miller–Rabin, a strengthened Fermat test) use several bases and a sharper condition.

Key idea

If $a^{n-1} \not\equiv 1 \pmod{n}$, then $n$ is composite. If $a^{n-1} \equiv 1$, $n$ may still be composite: a pseudoprime, or for every base at once, a Carmichael number.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.

Loading practice…

Builds on

Leads to

This is one of the top rungs of Number Theory.

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 2, The Ring of Integers Modulo n (primality testing)
  • MIT OpenCourseWare 18.781 Theory of Numbers (Spring 2012), lecture notes on primality testing and Carmichael numbers
@teddyrookbook GitHub