60 Modari
60 Modari
Modular Arithmetic
Klaus Sutner
Carnegie Mellon University
Fall 2022
1 Divisibility
2 Modular Arithmetic
3 Rotation
4 Chinese Remainder
Total Recall: Divisibility 2
Proposition
Note that ±1 | a and a | 0 for all a ∈ Z.
Divisibility is reflexive, transitive and almost antisymmetric.
Notation:
r = a mod b remainder
q = a div b quotient
Actual Algorithm 4
Suppose both a and b and given in binary, and have thousands of digits.
The data type used to store the bits is an array,
Lemma
For every n ≥ 2 there is a prime p such that p | n.
Lemma
If p is prime and p | ab then p | a or p | b.
Sample Proof 6
The last lemma is easily handled with a forward link to the greatest
common divisor, see below.
Theorem
Let n ≥ 2. Then there exist distinct primes p1 , . . . , pk such that
Proof.
Induction using the last lemma to split off prime factors. 2
Lemma
gcd(x, 0) = x
gcd(x, y) = gcd(y, x)
gcd(x, y) = gcd(y, x mod y)
Example 9
c=x·a+y·b
where x, y ∈ Z.
gcd(a, b) = x · a + y · b.
qi ri xi yi
− 1233 1 0
− 1000 0 1
1 233 1 −1
4 68 −4 5
3 29 13 −16
2 10 −30 37
2 9 73 −90
1 1 −103 127
9 0 1000 −1233
We have
a·x+b·y =c
We can divide by the GCD and use the extended Euclidean algorithm as
before. But note that the solution is not unique: for any solution (x0 , y0 )
we get infinitely many other solutions of the form
One can implement all the necessary arithmetic in O(k 2 ) steps for k-bit
numbers. In fact addition is only O(k), but for mods and remainders we
need O(k 2 ) steps.
But how often does the while-loop execute? Trivially no more than a ≥ b
times, but that’s no good at all.
Note that one must lose one bit at least at every other step. This follows
from
ri−2 = qi · ri−1 + ri
Hence total running time is O(k 3 ) steps for k-bit inputs.
Definition
Let p prime. The p-adic valuation of an integer n ̸= 0 is the largest e
such that pe divides n, in symbols νp (n); we set νp (0) = ∞.
Alas, the last formula does not yield an efficient way to compute gcds: we
have no good way to produce the prime decomposition of the numbers.
The Lattice View 16
absorption holds:
x ⊔ (x ⊓ y) = x x ⊓ (x ⊔ y) = x
Divisor Lattice 17
30
6 10 15
2 3 5
Exercise
Verify that ⟨N, lcm, gcd⟩ really forms lattice.
Exercise
How are lcm and gcd expressed in the picture of the divisor lattice of 30?
Exercise
How is the structure of prime divisors of 148176 = 24 33 73 expressed in
the picture of the divisor lattice?
1 Divisibility
2 Modular Arithmetic
3 Rotation
4 Chinese Remainder
Odds and Evens 21
Distinguishing between even and odd integers may sound trivial, but it is
often quite useful. Here is a simple example.
p(x) = a · x3 + b · x2 + c · x + d.
Claim
If both p(0) and p(1) are odd, then p(x) ̸= 0 for all integers x.
Here is a trick to prove this: it suffices to show that p(x) is always odd.
“Suffices” sounds weird, this is actually a stronger assertion.
Picture 22
10
-2 -1 1 2
-5
We write e for even, and o for odd. Here are the Cayley tables for
even/odd addition and multiplication:
+ e o · e o
e e o e e e
o o e o e o
p(e) = (a + b + c) · e + o = o
p(o) = (a + b + c)o + o = e · o + o = o.
x ≡m y ⇐⇒ m divides x − y
x=y (mod m)
a = b (mod m)
[x] + [y] = [x + y]
[x] · [y] = [x · y]
x = x′ , y = y ′ (mod m)
implies
x + y = x′ + y ′ (mod m)
′ ′
x·y =x ·y (mod m)
Let x = qm + r, x′ = q ′ m + r, y = pm + s, y ′ = p′ m + s. Then
x + y = (q + p)m + r + s x′ + y ′ = (q ′ + p′ )m + r + s
Note that this is a bit clumsy, we have to remainder twice (at least in
general).
All Congruences are Gaussian 32
are perfectly correct, but they get very tedious in actual use.
So, if it is clear from context what we mean, we may drop all these
decorations and just write x instead of [x]≡m . Similarly we just use +
and · for addition and multiplication of modular numbers.
Zm = ⟨{0, 1, . . . , m−1}, +, ·⟩
If you feel nervous about this initially, add brackets and subscripts to your
heart’s content.
Cayley Tables 34
Example (Z5 )
+ 0 1 2 3 4 · 0 1 2 3 4
0 0 1 2 3 4 0 0 0 0 0 0
1 1 2 3 4 0 1 0 1 2 3 4
2 2 3 4 0 1 2 0 2 4 1 3
3 3 4 0 1 2 3 0 3 1 4 2
4 4 0 1 2 3 4 0 4 3 2 1
Cayley Tables 35
Inevitable Clock Problem 36
multiply by 11:
Since there are only finitely many modular numbers one could, in
principle, use brute force. Alas, for even slightly large moduli this is not a
realistic option, we need some theory.
Proposition
Let ab = ac (mod m) and m′ = m/ gcd(a, m).
Then b = c (mod m′ ).
Exercise
Use p-adic valuations to prove the proposition.
Inhomogeneous Equations 39
Lemma
The equation
a·x=1 (mod m)
has a solution if, and only if, a and m are coprime.
If a solution exists it is unique modulo m.
Proof.
A solution means that ax − 1 = qm, so a and m must be coprime.
In the opposite direction use the extended Euclidean algorithm to
compute cofactors ax + my = 1.
2
Multiplicative Inverses 40
Example
m = 11.
x 1 2 3 4 5 6 7 8 9 10
x−1 1 6 4 3 9 2 8 7 5 10
Note that 10 = 10−1 (no surprise, really: 10 = −1).
So 1/2 = 6 (mod 11).
Euler’s Totient Function 41
Z⋆m = { a ∈ Zm | gcd(a, m) = 1 }
1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 18, 8, 12, 10, 22, 8, 20, 12, 18,
12, 28, 8, 30, 16, 20, 16, 24, 12, 36, 18, 24, 16, 40, 12, 42, 20, 24, 22, 46, 16, 42, . . .
Looks complicated.
It is certainly far from clear what the next value would be.
Plot 43
400
300
200
100
Obviously we can compute φ(n) by brute force, but that’s a white lie:
what if n has 1000 digits?
Lemma
In the general case
a · x = c (mod m)
we have a solution if, and only if, gcd(a, m) divides c.
Moreover, the number of solutions is gcd(a, m).
Exercise
Prove the general case.
Modular Arithmetic and Primes 47
Z⋆p = {1, 2, . . . , p − 1}
Proof.
First assume p is prime, wlog p > 2. We can pair off a ∈ Z⋆p and
a−1 ∈ Z⋆p .
a and a−1 are always distinct except in the case a = ±1: the quadratic
equation x2 = 1 (mod p) has at most two solutions since
x2 − 1 = (x + 1)(x − 1).
Question:
Why can’t we use Wilson’s theorem for a fast primality test?
Little Fermat 50
Proof.
a : Z⋆p → Z⋆p , b
Consider the map b a(x) = ax.
a is a bijection, so
b
Y Y Y Y
−ap−1 = ap−1 x= ax = a(x) =
b x = −1 (mod p)
x∈Z⋆
p x∈Z⋆
p x∈Z⋆
p x∈Z⋆
p
2 Modular Arithmetic
3 Rotation
4 Chinese Remainder
Application: Rotating Arrays 52
Problem: Rotation
Instance: An array A, a positive integer s.
Solution: Rotate A by s places.
Can we do better?
Forcing Constant Space 53
It is clear that we can get liner time/constant space if we rotate the array
by only one place: just remember a0 and move everyone over.
The last method is linear time and constant space. But we can still
quibble: it moves every element in the array twice.
Could we move the elements only once? After all, we know exactly where
they are supposed to go.
Something like
a0 ← as ← a2s ← a3s ← . . .
a sequence of displacements (we have to remember a0 which gets
clobbered right away).
Could this possibly work? If so, exactly how? What does . . . actually
mean?
Iterating Addition 56
α : Zm −→ Zm
x 7−→ x + s mod m
look like?
Example 57
0, 11, 2, 13, 4, 15, 6, 17, 8, 19, 10, 1, 12, 3, 14, 5, 16, 7, 18, 9
General Case 58
Moreover, since α(x) + y = α(x + y) (mod m) all the cycles are just
rotations of each other and it suffices to understand the single orbit
orb(0, α).
Proposition
α has gcd(s, m) distinct orbits, each of length m/ gcd(s, m).
And Rotation? 59
This means that we can concoct an algorithm that uses two nested loops:
Each array element is moved only once, and into its final position.
Exercise
Implement this rotation method. Compare to the reversal method.
1 Divisibility
2 Modular Arithmetic
3 Rotation
4 Chinese Remainder
Multiple Linear Equations 61
ai x = bi (mod mi ) where i = 1, . . . , n
We know how to simplify this system a little bit: for a solution to exist
we need that gcd(ai , mi ) divides bi .
So we get equivalent equations a′i x = b′i (mod m′i ) where a′i and m′i are
coprime.
x = ai (mod mi ) i = 1, . . . , n
f : Zm −→ Zm1 × Zm2
f (x) = (x mod m1 , x mod m2 )
Claim
f is injective and hence bijective.
Proof 63
x′ − x = q1 m1 = q2 m2 .
x = f −1 (a, b)
0 (0, 0) 8 (2, 3)
1 (1, 1) 9 (0, 4)
2 (2, 2) 10 (1, 0)
3 (0, 3) 11 (2, 1)
4 (1, 4) 12 (0, 2)
5 (2, 0) 13 (1, 3)
6 (0, 1) 14 (2, 4)
7 (1, 2)
αm1 + βm2 = 1
Then
f (αm1 ) = (0, 1)
f (βm2 ) = (1, 0)
whence
x = 2 mod 3 x = 1 mod 5
is x = 11.
(−3) · 3 + 2 · 5 = 1
x = 1 · (−3) · 3 + 2 · 2 · 5 = 11
Chinese Remainder Theorem 67
Our result also holds for more than 2 equations (and is very old).
Theorem (CRT)
Let mi , i = 1, . . . , n be pairwise coprime. Then the equations
x = ai (mod mi ) i = 1, . . . , n
How do we compute the solution for n > 2? We could use the method
for n = 2 recursively, but that is a bit tedious. Here is a better way.
Define
ci = m/mi
so that ci = 0 (mod mj ), i ̸= j, but ci and mi are coprime. Use EEA to
find inverses
αi ci = 1 (mod mi )
Then
x = a1 α1 c1 + a2 α2 c2 + . . . an αn cn (mod m)
A Generalization 69
In general, a solution may exist even if some of the moduli are not
coprime. This is expressed in the following generalization† .
x = ai (mod mi ) i = 1, . . . , n
ai = aj (mod gcd(mi , mj ))
† This
is the kind of result that you might want to be aware of, but there is no
need memorizing it in detail.
Application: Large Numbers 70
Suppose you have a 64-bit architecture, but you need to compute with
100-bit numbers.
The computation can involve many steps, we always keep our numbers in
two-component form, each using 64 bits. Only in the end will we convert
back to a single 100-bit number.
And the Primes? 71
There is an old result by Chebyshev that provides a lower bound for the
number of k-digit primes (primes in [2k−1 , 2k − 1]).
7 2k − 1 9 2k−1 − 1
−
8 ln (2k − 1) 8 ln (2k−1 − 1)
For example, for k = 100 we get at least 5.61 × 1027 primes. The length
of the interval is about 6.34 × 1029 .