Concept stop · What is it?

Fermat's Little Theorem and Its Uses

In the Chinese book: 费马小定理及其应用

Mod a prime p, keep multiplying 1 by a: after p−1 steps you are back at 1.

📖 §2.3 of 《整除、同余与不定方程》M23 Guess, Then Prove line

Before this stop, visit:Residue Systems and Their Uses (Concept)

Hands first, words secondPress “Multiply by … again” to keep multiplying by a, starting from 1. How many steps until you are back at 1? Try another a.

Loading figure…

Try this

  1. , : go step by step, . It takes steps to get back to . Switch to : how many steps? And ?
  2. Write down the number of steps for each of . How are they related to ?
  3. The number of steps is always a divisor of , so after exactly steps you always land on . Look at the “Fermat test” row below.
  4. Change to or . Is the Fermat test row all s?
  5. Change to or : some values of never get back to at all. What shows up in that row?

Fermat's little theorem Let be a prime and an integer not divisible by . Then

Multiply both sides by : , and this form holds for every integer .

Predict first

Mod , start at and keep multiplying by (where is not a multiple of ). The number of steps until you first get back to cannot be:

Choose an answer first; then you can see the result.

Why

The proof uses nothing but the picture from the previous stop: multiplying by is a reordering.

Multiply out both sides of the reordering

Step 1 / 6
Loading figure…
  1. The reordering

    Mod , multiply by . stays put; are sent to . These are the same six numbers, just in a different order.

Remove one condition

Break it

Counterexample lab: remove one condition

Conclusion: every not divisible by satisfies

. All cells of the Fermat test are . Click different cells to change , then press “Finish”: the numbers of steps differ, but they are all divisors of .

Loading figure…

Practice

Exercise 1Basic

Find the remainder when is divided by .

Exercise 2Basic

Find the remainder when is divided by .

Exercise 3Advanced

Prove that for every integer , is divisible by .

Exercise 4Challenge

The prime satisfies . Find .

Mistake Museum · Exhibits from this stop

Before you leave, look at the traps other people fell into here.See every exhibit in the museum →

Exhibit

Using Fermat's little theorem when the modulus is not prime

The wrong step: By Fermat's little theorem, .
What went wrong? Think first, then open

Diagnosis is not prime, so the theorem says nothing about it. In fact , so .

The right step: Make sure the modulus is prime first. For a composite modulus, either find the period directly (here it is ) or split it into prime moduli and handle each one.

“Check the condition before using the theorem: is p prime?”

Exhibit

A base that is a multiple of p

The wrong step: .
What went wrong? Think first, then open

Diagnosis , so . The theorem needs : start from , multiply by , and once you fall into you never get out.

The right step: If you don't want to check this condition, use the other form: , which holds for every integer .

“a must not be a multiple of p”

Exhibit

Using the theorem backwards

The wrong step: , so is prime.
What went wrong? Think first, then open

Diagnosis . It just happens that , so . The theorem says “prime ⇒ remainder 1”, not “remainder 1 ⇒ prime”.

The right step: If the remainder is not , you can be sure the number is composite; if it is , you can conclude nothing. Try another base: .

“The theorem only works in one direction”