Fermat & Euler — The Cycle of Powers a1, a2, … mod m

The powers of a mod m loop back to 1 after exactly ord(a) steps — and ord(a) always divides φ(m). The totient φ(m) counts the residues coprime to m.

step 0

Power cycle of a mod m

residue 1 (target) current ak visited

Facts & totient

Residues 1..m — coprime to m are counted by φ(m):
Power sequence ak mod m:
Press Step to begin. We multiply by a each step, watching ak mod m walk around until it returns to 1 — that return happens at k = ord(a), a divisor of φ(m).

What to try

The yellow tiles are the residues coprime to m; their count is φ(m). The cycle length (ord of a) always divides that count whenever a is coprime to m — this is exactly why aφ(m) ≡ 1.

How the theorems show up

Self-contained visualization. Fermat's little theorem is the case m = prime (then φ(m) = m−1); Euler's theorem aφ(m) ≡ 1 holds for every a coprime to m because ord(a) divides φ(m). See junior.md and professional.md for the proofs. When gcd(a, m) > 1 the powers never reach 1 (no cycle through 1) — the animation flags this.