7.1.2 Modular Arithmetic
The domain of integer numbers introduced in Chapter Numbers
has infinite size, i.e., there is no upper bound for an integer. However,
computer processors can only operate with numbers of a maximum size determined
by the length of a computer word on the corresponding architecture, e.g., a 64
bit architecture only supports 264 different numbers.
Apparently the question arises how to define the result of an operation whose
value is outside the range that can be represented by such a word, e.g., the
result of 240*230. While we may consider such an operation "illegal"
(and raise a hardware interrupt), there is a more elegant solution provided by
modular arithmetic: we define the result of every operation as the
result of the unbounded integer operation modulo the number m of
elements that can be represented by a computer word.
We start with a preliminary definition of modular arithmetic, which will later
be replaced by a more suitable one.
Definition 104 (Modular Arithmetic, Direct Approach)
Let m in Z>0 and Zm := {x in Z: 0 <= x < m}.
+m: Z x Z -> Zm |
x +m y := (x + y) mod m
|
|
-m: Z x Z -> Zm |
x -m y := (x - y) mod m
|
|
-m: Z x Z -> Zm |
-m x := (-x) mod m
|
|
*m: Z x Z -> Zm |
x *m y := (x * y) mod m
|
Example
These tables describe arithmetic
modulo 3 for a subset of Z:
+3 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 |
-4 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 |
-3 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 |
-2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 |
-1 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 |
0 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 |
1 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 |
2 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 |
3 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 |
4 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 |
|
*3 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 |
-4 | 1 | 0 | 2 | 1 | 0 | 2 | 1 | 0 | 2 |
-3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
-2 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 |
-1 | 1 | 0 | 2 | 1 | 0 | 2 | 1 | 0 | 2 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 |
2 | 1 | 0 | 2 | 1 | 0 | 2 | 1 | 0 | 2 |
3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 |
|
In above definition, the arguments may be arbitrary integers while the result
is one of m values. However, a little investigation of the function
tables reveals that the same pattern of result values is repeated every
m lines and every m columns. Consequently, it does not matter
whether we compute with an argument
a or with a+3 or with a-3 or with
a+3i for any i in Z. We may therefore define
the set of all values
[a]m := {a+im: i in Z}
that cannot be distinguished from a by modular arithmetic modulo
m. However, this definition is just a special case of the construction
of equivalence classes, as we are going to elaborate now. This construction
will provide us with a suitable domain for modular arithmetic.
First we define the relation that links two integers that "are the same"
with respect to arithmetic modulo m.
Definition 105 (Modular Congruence)
Two integers x and y are congruent (kongruent) modulo
m if they have the same remainder when divided by m:
x = m y : <=> (x mod m)
= (y mod m).
Clearly = m is an equivalence relation, for every m
in Z>0. The equivalence classes induced by this relation collect
all the integers that cannot be distinguished by
arithmetic modulo m.
Definition 106 (Residue Class)
The residue class (Restklasse) of a modulo m is the set
of all integer numbers that are congruent to a modulo m.
It is easy to see that [a]m =
{a+im: i in Z} as was our original
intuition. However, the new construction has the advantage that we may
construct the quotient set of Z with respect to = m, which
gives a suitable domain for modular arithmetic.
Definition 107 (Modular Integer Numbers)
The set of integers modulo m is the quotient set of Z with respect
to congruence modulo m:
Zm := Z/ = m.
Zm has m elements each of which
is represented by a natural number less than m, i.e.,
Zm = {[0]m, [1]m, [2]m, ...,
[m-1]m}.
|
Before we are going to define the arithmetic operations on this domain,
we emphasize an important property of our construction.
Proposition 122 (Congruence Properties)
Let a and a' be two integers that are in the same residue
class and b and b' be two integers that are also in the same
residue class. Then the result of any integer operation involving a and
b is in the same residue class as if the operation
were performed with a' and b' instead:
forall m in Z>0,
a in Z, a' in Z,
b in Z, b' in Z: |
[a]m = [a']m /\ [b]m = [b']m => |
[a+b]m = [a'+b']m
/\ |
[a-b]m = [a'-b']m
/\ |
[-a]m = [-a']m /\ |
[a*b]m = [a'*b']m.
|
Proof
Take arbitrary m in Z>0,
a in Z, a' in Z,
b in Z, b' in Z such that
[a]m = [a']m and
[b]m = [b']m. We show
We know that there exist quotients x, y, z, w
and remainders r, s such that
a = xm+r, a' =
ym+r, |
b = zm+s, b' =
wm+s, |
|
which implies
[a+b]m | = | |
[(xm+r)+(ym+s)]m | = |
|
[(x+y)m+(r+s)]m | = | (*) |
[(r+s)]m | = | |
[(z+w)m+(r+s)]m | = | (*) |
[(zm+r)+(wm+s)]m | = | |
[a'+b']m.
|
(*) It remains to be shown that
forall m in N>0, x in Z, y in Z: [xm+y]m = [y]m.
|
Example
We have [7]5 = [2]5 and [9]5 = [4]5. Consequently,
[7+9]5 = [2+4]5 = [6]5 = [1]5.
The importance of the congruence properties is that we may choose any
representative of a value of Zm in order to compute with
that value, e.g., when dealing with
[3]5 = {..., -7, -2, 3, 8, 13, ...}
we need not take the "canonical" representative 3 but may also choose -2 or
8 as the representative for computation. Consequently, the following
definition determines the function results uniquely.
Definition 108 (Modular Arithmetic, Residue Classes)
Let m in Z>0 and define the selector function
x := such a in Z: x =
[a]m.
|
We then define the operations of modular arithmetic (modulare
Arithmetik):
+m: Zm x Zm -> Zm
|
x +m y :=
[x+Zy]m |
(or: [a]m +m [b]m : =
[a+Zb]m) |
|
-m: Zm x Zm -> Zm
|
x -m y :=
[x-Zy]m |
(or: [a]m -m [b]m : =
[a-Zb]m) |
|
-m: Zm -> Zm |
-m x := [-Z x]m |
(or: -m [a]m : =
[-Z a]m) |
|
*m: Zm x Zm -> Zm |
x *m y :=
[x*Z y]m |
(or: [a]m *m [b]m : =
[a*Z b]m)
|
For performing arithmetic on some x in Zm,
- we apply
the selector function to determine a representative x
in Z,
- perform the corresponding operation in Z to yield
the result r in Z,
- and then determine the residue class
[r]m in Zm.
Example
We consider arithmetic in Z5 = {[0]5, [1]5, [2]5, [3]5, [4]5}.
[17]5+5[24]5 | = | [2]5 +5 [4]5 = [6]5 = [1]5 |
[7]5-5[10]5 | = | [2]5 -5 [0]5 = [3]5 |
[-7]5 | = | [3]5 |
[6]5*5[9]5 | = | [1]5*5[4]5 = [4]5 |
[-3]5*5[6]5 | = | [2]5*5[1]5 = [2]5
|
Logic Evaluator
We can implement modular arithmetic by equivalence classes as shown
below,
see Appendix modular.txt
: the
result of a modular operation is an equivalence class of integers
<x, y> whose difference x-y denotes the
corresponding value.
Author: Wolfgang Schreiner
Last Modification: October 4, 1999