JetBrains logo and wordmark

Lecture 2 / Modular arithmetic

Wilson’s,
Fermat’s, and
Euler’s Theorems

Modular Inverses

Youth Math Club 26/27 · Level 2

October 10th, 2026

Fedor Bakharev

Modular division
Factorials
Powers

I / Inverses

When may we cancel?

Problem

Compare and . Can we cancel?

Solution

denotes the greatest common divisor. Coprime means .

But .

I / Inverses

Multiplication rearranges residues

Theorem

If , then give all residues modulo , each once.

Proof

No prime factor of divides . Every prime power in must therefore divide .

Since , we obtain . There are distinct outputs and only possible residues. Hence every residue occurs once.

I / Inverses

A rearrangement modulo 11

Example

Multiply each residue by and reduce modulo .

012345678910
036914710258

Every residue from to appears exactly once, in a different order.

As the theorem predicts: .

I / Inverses

Modular inverses

Definition and theorem

An inverse of modulo is a number with . It exists exactly when , and is unique modulo .

Proof

If , the rearrangement contains exactly once.

We denote this inverse residue by .

A common divisor of divides , so .

Solving a congruence

Multiplying back by verifies the solution.

I / Inverses

An inverse solves the congruence

Problem

Solve .

Solution

Thus .

I / Inverses

Finding an inverse by Euclid

Problem

Find , then solve .

Solution

Euclidean algorithm

Back substitution

I / Inverses

A common divisor changes the modulus

Problem

Solve . Does have a solution?

Solution

In , the numbers are .

If , then . Since , this forces : impossible.

The second congruence has no solution.

I / Inverses

Residues that equal their inverses

Theorem

For an odd prime , the only solutions of are and .

Proof

For , . Modulo , all of have square .

II / Wilson

Inverse pairs modulo eleven

Problem

Find by pairing inverse residues.

Solution

The nonzero residues are .

The unpaired factors are and .

II / Wilson

Wilson's theorem

Theorem

For every prime , .

Proof

Let be odd. Each has a unique inverse .

If , pair with . The pairs do not overlap because .

The only unpaired factors are and .

For : .

II / Wilson

Wilson's converse

Theorem

For , if , then is prime.

Proof

Suppose is composite. Choose a proper divisor :

The factor occurs in . The hypothesis and also give

Subtract the two divisible integers:

Impossible, since . Hence is prime.

, since .

II / Wilson

A factorial near a prime

Problem

Find the remainder when is divided by .

Solution

This cancellation is valid because .

II / Wilson

A square root of minus one

Problem

Let be prime. Prove that has a solution.

Solution

Put .

Pair with , for .

Multiply the pair congruences:

Since , is even. Thus

Wilson gives , so

For , this gives .

III / Fermat

Fermat's little theorem

Theorem

If is prime and , then .

Proof

No factor of is divisible by . Hence , so cancellation is valid.

III / Fermat

Fermat's theorem for every integer

Theorem

For every prime and every integer , .

Proof

Case 1:

When , canceling recovers .

Case 2:

The second congruence holds for every integer .

III / Fermat

Reducing a large exponent

Problem

Find .

Solution

Base: modulo . Exponent: modulo .

III / Fermat

A large number is composite

Problem

Prove that is composite.

Solution

Try the prime : and .

The number is greater than , so it is composite.

III / Fermat

Counting words by rotation

A word is an ordered string of symbols. With , there are words of length .

AAAABAAABAAABAAABAAABAAAA
AAAAB

The constant words are and . There are nonconstant words.

Group each nonconstant word with its rotations. Two groups are either identical or disjoint.

Why must every nonconstant group contain exactly five words?

III / Fermat

Why prime length gives p rotations

Theorem

A nonconstant word of prime length has exactly distinct cyclic rotations.

Proof

If two rotations coincide, a shift by some , with , leaves the word unchanged.

Since , these positions are distinct and visit every position.

An unchanged word has the same symbol along this entire list. It would be constant: a contradiction.

III / Fermat

A combinatorial proof of Fermat

Theorem

For a prime and every integer , .

Proof by counting

For , use an alphabet of symbols and words of length .

The nonconstant words split into disjoint rotation groups, each containing exactly words.

For , the congruence is immediate: .

For any integer , choose with .

III / Fermat

Fermat from the binomial theorem

Lemma

If is prime and , then .

Proof and application

counts -element subsets of a -element set.

The right side is , and .

Start with . If , then . Induction proves every .

Reducing an arbitrary integer modulo handles the remaining cases.

III / Fermat

Fermat has no primality converse

Problem

Does force to be prime?

Solution

So the answer is no. A single Fermat congruence does not certify primality.

This counterexample concerns the single base .

IV / Totient

Euler's totient function

Definition

is the number of integers with and .

. For , we can equivalently count coprime residues in .

IV / Totient

Totients of prime powers

Theorem

For a prime and an integer , .

Proof

An integer fails to be coprime to exactly when it is a multiple of .

There are excluded integers among total integers.

IV / Totient

Counting the totient of sixty

Problem

Compute , using the prime divisors .

Solution

Excluded once: . Twice: . Three times: .

IV / Totient

The totient formulas

Theorem

If , then .

Theorem

If , with distinct primes , then .

Example

IV / Totient

Using the totient formula

Problem

Compute and .

Solution

, because .

V / Euler

Multiplication on the coprime residues

Modulo , the coprime residues are .

15711
51117

Here and .

V / Euler

Euler's theorem

Theorem

If and , then .

Proof

Let , and let be the coprime residues modulo .

No prime divisor of divides or , so each is coprime to . If , cancellation gives .

Thus rearrange .

Every factor of is coprime to . Thus , so cancel .

V / Euler

Fermat is the prime case

ModulusCoprime residuesEuler gives
Prime
Any

In each row, the base must be coprime to the modulus.

For prime , .

V / Euler

The last two digits of a power

Problem

Find the last two decimal digits of .

Solution

, so Euler applies.

Euler guarantees exponent ; here exponent already works.

V / Euler

A smaller exponent modulo twenty-four

Problem

Prove whenever .

Solution

Since is odd, are consecutive even integers: one is a multiple of , the other of .

This is stronger than the exponent .

, since .

, so .

V / Euler

Inverses from powers

Corollary

If , then .

Proof

For , , the inverse of every odd residue.

VI / Many prime divisors

More than a thousand prime divisors

Problem

Call rich if has more than distinct prime divisors. Is the set of rich integers finite?

VI / Many prime divisors

More than a thousand prime divisors

Proof

Choose distinct primes . Put and .

For , we have . Euler gives, for every integer ,

Thus has at least distinct prime divisors.

VII / Review

Choosing the right theorem

TaskMethod and condition
Solve Use an inverse if .
A factorial near a primeWilson: .
A large power modulo a primeFermat: reduce the exponent modulo if .
A large power modulo a compositeEuler: reduce modulo if .
Many prescribed prime divisorsChoose one exponent that works for all the chosen primes.