ARITHMETIC AND NUMBER THEORY

Fermat’s Little Theorem

Fermat’s Little Theorem predicts what happens when an integer is raised to a prime-related power and then divided by that prime. It is a core result in modular arithmetic, with a short statement, a useful visual pattern, and applications in modular inverses and primality testing.

THE THEOREM

Two equivalent forms

If p is prime and a is an integer not divisible by p, then the (p − 1)st power of a leaves remainder 1 when divided by p.

ap−1≡1(modp)(p prime,  p∤a)a^{p-1}\equiv 1\pmod p\qquad (p\text{ prime},\;p\nmid a)

The equivalent form works for every integer a, including multiples of p:

ap≡a(modp)(p prime,  a∈Z)a^p\equiv a\pmod p\qquad (p\text{ prime},\;a\in\mathbb Z)

Read the condition carefully. The first form requires p ∤ a so that a has a multiplicative inverse modulo p. The second form has no such restriction.

What does “modulo p” mean?

Two integers are congruent modulo p when they have the same remainder after division by p. The notation x ≡ y (mod p) means that p divides the difference x − y. For example, 17 ≡ 3 (mod 7), because 17 − 3 = 14 is divisible by 7.

Instead of keeping a large power, we can reduce it to its remainder after each multiplication. With modulus 7, powers of 3 begin 3, 9, 27, 81, and so on. Their remainders cycle through the nonzero residues. The theorem says that after six factors of 3, the remainder returns to 1.

This arrangement is the central idea of the proof. When a number is relatively prime to the prime modulus, multiplying the nonzero residue classes by it permutes them rather than losing or duplicating any class.

Worked examples

Example 1: reduce a large power modulo a prime

Find the remainder when 3100 is divided by 7. Since 7 is prime and 7 ∤ 3, Fermat’s Little Theorem gives 36 ≡ 1 (mod 7). Divide the exponent 100 by 6: 100 = 6 · 16 + 4. Therefore 3100 = (36)16 · 34, so its remainder is the same as 34.

3100≡(36)16⋅34≡116⋅81≡4(mod7)3^{100}\equiv(3^6)^{16}\cdot3^4\equiv1^{16}\cdot81\equiv4\pmod7

Thus the remainder is 4. Check: 34 = 81, and 81 = 7 · 11 + 4. The theorem reduces the exponent; ordinary division verifies the final remainder.

Example 2: find a modular inverse

For prime p and p ∤ a, divide ap−1 ≡ 1 by one factor of a in modular arithmetic by multiplying both sides by the inverse of a. This gives ap−2 as the inverse of a modulo p. For instance, the inverse of 3 modulo 7 is 35 modulo 7. Since 35 = 243 ≡ 5 (mod 7), and 3 · 5 = 15 ≡ 1 (mod 7), the inverse is 5.

a−1≡ap−2(modp)(p∤a)a^{-1}\equiv a^{p-2}\pmod p\quad (p\nmid a)

Example 3: use the all-integers form

For the prime 5 and integer 12, the second form says 125 ≡ 12 (mod 5). Both sides reduce to 2, since 12 leaves remainder 2. If a is itself divisible by 5, say a = 10, then 105 ≡ 10 ≡ 0 (mod 5), so the form still holds.

125≡12≡2(mod5),105≡10≡0(mod5)12^5\equiv12\equiv2\pmod5,\qquad10^5\equiv10\equiv0\pmod5

Proof by rearranging the nonzero residues

Assume p is prime and p ∤ a. Consider the nonzero remainders 1, 2, …, p − 1. Multiply each by a and reduce modulo p. The resulting remainders are all nonzero: if ai ≡ 0 (mod p), primality and p ∤ a would force p | i, which is impossible for 1 ≤ i ≤ p − 1.

They are also distinct. If ai ≡ aj (mod p), then a(i − j) ≡ 0 (mod p). Since p does not divide a, Euclid’s Lemma implies p | (i − j). But the difference between two numbers from 1 to p − 1 has absolute value less than p, so the only multiple of p it can be is 0. Thus i = j. The multiplied list is therefore just a rearrangement of 1, 2, …, p − 1.

  1. 1
    Multiply every nonzero residue by a.

    The list a, 2a, …, (p − 1)a has the same nonzero remainders as 1, 2, …, p − 1, in a different order.

  2. 2
    Multiply the entries in both lists.
    a⋅2a⋯(p−1)a≡1⋅2⋯(p−1)(modp)a\cdot2a\cdots(p-1)a\equiv1\cdot2\cdots(p-1)\pmod p
  3. 3
    Collect the p − 1 copies of a.
    ap−1(p−1)!≡(p−1)!(modp)a^{p-1}(p-1)!\equiv(p-1)!\pmod p
  4. 4
    Cancel the factorial modulo p.

    None of 1, 2, …, p − 1 is divisible by p, so their product (p − 1)! has a modular inverse. Cancel it to obtain the theorem.

    ap−1≡1(modp)a^{p-1}\equiv1\pmod p

The cancellation step is valid because every nonzero residue modulo a prime has a multiplicative inverse. This is exactly where the assumption that p is prime matters. The same argument does not work for a composite modulus, where a nonzero residue may have no inverse.

Why the two theorem forms are equivalent

If p ∤ a, multiply ap−1 ≡ 1 by a to get ap ≡ a. If p | a, then both a and ap are congruent to 0 modulo p, so the same conclusion follows. This proves the all-integers version from the nonzero-residue version.

Conversely, suppose ap ≡ a (mod p) for every integer a, and choose an a not divisible by p. Since a has an inverse modulo p, multiplying by that inverse yields ap−1 ≡ 1. The two statements express the same result, with the second form making the condition on a explicit.

A second proof idea using the binomial theorem

The all-integers statement can also be proved by induction for nonnegative integers. The binomial theorem expands (a + 1)p. Every middle binomial coefficient, from k = 1 through k = p − 1, is divisible by the prime p. Modulo p, those middle terms disappear, leaving only ap + 1.

(a+1)p=ap+(p1)ap−1+⋯+(pp−1)a+1≡ap+1(modp)(a+1)^p=a^p+\binom p1a^{p-1}+\cdots+\binom p{p-1}a+1\equiv a^p+1\pmod p

Start with 0p ≡ 0. If ap ≡ a, then the expansion above gives (a + 1)p ≡ a + 1. Induction proves np ≡ n for every nonnegative integer n. Any negative integer has the same residue as a nonnegative representative modulo p, so the congruence extends to all integers.

This proof highlights why prime moduli are special: p divides each interior binomial coefficient. The permutation proof instead highlights the nonzero residues and cancellation. Both arguments establish the same theorem, but each makes a different structure visible.

A Fermat test can miss a composite number

Take 341 = 11 × 31. It is composite, yet it passes the Fermat congruence for base 2: 2340 ≡ 1 (mod 341). Modulo 11, 210 ≡ 1 and 340 is a multiple of 10. Modulo 31, 210 ≡ 1 as well, so the same 340th power is 1 modulo both 11 and 31. Since 11 and 31 are coprime, the Chinese Remainder Theorem gives remainder 1 modulo their product, 341.

341=11⋅31,2340≡1(mod11),2340≡1(mod31)⟹2340≡1(mod341)341=11\cdot31,\qquad 2^{340}\equiv1\pmod{11},\quad2^{340}\equiv1\pmod{31}\quad\Longrightarrow\quad2^{340}\equiv1\pmod{341}

This example does not contradict Fermat’s Little Theorem: its hypothesis says the modulus is prime. Instead, it shows why the converse is false. A prime must pass the relevant test, but a composite may pass for a chosen base. A failed test is decisive evidence of compositeness; a successful test is only evidence, and stronger primality tests are used when certainty is needed.

Where the theorem is useful

Reducing powers

When working modulo prime p with p ∤ a, exponents can be reduced using ap−1 ≡ 1. This makes otherwise enormous powers manageable.

Finding inverses

The formula ap−2 gives the multiplicative inverse of a modulo p. This is useful in modular arithmetic and finite-field computations.

Primality checks

If n is prime and gcd(a,n)=1, then an−1 ≡ 1 (mod n). A failure proves n is composite. A match does not prove primality: some composite numbers pass particular Fermat tests.

Number theory proofs

The theorem links powers, divisibility, and congruences. It is a stepping stone toward Euler’s theorem and many results about residues modulo integers.

For a concrete inverse calculation, to solve 3x ≡ 4 (mod 7), multiply both sides by 3−1 ≡ 5 (mod 7). Then x ≡ 20 ≡ 6 (mod 7). Check: 3 · 6 = 18 ≡ 4 (mod 7).

Assumptions and limits

  • The modulus must be prime. The theorem is not true for every composite modulus. For example, with modulus 8 and a = 3, 37 ≡ 3, not 1.
  • The first form requires p ∤ a. If p divides a, then ap−1 ≡ 0 (mod p), not 1. Use ap ≡ a when a may be a multiple of p.
  • A Fermat test can give a false positive. Congruence an−1 ≡ 1 is necessary for a prime n under the coprime condition, but not sufficient for every composite n.
  • Congruence is not ordinary equality. The theorem says the two numbers have the same remainder modulo p; their integer values can be very different.

A brief historical note

Pierre de Fermat stated the result in the seventeenth century as part of his work on prime numbers. He did not leave a published proof of this theorem. Leonhard Euler later supplied a proof in a paper on theorems concerning prime numbers, and the result became a standard tool in elementary number theory. The name “Little” distinguishes it from Fermat’s Last Theorem, a separate and much deeper statement about integer solutions to xn + yn = zn for n greater than 2.

Common mistakes to avoid

  • Forgetting to check primality. Do not use the exponent p − 1 rule with a composite modulus without a different theorem and its hypotheses.
  • Using the wrong exponent. The standard exponent is p − 1, not p. The all-integers version uses p and returns a.
  • Applying the first form when p divides a. The nonzero residue condition is essential to the value 1.
  • Reducing an exponent without justification. For a nonzero base modulo prime p, powers repeat with period dividing p − 1; reduce by p − 1 only after confirming p ∤ a.
  • Treating a Fermat test as a proof of primality. A counterexample base proves compositeness, but passing one base only means the number passed that test.

Frequently asked questions

What is Fermat’s Little Theorem in simple terms?

If p is prime and p does not divide a, then a raised to p − 1 has remainder 1 when divided by p. For example, 3⁶ leaves remainder 1 when divided by 7.

Why does the theorem use p − 1?

There are p − 1 nonzero remainders modulo p. Multiplying all of them by a nonzero residue rearranges that set. Comparing the products yields a factor of a repeated p − 1 times.

Does the theorem work when a is negative?

Yes. The statements apply to every integer a. Negative integers are handled by their congruence classes; for example, −2 ≡ 5 (mod 7).

Can Fermat’s Little Theorem prove that a number is prime?

A failed Fermat congruence proves a number is composite. Passing the test does not guarantee primality because certain composite numbers can satisfy the congruence for one or more bases.

How is this different from Fermat’s Last Theorem?

Fermat’s Little Theorem concerns powers modulo a prime. Fermat’s Last Theorem says that xⁿ + yⁿ = zⁿ has no positive integer solutions for n greater than 2. They are distinct theorems.

What should I study next?

Review divisibility and prime factorization first, then explore the Chinese Remainder Theorem to combine congruences with different moduli.

Historical reference: Euler’s original paper on theorems concerning prime numbers.