658 Appendix T
Having introduced modular arithmetic basics, I can describe two fundamental theorems
that have been derived by Fermat and Euler.
Two numbers are said to be relatively prime,orco-prime to each other, if they do not
divide each other, or if their greatest common divider (GCD) is unity. For instance, (3,4),
(2,9), and (5,12) are co-prime. Clearly, two prime numbers are co-prime. Primes and
co-primes play a central role in modular arithmetic, as the Fermat and Euler theorems
and applications illustrate.
Fermat’s theorem states that if m is prime and (a, m) are co-prime, then
a
m−1
mod m = 1, (T13)
which can be put into the equivalent form: a
m
mod m = a.Thus,wehavea mod 2 = 1
or a
2
mod 3 = 1 for any a that is prime with number two or number three, and so on
with any prime modulus, for instance 11
23−1
mod 23 = 1. We see that as a first benefit,
Fermat’s theorem makes it possible to considerably simplify calculations in modular
arithmetic.
Euler’s theorem (named after the Swiss mathematician) states that if p and q are two
primes and N = pq is their product, then we have for any integer a which is prime
with n:
a
(p−1)(q−1)
mod N = 1. (T14)
The factor ( p − 1)(q − 1) with pq = N is also called φ(N ). A consequence of Euler’s
theorem is that the inverse 1/a is readily defined as
1
a
= a
−1
= a
φ(n)−1
mod N . (T15)
For instance, what is the inverse of 5 modulo 21? We have 21 = 3 ×7, so φ(21) = (3 −
1) × (7 −1) = 12 and 1/5 = 5
12−1
mod 21 =48 828 125 mod 21 =17. Thus, |1/5|
21
=
17, which is readily verified as (17 ×5) mod 21 = 85 mod 21 = 1. We see that the
calculation of an inverse through Euler’s theorem is quite straightforward, compared
with the method previously described. But the requirement is that the modulus N is
defined by the product of two primes, which represents a special or fortuitous case.
It is important to note that if (a, N )arenot co-prime to each other, the inverse a
−1
does not exist. If the two are co-primes but Euler’s theorem cannot apply, the inverse a
−1
can be found using the extended Euclidian algorithm. Here, I shall explain this algorithm
through two representative examples: for instance, find the inverse of 7 in modulus 39,
and the inverse of 5 in modulus 16 272, i.e., the values of 7
−1
mod 39 and 5
−1
mod
16 272. Taking the first example, the algorithm proceeds as shown in Table T1, with the
steps as numbered in the first column.
In steps 1 and 2, we fill out the table as shown. We first divide 39 by 7, to find that
the integer part of the result is [39/7] = 5. In step 3, we multiply line 2 by 5, and in
step 4 we subtract the result of line 3 from that of line 1. This completes the first round.
Consider next the ratio 7/4, whose integer part is [7/4] = 1. In step 5, we multiply the
results of line 4 by 1, and in step 6, we subtract the result of line 5 from that of line 2.
This completes the second round. Consider next the ratio 4/3, whose integer part is