20161011
More Modular Arithmetic
It’s Tuesday, so we will explore
Recall that x y = (x + y) mod n
with respect to
for some n 2)
and
(as promised).
(remember, we are defining the arithmetic operations
It would make sense to define as x y = (x – y) mod n and in fact that is exactly where
we are going to end up. But we are going to take a slightly round-about route because that
will help us when we define (modular division)
In normal arithmetic, when we write a – b = x, we understand that this is equivalent to
writing a = b + x. So when we want to figure out the value of x in the equation a b = x, it
makes sense to say x is the element of
such that a = b x
We need to do two things: show that such an x always exists, and that it is unique.
Proof of existence: Let a and b be any two elements of
Let x = (a – b) mod n. Clearly x
x = (a – b) + k*n for some integer k
b
x = (b + (a – b) + k*n) mod n
= (a + k*n) mod n
=a
Thus x = (a – b) mod n satisfies b
a
x = a, and it makes sense to say
b = (a – b) mod n
But is it unique? Could there be another value in
, say y, that also satisfies b
y = a?
Suppose
b
x=a
and
b
y=a
So (b + x) mod n = a
and
(b + y) mod n = a
So b + x = a + k*n
and
b + y = a + j*n
for some k and j
Subtracting one of these equations from the other gives
x – y = (k – j)*n
which means x
y (mod n)
But since both x and y are in
this means x = y. So there cannot be two different elements
of
that both satisfy b x = a
Note that this allows us to determine the value of –a for any a
-a = 0 – a = the unique element x
It is not hard to see that -a = (n – a) mod n
such that a
x=0
Modular Division
Recall that one of our goals in defining arithmetic on
was closure – when we apply the arithmetic
operator to two elements of , the result should also be in .
For addition, multiplication and subtraction, that was easy. For division, not so much. But we will see
what we can do.
When we write x = a/b, we understand that it is equivalent to x = a *
which replaces the division
operator by a multiplication operator – notice that this is the same trick that we pulled when we defined
modular subtraction by turning it into modular addition. The problem is that now we are stuck with
that
Given x
... a term that we call the reciprocal of b, and which we often write as
, what value y
should be chosen as
for all x except for 0.
?
Recall that in “normal” arithmetic,
So it seems reasonable that in modular arithmetic, we should let
satisfies x y = 1
be the element y
that
Unfortunately, it turns out that in many of the sets
there are elements other than 0 that have no
reciprocals. (Note that 0 never has a reciprocal) For example, consider for
0
1
2
3
4
5
0
0
0
0
0
0
0
1
0
1
2
3
4
5
2
0
2
4
0
2
4
3
0
3
0
3
0
3
4
0
4
2
0
4
2
5
0
5
4
3
2
1
The only elements of
that have reciprocals are 1 and 5. This is not an accident ... these are the only
elements of
that are relatively prime with 6.
<Recall: two integers are relatively prime if their greatest common divisor is 1. So 2 and 6 are not
relatively prime because gcd(2,6) = 2 >
Claim: In
, element x has a reciprocal if and only if x and n are relatively prime (i.e. gcd(x,n) = 1)
Proof: Suppose x has a reciprocal, ie there is an element y
=> (x * y) mod n = 1
=> x*y + k*n = 1
for some integer k
=> gcd(x,n) = 1
(see footnote)1
=> x and n are relatively prime
such that x
y = 1.
Suppose x and n are relatively prime, ie gcd(x,n) = 1
=> there exist integers a and b such that x*a +n*b = 1 (see the same footnote)
=> we can write a as k*n + r where r
(r is just a mod n)
=> x*(k*n + r) + n*b = 1
=> x*r + n*(k*x + b) = 1
Now take both sides mod n ... the term n*(k*x + b) disappears because it is a multiple of n, and
we are left with
x*r (mod n) = 1
i.e.
x
r=1
i.e.
x has a reciprocal
This innocent seeming result is actually hugely important. It lets us put the whole
But first, a couple of results that follow immediately from this one:
Claim: In
, if x has a reciprocal then the reciprocal is unique.
Proof: Easy. Assume x
a=1
Claim: Every element of
and x
If gcd(x,n) = 1
b = 1. Show a = b
except 0 has a reciprocal if and only if n is prime
Proof: If n is prime, then gcd(x,n) = 1
x
x
{1 ... n-1}
{1 ... n-1}, then n is prime
Definition: If x has a reciprocal, we say that x is invertible. We use
x.
1
problem away.
to represent the reciprocal of
There is a theorem that tells us that if a, b, c and d are integers such that a*b + c*d = 1, then gcd(a,c) = 1, gcd(a,d) = 1,
gcd(b,c) = 1, and gcd(b,d) = 1 (and vice versa). This result is usually presented during the proof of Euclid’s
Algorithm for finding the gcd() of two integers.
To illustrate that last claim about primes, let’s look at the
table for
0
1
2
3
4
5
6
0
0
0
0
0
0
0
0
1
0
1
2
3
4
5
6
2
0
2
4
6
1
3
5
3
0
3
6
2
5
1
4
4
0
4
1
5
2
6
3
5
0
5
3
1
6
4
2
6
0
6
5
4
3
2
1
I’ve highlighted the 1’s in the table to show that each non-zero element does have an unique reciprocal.
From this table we can see that in
, etc
Claim: if
= b, then
=a
Proof: exercise
Now, finally, we can define
In
:
, let y be an invertible element, and let x be any element. Then
If we think of
as
, it is clear that this is exactly parallel to the observation we made about
“normal” division: x / y = x *
Example: in
, what is 5
2? Answer: It is undefined, since 2 is not invertible in
Example: in
, what is 5
2? Answer:
= 4, so 5
2=5
4=6
Now this may seem a bit weird ... we are saying that 5 divided by 2 is equal to 6. What kind of sense
does that make?
Well, remember that in “normal” math, saying “x / y = z” is the same as saying “y * z = x”
In
, we see that 2
6 = 5, so it is reasonable to say 5
2=6
Example: in
, what is 2
5? Answer:
= 5, so 2
5=2
Again, we can confirm that this makes sense because 5
5=4
4=2
Ok, that’s all lots of fun. We’ve shown that we can do arithmetic in a meaningful way on finite sets.
But is it useful? It turns out that the ideas we have explored here are vital to modern cryptography,
which means they are vital to e-commerce. In a very real sense, companies like Amazon could not
exist without modular arithmetic.
Let’s look at a little example of a problem that becomes trivially easy to solve when we understand
modular arithmetic:
Problem: Find all values x such that 4 * x (mod 11) = 2
Solution:
4 * x (mod 11) = 2 is equivalent to 4
x = 2 in
Note that 4 is invertible in
Multiplying both sides by
But
we get
so this reduces to
Now in
,
So x = 3
2=6
=3
(How did I know that? 4*3 = 12, and 12 (mod 11) = 1)
But note that if x is a solution to 4*x (mod 11) = 2, then so is x + 11, x + 22, and in
general so is x + 11*k
So our final answer is:
all integers of the form 6 + 11*k, where k is an integer
In that example, finding
in
was easy enough to do in my head. But what if the question were
“Find all solutions to 82*x (mod 100019) = 322” It turns out that 100019 is prime (I simply looked it
up) so we know that
exists in
, but how do we find it?
The answer to this question is a bit outside the scope of this course at this point, but if you are
interested you will find a good description of different methods at
https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
© Copyright 2026 Paperzz