← SciSim / Mathematics

Modular Arithmetic & Fermat's Little Theorem

$a^{p-1}\equiv1\pmod{p}$ -- clock arithmetic, power cycles, RSA connection, primitive roots.
🎓 Tier: Standard Undergraduate -- Number Theory

📊 Section 1 -- Interactive Simulation

a mod n
--
a^p mod p
--
gcd(a,n)
--
Order of a
--
phi(n)
--
Primitive root?
--

Controls

Parameters

Display

💡 Section 2 -- The Idea, Step by Step

Modular arithmetic is the math of things that wrap around. Here it is built up from a clock face to the theorem behind RSA.

Everyday -- a clock that wraps

If it is $9$ o'clock and you wait $5$ hours, it becomes $2$ o'clock -- not $14$. A clock only has twelve numbers, so once you pass $12$ you start over. That "starting over" is the whole idea: $9+5=14$, but on a 12-hour clock, $14$ and $2$ are the same place.

The rule in symbols

The number you wrap around is the modulus $n$. We write $a\equiv b\pmod{n}$ to mean $a$ and $b$ land on the same spot -- equivalently, $n$ divides $a-b$. To find where a number lands, divide and keep the remainder: $17\bmod5=2$, because $17=3\cdot5+2$. In the sim, the Modulus $n$ slider sets the clock size and Base $a$ picks the number you drop onto it.

Powers and Fermat's surprise

Multiply a number by itself on the clock and the landing spots form a repeating cycle. Fermat's Little Theorem says something surprising: if $p$ is prime and $a$ shares no factor with it, then $a^{p-1}\equiv1\pmod{p}$ -- climb the ladder of powers $a^1,a^2,\ldots$ and you always return to $1$ within $p-1$ steps. For $a=3$, $p=7$: $3^6=729=104\cdot7+1\equiv1\pmod7$. This is what makes giant exponents cheap: to get $2^{100}\bmod13$ you never compute $2^{100}$ -- since $2^{12}\equiv1$, only $100\bmod12=4$ matters, giving $2^4=16\equiv3$. The Power $k$ slider walks up this ladder one rung at a time.

The Headline

$$a^{p-1}\equiv1\pmod{p}\quad(p\text{ prime},\ \gcd(a,p)=1)$$

Try this in the sim above

Set $n=7$, $a=3$ and step $k$ upward: every nonzero residue $1\ldots6$ lights up exactly once before the cycle returns to $1$ -- that base is a primitive root. Now keep $n=7$ but switch to $a=2$ and watch a shorter cycle that skips spots. Finally set $n=12$ (not prime) and notice the Primitive root? readout turn to No -- Fermat's clean return to $1$ is a privilege of prime moduli.

📐 Section 3 -- Modular Arithmetic & Fermat

Fermat's Little Theorem

If $p$ is prime and $\gcd(a,p)=1$, then $a^{p-1}\equiv1\pmod{p}$, equivalently $a^p\equiv a\pmod{p}$.

Euler's generalization: $a^{\phi(n)}\equiv1\pmod{n}$ for $\gcd(a,n)=1$ where $\phi(n)$ is Euler's totient.

SymbolDefinitionExample ($n=12$)
$a\equiv b\pmod{n}$$n\mid(a-b)$$26\equiv2\pmod{12}$
$\gcd(a,n)$Greatest common divisor$\gcd(9,12)=3$
$\phi(n)$Euler totient: count coprime to $n$$\phi(12)=4$
order of $a$Smallest $k$: $a^k\equiv1$ord$_7(3)=6$
primitive root$a$ with order $\phi(p)$3 is prim. root mod 7
Step 1 -- Modular Arithmetic Ring

$\mathbb{Z}/n\mathbb{Z}=\{0,1,\ldots,n-1\}$ with addition and multiplication mod $n$. Forms a ring. If $n$ is prime, it is a field $\mathbb{F}_p$ (every nonzero element has a multiplicative inverse). $5^{-1}\pmod7$: since $5\cdot3=15\equiv1$, so $5^{-1}\equiv3$.

Step 2 -- Proof of Fermat's Little Theorem

The set $\{a,2a,3a,\ldots,(p-1)a\}\pmod p$ is a permutation of $\{1,2,\ldots,p-1\}$ (since $\gcd(a,p)=1$). Multiplying: $(a)(2a)\cdots((p-1)a)\equiv(p-1)!\pmod p$. So $a^{p-1}(p-1)!\equiv(p-1)!$. Cancel $(p-1)!$ (invertible mod prime): $a^{p-1}\equiv1\pmod p$.

Step 3 -- Worked Example: $3^6\pmod7$

$3^1=3,3^2=9\equiv2,3^3=6,3^4=18\equiv4,3^5=12\equiv5,3^6=15\equiv1\pmod7$ ✓. Fermat: $3^{7-1}=3^6\equiv1\pmod7$ ✓. Order of 3 mod 7 is 6 = $\phi(7)$, so 3 is a primitive root.

Step 4 -- Fast Modular Exponentiation

Compute $a^k\pmod n$ in $O(\log k)$ multiplications by repeated squaring: $a^{12}=a^8\cdot a^4=(a^2)^4\cdot(a^2)^2$. Compute $a^2,a^4,a^8$ and multiply selected factors. Used in RSA encryption: $m^e\pmod n$ with $e$ having hundreds of bits.

Step 5 -- Chinese Remainder Theorem (CRT)

Given $x\equiv a_1\pmod{n_1}$, $x\equiv a_2\pmod{n_2}$ with $\gcd(n_1,n_2)=1$: unique solution $x\pmod{n_1n_2}$. Construction: $M=n_1n_2$, $M_1=n_2$, $M_2=n_1$, find inverses $y_i=M_i^{-1}\pmod{n_i}$, then $x=a_1M_1y_1+a_2M_2y_2\pmod M$.

Step 6 -- RSA Encryption Outline

Choose primes $p,q$; $n=pq$; $\phi(n)=(p-1)(q-1)$. Choose $e$ with $\gcd(e,\phi(n))=1$; $d=e^{-1}\pmod{\phi(n)}$. Public key $(n,e)$, private key $d$. Encrypt: $c=m^e\pmod n$. Decrypt: $c^d=m^{ed}\equiv m\pmod n$ by Euler's theorem. Security relies on hardness of factoring $n$.

Reference: Burton -- Elementary Number Theory, §2-4; Hardy & Wright -- An Introduction to the Theory of Numbers §7; Rosen -- Discrete Mathematics §4.

❓ Section 4 -- FAQ

🧮Conceptual  What does a mod n mean geometrically?

Think of a clock with $n$ positions (0 to $n-1$). $a\bmod n$ is where you land after $a$ steps clockwise. Addition mod $n$ wraps around. The simulation shows this on an actual clock. $26\bmod12=2$ -- after 2 full revolutions and 2 extra steps, land at 2.

Key takeaway: a mod n = position on n-clock after a steps. Addition wraps around the clock.
🔬Simulation  What does the power cycle show?

The graph plots $a^k\bmod p$ for $k=0,1,2,\ldots$. For a primitive root mod $p$, all values $1,2,\ldots,p-1$ appear exactly once in each period of length $p-1$. For non-primitive bases, the cycle is shorter. The cycle length = order of $a$ mod $p$.

Key takeaway: Power cycle: a^k mod p repeats with period = order of a. Primitive root: all p-1 nonzero residues appear.
🌍Applied  Where is Fermat's theorem used in practice?

RSA encryption (fundamental building block). Primality testing (Miller-Rabin, Fermat test). Hashing algorithms. Pseudorandom number generators (linear congruential). Diffie-Hellman key exchange uses discrete logarithm in $\mathbb{Z}_p^*$. Error-correcting codes (Reed-Solomon uses arithmetic in $\mathbb{F}_{2^k}$).

Key takeaway: RSA, primality testing, Diffie-Hellman, Reed-Solomon codes -- all built on modular arithmetic.
💡Non-Obvious  Fermat's test can give wrong answers (Carmichael numbers)?

If $a^{n-1}\equiv1\pmod n$ for all $\gcd(a,n)=1$, $n$ might still be composite -- these are Carmichael numbers (561, 1105, ...). The Fermat primality test is probabilistic, not deterministic. Miller-Rabin test strengthens it: no Carmichael numbers pass. AKS algorithm gives deterministic polynomial-time primality test.

Key takeaway: Carmichael numbers pass Fermat test but are composite. Miller-Rabin fixes this; AKS is deterministic.
📐Computational  Compute $2^{100}\pmod{13}$ using Fermat.

Fermat: $2^{12}\equiv1\pmod{13}$ (since 13 is prime, $\gcd(2,13)=1$). Write $100=12\cdot8+4$. So $2^{100}=(2^{12})^8\cdot2^4\equiv1^8\cdot16\equiv3\pmod{13}$. Check: $2^4=16=13+3\equiv3$ ✓. Fermat reduces large exponent to small one.

Key takeaway: 2^100 mod 13: 100 = 12*8 + 4, so 2^100 = (2^12)^8 * 2^4 = 1 * 16 = 3 mod 13.
🎓Deep  What is the connection between primitive roots and cyclic groups?

The multiplicative group $(\mathbb{Z}/p\mathbb{Z})^*$ has order $p-1$ and is cyclic. A primitive root $g$ is a generator: $g^0,g^1,\ldots,g^{p-2}$ are all $p-1$ nonzero residues. The discrete logarithm $\log_g a$ is the exponent. Primitive roots exist for primes and for $2,4,p^k,2p^k$. For composite moduli, the group may not be cyclic.

Key takeaway: (Z/pZ)* is cyclic of order p-1. Primitive root = generator. Discrete log is the inverse operation.
Best resources: MIT OCW 18.701; Brilliant.org -- Number Theory; Hardy & Wright Ch. 7.

⚠️ Section 5 -- Misconceptions & Common Errors

A · Conceptual Misconceptions
❌ Misconception: a^(p-1) = 1 for ALL a, not just gcd(a,p)=1.✅ Correction: Requires gcd(a,p)=1. For a=p or p|a: a^(p-1) is divisible by p, so a^(p-1) equiv 0, not 1. Fermat requires a not divisible by p.🔍 Students forget the coprimality condition.📖 Burton §2.
❌ Misconception: Fermat's Little Theorem implies primality.✅ Correction: If p is prime then a^(p-1)=1 mod p. Converse false: Carmichael numbers (e.g. 561=3x11x17) satisfy this for all coprime a but are composite.🔍 Students reverse the implication of Fermat's theorem.📖 Hardy & Wright §9.
❌ Misconception: a mod n and a are always different numbers.✅ Correction: If 0 <= a < n, then a mod n = a. Modular reduction only changes the value when a >= n or a < 0. Also, -1 mod 7 = 6, not -1.🔍 Students think reduction always changes the number.📖 Rosen -- §4.1.
B · Common Procedural Errors
❌ Error: Computing 3^100 mod 7 by computing 3^100 first.✅ Correct: Use Fermat: 3^6 = 1 mod 7. 100 = 6*16+4. 3^100 = (3^6)^16 * 3^4 = 1 * 81 = 81 mod 7 = 4. Never compute 3^100 directly.🔍 Students compute the full power before reducing, getting overflow.📖 Burton §2.
❌ Error: (a+b) mod n = (a mod n) + (b mod n) without final reduction.✅ Correct: (a+b) mod n = ((a mod n)+(b mod n)) mod n. The final mod n is required since (a mod n)+(b mod n) might exceed n.🔍 Students forget the outer mod n after adding the reduced values.📖 Rosen -- §4.1.
❌ Error: Writing phi(p^2) = p-1 for prime p.✅ Correct: phi(p^2) = p^2 - p = p(p-1). Numbers divisible by p in {1,...,p^2} are p,2p,...,p*p -- exactly p of them. phi(p^2) = p^2 - p.🔍 Students confuse phi(p) = p-1 with phi(p^k) = p^k - p^(k-1).📖 Burton §3.
Education research: Tall, D. -- Advanced Mathematical Thinking (2002).