In this lecture, we explore units in modular arithmetic, the concept of order, and a really cool result in elementary number theory: Fermat's Little Theorem.
Recall that $\mathbb{Z}_n = \{0, 1, 2, \ldots, n-1\}$ represents the integers modulo $n$. In this system, all arithmetic operations are performed modulo $n$.
In SageMath, we use Mod(a, n) to work with elements of $\mathbb{Z}_n$. This allows us to perform modular arithmetic easily.
Examples:
Again, SageMath can compute inverses.
The following lemma characterizes exactly which elements are units.
Proof:
Observe there exists a solution to the congruence equation $ax \equiv 1 \pmod{n}$ if and only if there is some integer $y$ such that $1 - ax = ny$ which happens if and only if there is a solution to the linear diophantine equation (LDE) $ax + ny = 1$. By previous work, we know this LDE has a solution if and only if $\gcd(a,n) = 1$.
□
Before we define order formally, let's explore what happens when we compute successive powers of an integer modulo $n$.
Notice that for $2^k \pmod{7}$, we eventually get back to $1$. This leads us to the concept of order.
Important Note: The order of $a$ modulo $n$ exists if and only if $a$ is a unit modulo $n$ (i.e., $\gcd(a, n) = 1$). If $a$ is not a unit, then $a^k$ can never equal $1$ modulo $n$ for any positive integer $k$. (Exercise: provide a proof of this!)
Examples:
When $n$ is a prime number, something special happens. Let's explore this pattern. We can compute $\text{ord}_n(a)$ in Sage as Mod(a,n).multiplicative_order(), but the following code does this by hand (it also uses power_mod, which is another option to compute powers in $\mathbb{Z}_n$).
Observation: When $p$ is prime, the order of every unit modulo $p$ divides $p - 1$. This is a special case of a more general theorem (Lagrange's theorem in group theory), but we can prove a key lemma that helps explain this pattern.
Proof:
Suppose $a^k \equiv 1 \pmod{n}$. By the division algorithm, we can write: $$k = qd + r$$ where $0 \leq r < d$.
Now, since $a^d \equiv 1 \pmod{n}$ (by definition of order), we have: $$a^k = a^{qd + r} = (a^d)^q \cdot a^r \equiv 1^q \cdot a^r \equiv a^r \pmod{n}$$ But we also know that $a^k \equiv 1 \pmod{n}$, so: $$a^r \equiv 1 \pmod{n}$$Since $d$ is the smallest positive integer such that $a^d \equiv 1 \pmod{n}$, and we have $0 \leq r < d$ with $a^r \equiv 1 \pmod{n}$, we must have $r = 0$.
Therefore, $k = qd$, which means $d \mid k$. □
The observations we made about orders modulo primes lead us to one of the most important theorems in number theory.
Remarks:
Indeed, $64 = 9 \cdot 7 + 1$, so $2^6 \equiv 1 \pmod{7}$.