This notebook introduces some of the finer aspects of modular arithmetic, including Euler's phi function, Euler's theorem, the Chinese Remainder Theorem, and efficient methods for computing large powers.
Recall that an element $a \in \mathbb{Z}_n$ is a unit if there exists $b \in \mathbb{Z}_n$ such that $ab \equiv 1 \pmod{n}$. We've seen that $a$ is a unit modulo $n$ if and only if $\gcd(a, n) = 1$.
A natural question arises: How many units are there modulo $n$?
This question leads us to one of the most important functions in number theory.
Examples:
For prime $p$: $\varphi(p) = p - 1$ since every number from 1 to $p-1$ is relatively prime to $p$.
For prime powers: $\varphi(p^k) = p^k - p^{k-1} = p^{k-1}(p - 1)$ since the only numbers not relatively prime to $p^k$ are the multiples of $p$.
One of the most useful properties of Euler's phi function is that it is multiplicative.
We will prove this later. For now, we can observe that it gives an efficient way to compute $\varphi(n)$ when we have a prime factorization of $n$: If $n = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}$ is the prime factorization of $n$, then:
$$\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) = p_1^{a_1-1}(p_1 - 1) \cdot p_2^{a_2-1}(p_2 - 1) \cdots p_k^{a_k-1}(p_k - 1)$$Example: $\varphi(12) = \varphi(2^2 \cdot 3) = \varphi(2^2) \cdot \varphi(3) = 2^1(2-1) \cdot (3-1) = 2 \cdot 2 = 4$
Exercise: Give an example to show that $\varphi(nm) \neq \varphi(n)\varphi(m)$ if $\gcd(n,m) \neq 1$.
Exercise: Give an example to show that $\varphi(nm) \neq \varphi(n)\varphi(m)$ if $\gcd(n,m) \neq 1$.
Euler's phi function leads us to a powerful generalization of Fermat's Little Theorem.
Remarks:
Often in number theory, we need to solve systems of congruence equations. (This will be the case when we want to prove the multiplicativity of Euler's phi function!) The Chinese Remainder Theorem provides a powerful method for doing so.
Solution by hand: From the first equation, $x = 5k + 2$ for some integer $k$.
Substituting into the second equation: $$5k + 2 \equiv 3 \pmod{7}$$ $$5k \equiv 1 \pmod{7}$$Since $5 \cdot 3 = 15 \equiv 1 \pmod{7}$, we have $k \equiv 3 \pmod{7}$, so $k = 7j + 3$.
Therefore: $$x = 5(7j + 3) + 2 = 35j + 17$$So $x \equiv 17 \pmod{35}$.
Proof:
We prove the theorem for $k = 2$; the general case follows by induction.
Let $n_1$ and $n_2$ be relatively prime, and consider: $$\begin{cases} x \equiv a_1 \pmod{n_1} \\ x \equiv a_2 \pmod{n_2} \end{cases}$$ Existence: Since $\gcd(n_1, n_2) = 1$, by Bézout's identity there exist integers $m_1, m_2$ such that: $$m_1 n_1 + m_2 n_2 = 1$$Define $x = a_1 m_2 n_2 + a_2 m_1 n_1$. Then:
Uniqueness: Suppose $x$ and $y$ are both solutions. Then:
Since $\gcd(n_1, n_2) = 1$, we have $n_1 n_2 \mid (x - y)$, thus $x \equiv y \pmod{n_1 n_2}$. □
Notice that the proof of the Chinese Remainder Theorem actually tells you an algorithm to solve simultaneous congruences: Given the congruences equations $$\begin{cases} x \equiv a \pmod{n} \\ x \equiv b \pmod{m} \\ \end{cases}$$ where $\gcd(m,n) = 1$ 1) First solve the linear diophantine equation $m\cdot z + n\cdot y = 1$ using the Euclidean algorithm. 2) Take $x = a \cdot mz + b \cdot ny$ for one solution. 3) Any other solution is $x + k\cdot \text{lcm}(m,n)$.
Proof: We now have all of the tools needed to sketch the proof of the multiplicativity of $\varphi$. Let $m, n > 2$ be positive integers that are relatively prime. By the Chinese Remainder Theorem, there exists an integer $e \in \mathbb{Z}$ such that $e \equiv 1 \pmod{n}$ and $e \equiv 0 \pmod{m}$. Similarly, there is an integer $f$ such that $e \equiv 0 \pmod{n}$ and $e \equiv 1 \pmod{m}$. Define a function $$ \psi \colon \mathbb{Z}_n \times \mathbb{Z}_m \to \mathbb{Z}_{nm}, \qquad \qquad \psi(a,b) = a\cdot e + b\cdot f \mod mn. $$
Claims: 1) $\psi$ is a bijection. Indeed, the inverse map sends $x \in \mathbb{Z}_{nm}$ to $(a \mod n, a \mod m)$. 2) $\psi(a,b)$ is a unit modulo $nm$ if and only if both $a$ is a unit modulo $n$ and $b$ is a unit modulo $m$.
Thus $$\varphi(mn) = \#\text{Units modulo }mn = (\#\text{Units modulo }n)\cdot(\#\text{Units modulo }m) = \varphi(n)\varphi(m).$$□
When computing large powers like $a^n \bmod m$, directly computing $a^n$ and then taking the remainder is inefficient for large $n$. Binary exponentiation (also called exponentiation by squaring) provides a much faster method.
We can compute this by:
This reduces the number of multiplications by quite a bit!
To compute $a^n \bmod m$:
Example: Compute $3^{13} \bmod 7$:
Try these exercises to practice with Euler's phi function, the Chinese Remainder Theorem, and binary exponentiation!
Run the cell below to generate a random problem, then compute $\varphi(n)$ in the next cell.
Run the cell below to generate a random system of congruences.
Run the cell below to generate a problem using Euler's theorem.