MathCompass

Modular Arithmetic II: Advanced

Number Theory Advanced

πŸ“‹ Prerequisites

Now we go deeper into modular arithmetic: inverses (the modular version of division), Fermat's little theorem, Euler's theorem, Wilson's theorem, and fast exponentiation. These are the workhorses of competition number theory β€” they let you compute huge powers instantly and solve congruences like a pro.

πŸ“š Key Concepts

Modular Inverse

The inverse of $a$ modulo $m$ is a number $a^{-1}$ such that:

\[ a \cdot a^{-1} \equiv 1 \pmod{m} \]

The inverse exists if and only if gcd(a, m) = 1 (a and m are coprime).

When it exists, it's unique mod m.

\(Example: 3 \\times 5 = 15 \\equiv 1 (mod 7), so the inverse of 3 mod 7 is 5.\)

How to Find Inverses

Method 1: Brute force (for small m) β€” \(try a \\times 1, a \\times 2, ... until you get 1 mod m.\)

Method 2: Extended Euclidean Algorithm β€” find integers x, y such that ax + my = 1. Then x mod m is the inverse.

Method 3: Fermat's Little Theorem (when m is prime) β€” see below.

Fermat's Little Theorem (FLT)

If $p$ is prime and $a$ is not divisible by $p$:

\[ a^{p-1} \equiv 1 \pmod{p} \]

Equivalently: $ a^p \equiv a \pmod{p} $ for any integer a.

Application to inverses: If p is prime and p ∀ a, then $ a^{-1} \equiv a^{p-2} \pmod{p} $.

Why: multiply both sides of $ a^{p-1} \equiv 1 $ by $ a^{-1} $ to get $ a^{p-2} \equiv a^{-1} $.

Application to exponents: $ a^k \equiv a^{k \bmod (p-1)} \pmod{p} $ (when a not divisible by p). Reduces huge exponents fast!

Euler's Totient Function Ο†(n)

Ο†(n) counts how many integers from 1 to n are coprime to n.

Formula: If $ n = p_1^{a_1} p_2^{a_2} ... p_k^{a_k} $:

\[ \varphi(n) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right)...\left(1 - \frac{1}{p_k}\right) \]

Examples:

Euler's Theorem

If gcd(a, n) = 1:

\[ a^{\varphi(n)} \equiv 1 \pmod{n} \]

This is the generalization of Fermat's Little Theorem to composite moduli.

FLT is the special case where n is prime (Ο†(p) = p-1).

Application: $ a^k \equiv a^{k \bmod \varphi(n)} \pmod{n} $ when gcd(a,n) = 1. Reduces exponents modulo Ο†(n).

Wilson's Theorem

An integer $p > 1$ is prime if and only if:

\[ (p-1)! \equiv -1 \pmod{p} \]

\(Example: p = 5. (5-1)! = 4! = 24 \\equiv 24 - 25 = -1 (mod 5).\) βœ“

Useful for theoretical proofs and some competition problems about factorials mod primes.

Fast Exponentiation (Binary Exponentiation)

Compute $ a^n \pmod{m} $ in O(log n) multiplications instead of O(n).

Idea: Break the exponent into powers of 2.

Example: $ a^{13} = a^8 \times a^4 \times a^1 $ (since 13 = 8 + 4 + 1 = \(1101_{2})\)

Algorithm:

  1. result = 1, base = a mod m
  2. While exponent > 0:
    • \(If exponent is odd: result = (result \\times base) mod m\)
    • \(base = (base \\times base) mod m\)
    • exponent = exponent // 2

Chinese Remainder Theorem (Preview)

If moduli are pairwise coprime, a system of congruences has a unique solution modulo their product.

We'll cover this in detail in the CRT module.

⚠️ Common Mistake: Fermat's Little Theorem only works when the modulus is PRIME. For composite moduli, use Euler's theorem instead. And both require that a and the modulus are coprime β€” if they're not, you can't reduce the exponent this way. Also: Wilson's theorem is an if-and-only-if, so it can test primality (though it's not efficient for large numbers). Don't confuse Ο†(n) with d(n) (number of divisors) β€” they're different functions!
πŸ’‘ Key Insight: The "reduce the exponent" trick is superpowered. To compute a^b mod m when gcd(a,m)=1: find Ο†(m), compute b mod Ο†(m), then compute a^(b mod Ο†(m)) mod m. For prime modulus p, it's even easier: reduce mod (p-1). This turns "what's the last 3 digits of 7^2025?" from impossible to trivial. Combined with fast exponentiation, you can compute enormous modular powers in seconds.

✏️ Example Problems

πŸ“ Example 1 (Fermat's Little Theorem β€” big exponent)

What is $ 2^{100} \pmod{7} $?

7 is prime. Use Fermat's Little Theorem: $ a^{p-1} \equiv 1 \pmod{p} $

Step 1: p = 7, so p-1 = 6.

$ 2^6 \equiv 1 \pmod{7} $

Step 2: Reduce exponent 100 mod 6.

100 Γ· 6 = 16 remainder 4

\(100 \\equiv 4 (mod 6)\)

Step 3: $ 2^{100} \equiv 2^4 \pmod{7} $

$ 2^4 = 16 $

16 mod 7 = 16 - 14 = 2

Step 4: Verify with cycle method.

\((2^{1}=2, 2^{2}=4, 2^{3}=8 \\equiv\) 1 mod 7. Wait\) β€” cycle length is 3, not 6!

\(100 mod 3 = 1, so 2^{1}^{0}^{0} \\equiv 2^{1} = 2 mod 7. Same answer\) βœ“

(FLT gives an upper bound on cycle length; the actual cycle can be shorter.)

Answer: 2

πŸ“ Example 2 (Euler's totient function)

What is Ο†(36)? (How many numbers from 1 to 36 are coprime to 36?)

Step 1: Prime factorization of 36.

\(36 = 2^{2} \\times 3^{2}\)

Step 2: Apply Euler's totient formula.

\[ \varphi(n) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right)... \]

\[ \varphi(36) = 36 \left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right) \]

$ = 36 \times \frac{1}{2} \times \frac{2}{3} $

$ = 36 \times \frac{1}{3} = 12 $

Check: Numbers 1-36 coprime to 36 (not divisible by 2 or 3):

1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35 = 12 numbers. βœ“

Answer: 12

πŸ“ Example 3 (Modular inverse via FLT)

What is the inverse of 4 modulo 11? \((Find x such that 4x \\equiv 1 mod 11.)\)

11 is prime. Use Fermat's Little Theorem: inverse of a mod p = $ a^{p-2} \pmod{p} $

Method 1: FLT

Inverse of 4 mod 11 = $ 4^{11-2} = 4^9 \pmod{11} $

\((4^{2} = 16 \\equiv\) 5 mod 11\)

\((4^{4} = (4^{2})^{2} \\equiv 5^{2} = 25 \\equiv\) 3 mod 11\)

\((4^{8} = (4^{4})^{2} \\equiv 3^{2}\) = 9 mod 11\)

\((4^{9} = 4^{8} \\times 4^{1} \\equiv 9 \\times 4 = 36 \\equiv\) 3 mod 11\)

Inverse = 3

Method 2: Brute force (verify)

\(4 \\times 1=4, 4 \\times 2=8, 4 \\times 3=12 \\equiv 1 mod 11\) βœ“

Check: \(4 \\times 3 = 12 \\equiv 1 (mod 11).\) βœ“

Answer: 3

Previous
Next