Top Banner
Modular Arithmetic for FLSAM Alex Yu
23

Mods

Jan 27, 2016

Download

Documents

Alex Yu

Introduction to Modular Arithmetic
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mods

Modular Arithmetic for FLSAM

Alex Yu

Page 2: Mods

Contents

1 Congruences and Residue Classes 2

1.1 Modular Congruences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.1.1 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.1.2 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.2 Residue Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.2.1 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.2.2 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.3 Inverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.3.1 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.3.2 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2 Standard Theorems and Their Applications 15

2.1 Fermat’s Little Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.1.1 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2 Chinese Remainder Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.2.1 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.3 Euler’s Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

1

Page 3: Mods

Chapter 1

Congruences and Residue Classes

1.1 Modular Congruences

First off, what is a modular congruence?

Definition 1: Modular Congruence.If a, b are integers, and m is a positive integer, then we say thata is congruent to b modulo m if m divides a − b; that is, if a, bhave the same remainder modulo m. This is generally denoted asa ≡ b (mod m). The previous equation is denoted as a modularcongruence.

In case one hasn’t made the connection yet, the fact that m|a − b directly implies that a, bhave the same remainder upon division by m. To see why, let a = k1m + r1, b = k2m + r2, where0 ≤ r1, r2 ≤ m−1. Then a− b = m(k1−k2)+(r1−r2). Then clearly, m|a− b if and only if r1 = r2,and so we’re done.

Therefore, if a ≡ b (mod m), then a, b have the same remainder. Alternatively, the above proofcould have been shortened simply by subtracting b over, going from a ≡ b (mod m) to a − b ≡ 0(mod m). The fact that a ≡ b (mod m) if and only if a and b have the same remainder upondivision by m suggests the following properties (listed here without proof; they are easy to prove,simply by considering the fact that m|a − b or that a − b = km where k is an integer if a ≡ bmod m, or by letting a = km+ r):

• a ≡ a mod m

• If a ≡ b mod m, then b ≡ a mod m.

• If a ≡ b mod m, and b ≡ c mod m, then a ≡ c mod m.

• If ai ≡ bi mod m for all i where 1 ≤ i ≤ n, then a1 +a2 +a3 + · · ·+an ≡ b1 +b2 +b3 + · · ·+bnmod m.

2

Page 4: Mods

FLSAM Handout: Modular Arithmetic :O

• If a+ b ≡ c mod m, then a ≡ c− b mod m.

• If a ≡ b mod m, then a+ c ≡ b+ c mod m.

• If ai ≡ bi mod m, for all i where 1 ≤ i ≤ n, then a1a2 · · · an ≡ b1b2 · · · bn mod m.

• If a ≡ b mod m, then ac ≡ bc mod m (you should remember that this does not hold theother way around; that is, if ac ≡ bc mod m, it is not always true that a ≡ b mod m! We’lldiscuss why later.)

Now let us consider the proofs of some slightly more advanced properties:

Property 1: Exponential Congruence.If a ≡ b mod m, then an ≡ bn mod m for all positive integersn.

Proof. Let a = k1m+r, b = k2m+r. Then we wish to prove that (k1m+r)n ≡ (k2m+r)n mod m.

By the Binomial Theorem, the LHS (left-hand side) can be written as

(k1m+ r)n = (k1m)n +

(n

1

)(k1m)n−1r +

(n

2

)(k1m)n−2r2 + · · ·+

(n

n− 1

)(k1m)rn−1 + rn

Clearly, every term in the expansion except for the last (i.e. rn) is divisible by m, so we havethat (k1m + r)n ≡ rn. If we write b out the same way as we did for a and expand, we reach thesame conclusion; that is, that an ≡ bn ≡ rn mod m, and we are done.

Property 2: Polynomial Congruence.If a ≡ b mod m and f(x) is a polynomial with integer coeffi-cients, then f(a) ≡ f(b) mod m.

Proof. To make our lives easier, we’re going to split the polynomial by term: we’re going to letf(x) = anx

n + an−1xn−1 + · · ·+ a1x+ a0.

Using the previous property we just proved, we would like to pair up terms of f(a) and f(b),and show that each individual xk term in f(a) is congruent to its correponding term in f(b).

Indeed, we may do this. For any term akxk, note that

a ≡ b mod m =⇒ ak ≡ bk mod m =⇒ akak ≡ akbk mod m

Since this is true for every akxk term from k = 0 to k = n, we sum up the congruences:

anan ≡ anbn mod m

an−1an−1 ≡ an−1bn−1 mod m

Page 5: Mods

FLSAM Handout: Modular Arithmetic :O

...

a1a ≡ a1b mod m

a0(a)0 ≡ a0(b)0 mod m

Summing up these n+ 1 congruences gives us that f(a) ≡ f(b) mod m, as desired.

It should also be noted that a−b|f(a)−f(b); this can also be easily seen by expanding f(a)−f(b)as

f(a)− f(b) = an (an − bn) + an−1(an−1 − bn−1

)+ · · ·+ a1(a− b),

which is clearly divisible by a− b.

——————————–

Example 1:Prove that 270 + 370 ≡ 0 mod 13.

Solution 1. Note that 270 + 370 =(22)35

+(32)35

=(22 + 32

) (268 − 26632 + · · ·

). Thus 270 +

370 =(22 + 32

)N = 13N ≡ 0 mod 13, as desired (N is some large positive integer that we don’t

need to calculate).

Solution 2. Since 26 ≡ −1 mod 13, we have 270 ≡(26)11 · 24 ≡ (−1)11 · 3 ≡ −3. Also, note

that 33 ≡ 1 mod 13, so 370 ≡(33)23 · 3 ≡ 1 · 3 ≡ 3 mod 13. So 270 + 370 ≡ −3 + 3 ≡ 0 mod 13,

as desired.

——————————–

——————————–

Example 2:Calculate the remainder of 7100 upon division by 100.

Solution. Note that 74 = 2401, so 74 ≡ 1 mod 100. Then 7100 ≡(74)25 ≡ 125 ≡ 1 mod 100.

Therefore 7100 has a remainder of 1 upon division by 100.

——————————–

——————————–

Example 3:Verify the congruence

(30731 − 41

)10 ≡ 24 mod 100.

Solution. Note that 307 ≡ 7 mod 100, so we may use Property 1 to get 30731 ≡ 731 mod 100.But 74 ≡ 1 mod 100, so 731 ≡

(74)7 · 73 ≡ 73 ≡ 43 mod 100. So the expression reduces to

(43− 41)10 = 210 ≡ 24 mod 100, as desired.

Page 6: Mods

FLSAM Handout: Modular Arithmetic :O

——————————–

Anyways, these are pretty standard problems; ones like these, you generally find something thatends up cancelling out to make a lot of 1’s or 0’s. Sometimes it won’t be easy; but you’ll learn toolsthat will make your life a lot easier later in this handout. However, currently we’re going to moveon to some more properties. Recall how I said that the statement ”If ac ≡ bc mod m, then it isnot necessarily true that a ≡ b mod m”. Well, we are going to explore a little bit more with thisstatement:

Property 3: Division in modulo m.

If ac ≡ bc mod m, then a ≡ b mod(

mgcd(m,k)

).

Proof. Let gcd(m, k) = d, and also m = d ·m1, k = d · d1, where gcd(m1, k1) = 1. Now note that

m|k(a− b), thus the fraction k(a−b)m is an integer. Substituting yields

k(a− b)m

=dk1(a− b)m1d

=k1(a− b)m1

But sincem1 and k1 do not share any common divisors, it is clear thatm1 must completely divide(a− b) instead, so a ≡ b mod m1. But m1 = m

d = mgcd(m,k) , so we’ve reached our conclusion.

So then, if ac ≡ bc mod m, when can we assume that a ≡ b mod m? If gcd(c,m) is 1! If thisis the case, then plugging in this value into the property yields a ≡ b mod m

1 , which is exactlya ≡ b mod m. So one needs to be wary when performing division modulo some number m; it canget tricky.

Property 4: Congruences modulo the LCM.a ≡ b mod mi for integer i, 1 ≤ i ≤ n if and only if a ≡ bmod (lcm(m1,m2, · · · ,mn)).

Proof. Note that we must have a − b be a multiple of m1,m2, etc., therefore, each of mi mustdivide a − b. But the smallest number such that m1,m2, · · · ,mn all divide it is defined to belcm(m1,m2, · · · ,mn). Therefore a− b must be an integer multiple of lcm(m1,m2, · · · ,mn), whichgives us the desired conclusion. To prove the converse, note that if lcm(m1,m2, · · · ,mn)|a−b, thenmi|a− b, since mi|lcm(m1,m2, · · · ,mn), which gives the desired conclusion, and we’re done.

——————————–

Example 4:Show that if a ≡ b mod 2, 5, 6, then a ≡ b mod 30. Also, show that if a ≡ b mod 30, then a ≡ bmod 15.

Solution. The first part follows directly from the previous property, as the LCM of (2, 5, 6) isprecisely 30. For the second part, note that since 30|a− b, and 15|30, the conclusion follows. (thisis the basic argument we used to prove the converse direction of the previous property)

Page 7: Mods

FLSAM Handout: Modular Arithmetic :O

——————————–

——————————–

Example 5:We know that 14 ≡ 32 mod 18, can we just divide by 2?

Solution. No! If we do that, we’ll get that 7 ≡ 16 mod 18, clearly false. We have to useProperty 3. Since gcd(14, 32) = 2, we obtain that 14 ≡ 32 mod 18 =⇒ 7 ≡ 16 mod 18

2 =⇒ 7 ≡ 16mod 9, which is true. Division can be a little tricky when working with mods.

——————————–

1.1.1 Recap

Two number are congruent modulo m if they have the same remainder upon division by m, and isdenoted as a ≡ b mod m. This implies that m|a− b.

Here’s a full list of all properties covered so far:

• a ≡ a mod m

• If a ≡ b mod m, then b ≡ a mod m.

• If a ≡ b mod m, and b ≡ c mod m, then a ≡ c mod m.

• If ai ≡ bi mod m for all i where 1 ≤ i ≤ n, then a1 +a2 +a3 + · · ·+an ≡ b1 +b2 +b3 + · · ·+bnmod m.

• If a+ b ≡ c mod m, then a ≡ c− b mod m.

• If a ≡ b mod m, then a+ c ≡ b+ c mod m.

• If ai ≡ bi mod m, for all i where 1 ≤ i ≤ n, then a1a2 · · · an ≡ b1b2 · · · bn mod m.

• If a ≡ b mod m, then ac ≡ bc mod m

• If a ≡ b mod m, then an ≡ bn mod m (keep in the mind the reverse does not always hold!For example, 34 ≡ 74 ≡ 1 mod 16, but 3 ≡ 7 mod 16 is false!).

• If a ≡ b mod m and f(x) is a polynomial with integer coefficients, then f(a) ≡ f(b) mod m.

• If ac ≡ bc mod m, then a ≡ b mod(

mgcd(m,c)

).

• a ≡ b mod mi for 1 ≤ i ≤ n if and only if a ≡ b mod (lcm(m1,m2, · · · ,mn)).

In case this large list of properties seems a bit overwhelming, keep in mind that most of thebasic properties of arithmetic are preserved. For example, addition, subtraction, and multiplicationstay pretty much the same; the only thing that’s weird is division.

Page 8: Mods

FLSAM Handout: Modular Arithmetic :O

1.1.2 Review Problems

1. Prove that for integers x, y and prime p, that (x+y)p ≡ xp+yp mod p. (Hint: Use BinomialTheorem. Can you prove that if 1 ≤ k ≤ p− 1, then p divides

(pk

)?)

2. If p is a prime, and 0 ≤ k ≤ p− 1 is an integer, then(p−1k

)≡ (−1)k mod p. (Hint: Expand(

p−1k

)into products. Remember that p− k ≡ −k mod p.)

3. Show that10n + 3 · 4n + 5

9is an integer if n is a positive integer. (Hint: Use mods, and

Property 3)

4. If n is a positive integer, show that the fraction8n − 4 · 6n + 2

7is an integer if and only if n

is odd. (Hint: Do casework.)

5. Show that there exists no polynomial f(x) with integer coefficients such that f(f(3)) =52, f(3) = 17. (Hint: 14)

Page 9: Mods

FLSAM Handout: Modular Arithmetic :O

1.2 Residue Classes

Definition 2: Residue Class.If a, b have the same remainder upon division by m, then a ≡b mod m. Then we say that a, b belong to the same residueclass. Congruence in modulo m splits the integers intom disjoint(non-intersecting) residue classes. They are commonly writtenas a + mZ for a = 0, 1, . . . ,m − 1. They can also be denoted asa+mZ = {a+mk|k ∈ Z}.

For example, 7, 11, 27 belong to the same residue class modulo 4 because all three of them have aremainder of 3 upon division by 4. Therefore, they belong to the residue class 3 + 4Z. In fact, thisspecific residue class is the set {. . . ,−5,−1, 3, 7, 11, 15, . . .}.

A set A of integers is called a complete set of residue classes (or a complete residue set) if forevery residue class, there is at least one element of A that belongs to it. In simpler terms, for each0 ≤ k ≤ m− 1 there is some n belonging the set A such that n ≡ k mod m. For example, any setof m consecutive integers is a complete residue set modulo m, and any set of k consecutive integersis a complete residue set if 1 ≤ m ≤ k.

——————————–

Example 6:Let a,m be positive integers. Then the set A = {1 · a, 2 · a, · · · ,m · a} is a complete residue set ifand only if gcd(a,m) = 1.

Solution. Assume that gcd(a,m) = 1. For sake of contradiction, let us assume that the set A isnot a complete residue set. Then there are two elements of A that have the same remainder modulom; that is to say, for some i, j where 1 ≤ i, j ≤ m, i 6= j, ia ≡ ja mod m. But since gcd(a,m) = 1,this implies that i ≡ j mod m, contradiction, as i 6= j, and both i, j are between 1 and m. So Ais therefore a complete set, as desired.

To prove the other direction, assume that A is a complete set. For sake of contradiction, assumethat gcd(a,m) 6= 1. Then let this value be equal to k, so that m = k ·m1, a = k · a1. Since m1 < mby our assumption, m1a ∈ A. But note that m1a = m1a1k = a1(m1k) = a1m ≡ ma ≡ 0 mod m,contradiction, as we have assumed that A is a complete set. Therefore if A is complete, thengcd(a,m) must be 1. So we have proved both directions and we are done.

——————————–

——————————–

Example 7:For any positive integers m, a, where gcd(a,m) = 1, and any integer b, prove that there is someinteger x such that ax ≡ b mod m, and that set of all solutions for x is a residue class mod m.

Page 10: Mods

FLSAM Handout: Modular Arithmetic :O

Solution. By the previous example, we have that the set {1 · a, 2 · a, · · · ,m · a} is a complete setof residue classes. Therefore, there is exactly one solution x1 where 1 ≤ x1 ≤ m such that x1 ·a ≡ bmod m. Now we only need to prove that for any x that is not x1 that satisfies ax ≡ b mod m, thatx1 ≡ x mod m, as doing so would imply that they all belong to the same residue class modulo m.

So let some x2 be another solution. Then ax2 ≡ b mod m. Therefore ax2 ≡ ax1 mod m. Butsince gcd(a,m) = 1, we divide, yielding x1 ≡ x2 mod m, so x1, x2 belong to the same residue class,as desired.

——————————–

1.2.1 Recap

Remember, a residue class modulo m is a set of integers that all have the same remainder upondivision by m. For example, one set of residues modulo 5 is the set {. . . ,−1, 4, 9, 14, 19, . . .}, as thisset contains all integers that are equivalent to 4 modulo 5 (i.e. they have a remainder of 4 upondivision by 5). Residue classes may not seem important (or even relevant), but they will probablyhelp in some way in the future. They tend to help a lot when dealing with a lot of equivalenceproblems, such as solving linear congruences,etc.

1.2.2 Review Problems

1. Find the set of all solutions to the congruence 7x ≡ 2 mod 10.

2. Find the set of all solutions to the congruence 16x ≡ 6 mod 18.

3. Show that for any odd prime p 6= 3, that there exist infinitely many r such that 3r ≡ 1mod p.

Page 11: Mods

FLSAM Handout: Modular Arithmetic :O

1.3 Inverses

Inverses will probably help you a lot in the future; but first, let us begin by defining the term itself:

Definition 3: Inverse.Given a positive integer m and integer a, consider the numbera−1 such that a ·a−1 ≡ 1 mod m. Then a−1 is the inverse of a.

Generally, when working in the case of plain old real numbers, the inverse of an integer is a fraction,correct? The inverse of 2 is 1

2 , the inverse of 6 is 16 .

However, when working in mods, the inverse of a number (given it is invertible) is always aninteger. If it seems a bit odd, try some examples: For example, the inverse of 3 modulo 5 is 2;2 · 3 ≡ 1 mod 5. The inverse of 5 modulo 7 is 3, since 3 · 5 ≡ 1 mod 7. But even though I’ve justpresented two examples, does this mean that it’s true for all integers?

Theorem 1.3.1. Let m be a positive integer. Then for any integer a suchthat gcd(a,m) = 1, a has a unique integer inverse modulo m.

Proof. Well, what does this even mean? This means that if you are given a positive integer m, andan integer a so that a,m do not share any common factors, then a has only one integer inversemodulo m. That is, all solutions for b, where ab ≡ 1 mod m belong to the same residue class.

Note that since gcd(a,m) = 1, then the set A = {1 ·a, 2 ·a, 3 ·a, · · · ,m ·a} is a complete residueset. Therefore there is only one element of A that is congruent to 1 modulo m, and let this elementbe k · a. Since k is an integer between 1 and m, then k is an integer inverse of a. To prove thatall possible integer solutions are congruent to k modulo m (i.e. they belong to the residue classk +mZ), let there be a second inverse, k′. So k′a ≡ ka mod m. But since gcd(a,m) = 1, we havethat k′ ≡ k mod m. So then k′ is actually not a unique solution (modulo m), and so thereforethere is only one residue class that is an inverse of a.

Remark. Note that since there are technically infinitely many solutions (as any member of theresidue class a−1 + mZ is a solution), and also any member of the residue class a + mZ could besubstituted for a with the same result, then we could refine the theorem statement a bit:

Given a positive integer m and a residue class a+mZ such that gcd(a,m) = 1, then there exists aunique residue class such that the product of any member of that residue class and any member of

the residue class a+mZ is equivalent to 1 modulo m.

Though using the ”‘refined theorem”’ is just nitpicking, if it’s a bit confusing with all of theresidue class stuff, it would be recommended to go with the original statement. But to clarifythings a bit, a few examples are provided for the sake of the reader:

——————————–

Page 12: Mods

FLSAM Handout: Modular Arithmetic :O

Example 8:Find all solutions to the congruence 3x ≡ 1 mod 10.

Solution. Note that if x = 7, then it holds. Therefore by Theorem 1.3.1, all solutions thatsatisfy this congruence are the integer congruent to 7 modulo 10. That is, any member of theresidue class 7 + 10Z. To make things even simpler, we may write it as all x ≡ 7 mod 10.

——————————–

——————————–

Example 9:Find all integers (x, n) such that 12n + 3x ≡ 2 mod 11.

Solution. Since 12 ≡ 1 mod 11, 12n ≡ 1 mod 11. Therefore our equivalence can be writtenas 3x + 1 ≡ 2 mod 11 =⇒ 3x ≡ 1 mod 11. Note that x ≡ 4 mod 11 is a solution. By Theorem1.3.1, this is the only solution, so our solution set is all integer x ≡ 4 mod 11, or the residue class4 + 11Z.

——————————–

But what happens when gcd(a,m) 6= 1? Is there still an inverse? Or there does exist no inversefor a? Well, if gcd(a, n) 6= 1, let this value be k, and a = a1k,m = m1k. Then we want to find someb such that ba ≡ bka1 ≡ 1 mod m1k. This would imply that m1k|bka1 − 1. Therefore, k|bka1 − 1.But since k|bka1, this would imply that k would have to divide 1, impossible. So therefore ifgcd(a,m) 6= 1, there is no inverse. In fact, if gcd(a,m) = k, then ba always has a remainder thatis divisible by k (try to prove this yourself).

Thus, it suffices to make a refinement of Theorem 1.3.1:

Theorem 1.3.2. Let m be a positive integer, and a an integer. Then a hasan inverse modulo m if and only if gcd(a,m) = 1. Moreover, this inverse isunique modulo m.

Also, Theorem 1.3.2 implies the following corollary:

Corollary 1.3.1. For any a not divisible by p, a will have a unique inversemodulo p, if and only if p is a prime.

This follows pretty much instantly from Theorem 1.3.2, due to the fact that gcd(a, p) = 1 for allp - a.

Also, note that the presence of inverse can also help us prove some earlier results faster! Forexample, take Example 7. Since gcd(a,m) = 1, a has a unique inverse modulo m, so we have ax ≡ bmod m =⇒ a−1ax ≡ x ≡ a−1b mod m, therefore all x belong to the residue class a−1b+mZ.

Also, inverses can be used in a way to define ”‘division”’ partially. For example, 16 mod 11

seems to be meaningless; 16 isn’t an integer, so how does this work? Well, 1

6 is technically the

Page 13: Mods

FLSAM Handout: Modular Arithmetic :O

multiplicative inverse of 6 when working with the reals, so one may define 16 modulo 11 to be

the inverse of 6 modulo 11 (specifically, 6−1 ≡ 2 mod 11). So for example, 35 mod 7 would be

interpreted as 3 · 5−1 ≡ 3 · 3 ≡ 9 mod 7.

But this seems hardly useful; why not just skip the fractions and use the inverse notation a−1

to avoid logical pitfalls? Short answer: it makes things easier. In fact, operations with fractions inmods is pretty much the same of how they work when dealing with real numbers as a whole.

For example:

Theorem 1.3.3. ab + c

d ≡ad+bcbd mod m, given that gcd(b,m) = 1, gcd(d,m) = 1.

Proof. Since gcd(b,m) = gcd(c,m) = 1, they have unique inverses. The statement is equivalentto proving that a · b−1 + c · d−1 ≡ (ad + bc) ·

(b−1d−1

)mod m. But note that ad · b−1d−1 ≡

ab−1(d · d−1

)≡ ab−1 mod m. Similarly, bc ·

(b−1d−1

)≡ cd−1 mod m.

So we have that

ab−1 ≡ ad ·(b−1d−1

)mod m

cd−1 ≡ bc ·(b−1d−1

)mod m

and adding the two together gives the desired statement.

Theorem 1.3.4. ab ·

cd ≡

acbd mod m, given that gcd(b,m) = 1, gcd(d,m) = 1.

Proof. This can be rewritten as ab−1 · (cd−1) or ab−1cd−1 ≡ (ac)(b−1d−1

)mod m, as desired.

So why does this seem to work just like working with fractions in the reals? Well, think aboutthis way: when working modulo a number m, several axioms of arithmetic are preserved:

• Commutativity: Addition and multiplication are commutative operations.

• Associativity: Addition and multiplication are associative operations.

• Distributivity: Multiplication distributes over addition.

• Identities: There is a unique additive and multiplicative identity.

• Additive Inverse: For every number belonging to a set A, there exists a unique additiveinverse.

• Closure: If x, y belong to a set A, then x+ y is a unique element belonging to A.

The only basic axiom of real numbers that is not preserved in Zn (that is, the integers modulon) is:

Page 14: Mods

FLSAM Handout: Modular Arithmetic :O

• Multiplicative Inverse: For every element of a set A, there exists a unique multiplicativeinverse (only works if n is prime; which means that Zp where p is a prime has this property,but not for any general n).

So if we do operations with fractions in the real numbers, if we denote 1a has a−1, they’re pretty

much the same operations we use modulo m. Commutativity, associativity, and distributivity arestill there, so we can do basic operations. And if a has an inverse, then a · a−1 = 1, just like in thereals. Which means that we can do operations like normal (most of the time)!

We’ll end off with a notable example of how this fact could be utilized:

——————————–

Example 10:(IMO 2005) Consider the sequence a1, a2, · · · defined by

an = 2n + 3n + 6n − 1

for all positive integers n. Find all positive numbers that relatively prime to every term of thesequence.

——————————–

Proof. Now, don’t be intimidated! The first thing to do is to start with primes. If there is an integern that divides some ak, then any prime divisor of n must also divide ak. Therefore it suffices tofind all primes that that divide none of the terms.

Starting with primes 2, 3, 5, 7, 11 we find that 2, 3 divide a2, 5 divides a3 and a1, 7 divides a5,and 11 divides a9 through basic mods. Look at 5, 7, 11. They divide a3, a5, a9, respectively. Thisgives us the hunch that for any prime p > 3, p|ap−2. If that’s true, then this means that for anyprime, there is always at least one term such that the prime divides it, which would mean the onlyinteger relatively prime to all terms is 1!

To prove this, we will make use of Fermat’s Little Theorem (which will be covered verysoon), which states that for any integer a where p - a, then ap−1 ≡ 1 mod p.

Then assume p > 3 (we’ve already shown that 2, 3 divide a2, so we don’t need to cover thoseprimes anymore). Therefore by Fermat’s Little Theorem, we have that 2p−1 ≡ 3p−1 ≡ 6p−1 ≡ 1mod p, since 2, 3 - p.

Then this means that 2p−2 ≡ 2−1 mod p, 3p−2 ≡ 3−1 mod p, 6p−2 ≡ 6−1 mod p, achievedby multiplying each equation by 2−1, 3−1, 6−1, respectively. Therefore we have that ap−2 ≡ 2−1 +3−1 + 6−1 − 1, and we want to show ap−2 ≡ 0 mod p. But we already know that operations withfractions modulo p works just like the real numbers, so we get ap−2 ≡ 1

2 + 13 + 1

6 − 1 ≡ 0 mod p,as desired. Which means that the only positive integer relatively prime to all terms is 1.

(In fact, for every prime p, there are infinitely many terms that are divisible by p.)

Page 15: Mods

FLSAM Handout: Modular Arithmetic :O

1.3.1 Recap

In summary, we’ve shown the following things:

• The integer a has a unique inverse modulo m if and only if gcd(a,m) = 1. Otherwise, a hasno inverse.

• If p is a prime, then for any a such that p - a, then a has a unique inverse modulo p.

• Working with ”‘fractions”’ in Zn works pretty much exactly how operations with fractionswork in the real numbers.

These three things are vitally important when working with inverses; it will help to avoid anylogicals pitfalls and missed cases. Also, the fact that you can handle ”‘fractions”’ in Zn in the sameway that you work with them in the real numbers will make things a lot easier (you can avoid acouple lines of work).

1.3.2 Review Problems

1. (AIME) Let an = 6n + 8n. What is the remainder when a83 is divided by 49? (You don’tnecessarily have to use inverses, but there exists a solution using them)

2. Prove that for any prime p > 3, that

[(p− 1)!]2(2p− 1)2p(

1

12+

1

22+ · · ·+ 1

(p− 1)2

)≡ 12 + 22 + 32 + · · ·+ (p− 1)2 ≡ 0 mod p

(Hint: It’s a little easier than it might look at first glance; however, try to evaluate (p − 1)!mod p by spltting (p− 1)! into 1 · 2 · · · (p− 1) and pairing inverses; we’ll cover this in a coupleof sections. But note that 2 has its own unique inverse; so does 3, 4, etc.)

3. Let a, b be positive integers that are relatively prime. Prove that the arithmetic progressiona, a + b, a + 2b, a + 3b, · · · contains infinitely many terms that share the exact same primedivisors (not necessarily the same number of prime divisors).

(Hint 1: This progression is all terms that are equivalent to a mod b. Also, a has an inversemod b. So for some x, we have that ax ≡ 1 mod b. So then a(ax) ≡ a mod b, a(ax)2 ≡ amod b, etc.)

(Hint 2: Notice that for this sequence, if the term is written in the form a + kb, k can beany positive integer. But it doesn’t have to be written this way. For example, using x frombefore, is (a+ b)ax a term in this sequence?)

Page 16: Mods

Chapter 2

Standard Theorems and TheirApplications

First off, if you are not acquainted with the contents of the previous chapter, then go back and readit (you should be familiar with the usage and notions of basic modular arithmetic operations, residueclasses, and inverses). This chapter is intended for those who have mastered or are familiar withthe very basics and are ready to move on to study the standard theorems in modular arithmetic.

2.1 Fermat’s Little Theorem

A rather useful and interesting theorem in number theory is Fermat’s Little Theorem, and isquite well-known. It manages to draw a bridge between primes and modular arithmetic, two of themost important subjects in number theory.

Fermat’s Little Theorem: If a is an integer, and p is a prime,then

ap ≡ a mod p

As we’ve seen in the IMO question at the end of the last chapter, Fermat’s Little Theorem canprove to be very useful!

Proof. Lemma: For integers x, y, and prime p, we have xp + yp ≡ (x+ y)p mod p.

Note that this is problem 1 of Chapter 1, Section 1. If you haven’t seen the problem, go back andtry it on your own. (Hint: Use Binomial Theorem. Prove that if 1 ≤ k ≤ p− 1 then p divides

(pk

))

Using this Lemma, we have xp + yp + zp ≡ (x + y)p + zp mod p. Using the Lemma on x + yand z we get (x+ y)p + zp ≡ (x+ y + z)p mod p.

It seems that our Lemma has a general form for any n terms, and we’ll use the Lemma to prove:

ap1 + ap2 + · · ·+ apn ≡ (a1 + a2 + · · ·+ an)p mod p

15

Page 17: Mods

FLSAM Handout: Modular Arithmetic :O

Note that we’ve already shown that the cases for n = 2, 3 are true. So assume it holds for anynumber k. By induction we aim to prove that this implies the case for k + 1 is true. So we have

ap1 + ap2 + · · ·+ apk + apk+1 ≡ (a1 + a2 + · · ·+ ak)p + apk+1 mod p

from the case for k. Using the Lemma we get

(a1 + a2 + · · ·+ ak)p + apk+1 ≡ (a1 + a2 + · · ·+ ak + ak+1)

p mod p

so we’ve proven the inductive step and we’re done.

To prove Fermat’s Little Theorem, we set n = a and each ai = 1. Then we get

ap1 + ap2 + · · ·+ apa ≡ (a1 + a2 + · · ·+ aa)p mod p =⇒ 1 + 1 + · · ·+ 1 ≡ (1 + 1 + · · ·+ 1)p mod p

Since there are a 1’s on both sides, we get ap ≡ a mod p as desired. But note that thisonly works for nonnegative integers. To prove it for negative, just use −1 instead of 1, so we get(−a)p ≡ −a mod p where a is a positive integer. (or you could note that (−1)p = −1)

——————————–

Example 11:Evalute the remainder of 2118 upon division by 13.

Solution. Note that from Fermat’s Little Theorem (FLT), we have 212 ≡ 1 mod 13. 2118 can

be written as 212·9+10 ≡(212)9 · 210 ≡ 210 mod 13. So it suffices to find the remainder of 1024

upon division by 13. Through basic division we find this remainder to be 10.

——————————–

Alternatively, there is a second proof (in fact, there are dozens of proofs to this famous theorem- try to find one yourself!) that I will include:

Proof. Let a be an integer such that gcd(a, p) = 1. Then it is clear that {a, 2a, 3a, · · · , (p− 1)a}is a complete residue set. So each value in this set correponds to a unique value in the set{1, 2, 3, · · · , p− 1}. So we have

(a)(2a)(3a) · · · ((p− 1)a) ≡ 1 · 2 · 3 · · · (p− 1) mod p

Or

(1 · 2 · 3 · · · (p− 1)) ap−1 ≡ 1 · 2 · 3 · · · (p− 1) mod p

Since each of 1, 2, · · · , p− 1 is relatively prime with p, we can divide by 1 · 2 · 3 · · · (p− 1) to getap−1 ≡ 1 mod p, as desired.

Page 18: Mods

FLSAM Handout: Modular Arithmetic :O

2.1.1 Review Problems

1. Evaluate 4104 mod 19.

2. Let p to be an odd prime, and a, b to be odd natural numbers such that a + b is a multipleof p and a− b is a multiple of p− 1. Prove that ab + ba is a multiple of 2p. (Hint: a+ b ≡ 0mod p⇐⇒ a ≡ −b mod p)

3. If x, y, z are distinct integers, prove that (x − y)7 + (y − z)7 + (z − x)7 is divisible by7(x− y)(y − z)(z − x). Can you generalize this? (Hint: What happens when x = y?)

Page 19: Mods

FLSAM Handout: Modular Arithmetic :O

2.2 Chinese Remainder Theorem

Chinese Remainder theorem is a very, very useful theorem (though it may not appear so at firstglance).

Chinese Remainder Theorem: Let n1, n2, n3, · · · , nk be kpositive integers that are pairwise coprime (which means thatany two of them are coprime). Then for any k numbersa1, a2, a3, · · · , ak, there exists an integer x satisying all of thefollowing congruences:

x ≡ a1 mod n1

x ≡ a2 mod n2...

x ≡ ak mod nk

Moreover, there are infinitely many solutions, and are equivalentto each other mod n1n2n3 · · ·nk.

Proof. ConsiderN = n1n2 · · ·nk. Now consider the integerN

nifor some i. Then since gcd

(N

ni, ni

)= 1,

we have that there exists some integer si such that siN

ni≡ 1 mod ni; that is, there exists an in-

verse, so let this be denoted instead as

(N

ni

)−1ni

, or the inverse ofN

nimodulo ni. Then we can see

that

x =k∑i=1

aiN

ni

(N

ni

)−1ni

is a solution. This is due to the fact that if we take x modulo any ni, thenN

nj≡ 0 mod ni if i 6= j.

So then we have

x ≡k∑i=1

aiN

ni

(N

ni

)−1ni

≡ aiN

ni

(N

ni

)−1ni

≡ ai(1) ≡ ai mod ni

Thus x satisfies all of the congruences, and is a solution. Now we show that all such x are equivalentmodulo N . So for sake of contradiction assume that there exist two solutions x1, x2 such that

Page 20: Mods

FLSAM Handout: Modular Arithmetic :O

x1 6≡ x2 mod N . Since they are both solutions we have

x1 ≡ a1 ≡ x2 mod n1

x1 ≡ a2 ≡ x2 mod n2...

x1 ≡ ak ≡ x2 mod nk

Therefore we must have that x1 − x2 is a multiple of n1, n2, ..., nk. But since n1, n2, · · · , nk arepairwise relatively prime, this means that x1 − x2 is a multiple of n1n2 · · ·nk = N , contradiction.Therefore all solutions are equivalent to one unique residue modulo N .

Remark. There is also a generalization of this theorem when n1, n2, · · · , nk are not pairwiserelatively prime, and that there is a unique solution modulo lcm(n1, n2, · · · , nk).

——————————–

Example 12:Today Mrs. Hernandez is trying to arrange her classroom desks into columns of equal length.However, she discovers that when she tries to divide the classroom into 2 columns, there is onedesk left. When she tries to divide it into 3 columns, there are 2 left, and when she tries to divideit into 5 columns, there is 1 desk left. How many possible desks does Mrs. Hernandez have?

Solution. Note that we are trying to find all positive integer x such that

x ≡ 1 mod 2

x ≡ 2 mod 3

x ≡ 1 mod 5

By Chinese Remainder Theorem there is a unique solution modulo 30, so once we find thesolution that is below 30, we’ll have found all of them, as all solutions are equivalent modulo 30.Note that x = 11 fits the three congruences, and since 11 < 30 we have by the Chinese RemainderTheorem that the possible number of desks that Mrs. Hernandez can have is any number x suchthat x ≡ 11 mod 30. For example, 11, 41, 71 all fit the criteria.

——————————–

——————————–

Example 13:

Let f(n) be the least positive integer such that

f(n)∑k=1

k is divisible by n. Prove that f(n) = 2n− 1 if

and only if n = 2m where m is nonnegative.

Page 21: Mods

FLSAM Handout: Modular Arithmetic :O

Solution. From this we conclude that such f(n) must be the minimal positive integer such

thatf(n) (f(n) + 1)

2is a multiple of n, or that f(n) (f(n) + 1)) ≡ 0 mod 2n. For convenience, let

f(n) = α. Additionally, let n be written as 2ms where s is an odd number, and m is nonnegative.Then we must have that

α(α+ 1) ≡ 0 mod 2m+1

α(α+ 1) ≡ 0 mod s

Well, this certainly looks like the Chinese Remainder Theorem can be used! We know that foreach equation, since α and α + 1 are relatively prime, either α ≡ 0 or α ≡ −1. So let’s split thisinto 4 cases:

1. α ≡ 0 mod 2m+1, α ≡ 0 mod s

2. α ≡ 0 mod 2m+1, α ≡ −1 mod s

3. α ≡ −1 mod 2m+1, α ≡ 0 mod s

4. α ≡ −1 mod 2m+1, α ≡ −1 mod s

Since 2m+1 and s are relatively prime, by the Chinese Remainder Theorem we have that foreach case there is a unique α under 2m+1s = 2n that satisfies the congruences. For Case 1, it isclearly 2n, and for Case 4 it’s clearly 2n− 1. Note that 2n, 2n− 1 are the two largest integers lessthan or equal to 2n. Therefore Case 2 and Case 3 will each give a unique solution under both 2nand 2n − 1, but this violates the fact that 2n − 1 must be the minimal solution. However, this isonly true if s > 1. Therefore we must have that s = 1 (in which case Case 2 and Case 3 becomethe same as Case 1 and Case 4), so n = 2m as desired.

To prove the other direction, it suffices to prove that the minimal positive integer k such thatk(k + 1)

2is a multiple of 2m is 2m+1 − 1. This is left as an exercise for the reader.

——————————–

Page 22: Mods

FLSAM Handout: Modular Arithmetic :O

2.2.1 Review Problems

1. (Sun Tsu Suan-Ching; 4th century AD) There are certain things whose number is unknown.Repeatedly divided by 3, the remainder is 2; by 5 the remainder is 3; and by 7 the remainderis 2. What will be the number?

2. Prove that for any integer n, there exist n consecutive numbers such that none of them aresquare-free. A number that is square-free is not divisible by any perfect square (except 1).(Hint: Try and make some integer k such that k ≡ 0 mod 22, k + 1 ≡ 0 mod 32, etc.)

3. The unisolvence theorem states that given n + 1 points of the form (xi, yi) where no two xiare the same, there exists a unique polynomial of degree at most n such that f(xi) = yi fori = 1, 2, . . . , n+ 1.

(Hint 1: Use the method that we employed to not only prove the Chinese Remainder Theorem,but construct the unique solution; this unique solution is known as the Lagrange InterpolationPolynomial)

(Hint 2: To prove uniqueness, let there be two such polynomials p(x), q(x). Now look at thedegree of r(x) = p(x) − q(x). If p(xi) = q(xi) = yi, for n + 1 points, how many roots doesr(x) have?)

Page 23: Mods

FLSAM Handout: Modular Arithmetic :O

2.3 Euler’s Theorem

Euler’s Theorem or the Fermat-Euler Theorem/Euler’s Totient Theorem is a generaliza-tion of Fermat’s Little Theorem and states:

Euler’s Theorem: Given any a, n such that gcd(a, n) = 1, then

aφ(n) ≡ 1 mod n

where φ(n) is the totient function of n.

Note that Fermat’s Little Theorem is the specific case for when n is a prime.

But first off, what is this totient function thing? Well, the totient function φ(n) is defined asthe number of positive integers under n that are relatively prime to it. For example, φ(10) = 4because if we list out the first couple of positive integers, 1, 2, 3, 4, 5, 6, 7, 8, 9, then only 1, 3, 7, 9are relatively prime to 10. Similarly φ(8) = 4 because for the integers 1, 2, 3, 4, 5, 6, 7 the integers1, 3, 5, 7 are relatively prime to 8. There is also a formula for this totient function, so that we don’thave to write out all the integers below n. We have that

φ(n) = n

k∏i=1

(pi − 1

pi

)Where pi is a prime that divides n. So for example, to find the number of integers below 100 thatare relatively prime to 100, we have that 100 = 22 · 52, so the answer is

φ(100) = 100

(1

2

)(4

5

)= 40

Now we use this to prove Euler’s Theorem:

Proof. From Fermat’s Little Theorem, we have that if gcd(a, p) = 1 with p a prime, then

ap−1 ≡ 1 mod p

So let ap−1 be rewritten as kp+ 1 where k is an integer. Now let us consider (kp+ 1)pk−1

mod pk.