8.6 Modular Arithmetic
Total Page:16
File Type:pdf, Size:1020Kb
“mcs” — 2015/5/18 — 1:43 — page 263 — #271 8.6 Modular Arithmetic On the first page of his masterpiece on number theory, Disquisitiones Arithmeticae, Gauss introduced the notion of “congruence.” Now, Gauss is another guy who managed to cough up a half-decent idea every now and then, so let’s take a look at this one. Gauss said that a is congruent to b modulo n iff n .a b/. This is j written a b.mod n/: ⌘ For example: 29 15 .mod 7/ because 7 .29 15/: ⌘ j It’s not useful to allow a modulus n 1, and so we will assume from now on that moduli are greater than 1. There is a close connection between congruences and remainders: Lemma 8.6.1 (Remainder). a b.mod n/ iff rem.a; n/ rem.b; n/: ⌘ D Proof. By the Division Theorem 8.1.4, there exist unique pairs of integers q1;r1 and q2;r2 such that: a q1n r1 D C b q2n r2; D C “mcs” — 2015/5/18 — 1:43 — page 264 — #272 264 Chapter 8 Number Theory where r1;r2 Œ0::n/. Subtracting the second equation from the first gives: 2 a b .q1 q2/n .r1 r2/; D C where r1 r2 is in the interval . n; n/. Now a b.mod n/ if and only if n ⌘ divides the left side of this equation. This is true if and only if n divides the right side, which holds if and only if r1 r2 is a multiple of n. But the only multiple of n in . n; n/ is 0, so r1 r2 must in fact equal 0, that is, when r1 rem.a; n/ WWD D r2 rem.b; n/. ⌅ WWD So we can also see that 29 15 .mod 7/ because rem.29; 7/ 1 rem.15; 7/: ⌘ D D Notice that even though “(mod 7)” appears on the end, the symbol isn’t any more ⌘ strongly associated with the 15 than with the 29. It would probably be clearer to write 29 7 15, for example, but the notation with the modulus at the end is ⌘mod firmly entrenched, and we’ll just live with it. The Remainder Lemma 8.6.1 explains why the congruence relation has proper- ties like an equality relation. In particular, the following properties7 follow imme- diately: Lemma 8.6.2. a a.mod n/ (reflexivity) ⌘ a b IFF b a.mod n/ (symmetry) ⌘ ⌘ .a b AND b c/ IMPLIES a c.mod n/ (transitivity) ⌘ ⌘ ⌘ We’ll make frequent use of another immediate corollary of the Remainder Lemma 8.6.1: Corollary 8.6.3. a rem.a; n/ .mod n/ ⌘ Still another way to think about congruence modulo n is that it defines a partition of the integers into n sets so that congruent numbers are all in the same set. For example, suppose that we’re working modulo 3. Then we can partition the integers into 3 sets as follows: :::; 6; 3; 0; 3; 6; 9; : : : f g :::; 5; 2; 1; 4; 7; 10; : : : f g :::; 4; 1; 2; 5; 8; 11; : : : f g 7Binary relations with these properties are called equivalence relations, see Section 9.10. “mcs” — 2015/5/18 — 1:43 — page 265 — #273 8.7. Remainder Arithmetic 265 according to whether their remainders on division by 3 are 0, 1, or 2. The upshot is that when arithmetic is done modulo n, there are really only n different kinds of numbers to worry about, because there are only n possible remainders. In this sense, modular arithmetic is a simplification of ordinary arithmetic. The next most useful fact about congruences is that they are preserved by addi- tion and multiplication: Lemma 8.6.4 (Congruence). If a b.mod n/ and c d.mod n/, then ⌘ ⌘ a c b d.mod n/; (8.7) C ⌘ C ac bd .mod n/: (8.8) ⌘ Proof. Let’s start with 8.7. Since a b.mod n/, we have by definition that ⌘ n .b a/ .b c/ .a c/, so j D C C a c b c.mod n/: C ⌘ C Since c d.mod n/, the same reasoning leads to ⌘ b c b d.mod n/: C ⌘ C Now transitivity (Lemma 8.6.2) gives a c b d.mod n/: C ⌘ C The proof for 8.8 is virtually identical, using the fact that if n divides .b a/, then it certainly also divides .bc ac/. ⌅ 8.7 Remainder Arithmetic The Congruence Lemma 8.6.1 says that two numbers are congruent iff their remain- ders are equal, so we can understand congruences by working out arithmetic with remainders. And if all we want is the remainder modulo n of a series of additions, multiplications, subtractions applied to some numbers, we can take remainders at every step so that the entire computation only involves number in the range Œ0::n/. “mcs” — 2015/5/18 — 1:43 — page 266 — #274 266 Chapter 8 Number Theory General Principle of Remainder Arithmetic To find the remainder on division by n of the result of a series of additions and multiplications, applied to some integers replace each integer operand by its remainder on division by n, ✏ keep each result of an addition or multiplication in the range Œ0::n/ by im- ✏ mediately replacing any result outside that range by its remainder on divi- sion by n. For example, suppose we want to find rem..444273456789 155558585555/4036666666; 36/: (8.9) C This looks really daunting if you think about computing these large powers and then taking remainders. For example, the decimal representation of 444273456789 has about 20 million digits, so we certainly don’t want to go that route. But re- membering that integer exponents specify a series of multiplications, we follow the General Principle and replace the numbers being multiplied by their remainders. Since rem.44427; 36/ 3; rem.15555858; 36/ 6, and rem.403; 36/ 7, we D D D find that (8.9) equals the remainder on division by 36 of .33456789 65555/76666666: (8.10) C That’s a little better, but 33456789 has about a million digits in its decimal represen- tation, so we still don’t want to compute that. But let’s look at the remainders of the first few powers of 3: rem.3; 36/ 3 D rem.32; 36/ 9 D rem.33; 36/ 27 D rem.34; 36/ 9: D We got a repeat of the second step, rem.32; 36/ after just two more steps. This means means that starting at 32, the sequence of remainders of successive powers of 3 will keep repeating every 2 steps. So a product of an odd number of at least three 3’s will have the same remainder on division by 36 as a product of just three 3’s. Therefore, rem.33456789; 36/ rem.33; 36/ 27: D D “mcs” — 2015/5/18 — 1:43 — page 267 — #275 8.7. Remainder Arithmetic 267 What a win! Powers of 6 are even easier because rem.62; 36/ 0, so 0’s keep repeating after D the second step. Powers of 7 repeat after six steps, but on the fifth step you get a 1, that is rem.76; 36/ 1, so (8.10) successively simplifies to be the remainders of D the following terms: .33456789 65555/76666666 C .33 62 65553/.76/1111111 C .33 0 65553/11111111 C 27: D Notice that it would be a disastrous blunder to replace an exponent by its re- mainder. The general principle applies to numbers that are operands of plus and times, whereas the exponent is a number that controls how many multiplications to perform. Watch out for this. 8.7.1 The ring Zn It’s time to be more precise about the general principle and why it works. To begin, let’s introduce the notation n for doing an addition and then immediately taking C a remainder on division by n, as specified by the general principle; likewise for multiplying: i n j rem.i j; n/; C WWD C i n j rem.ij; n/: WWD Now the General Principle is simply the repeated application of the following lemma. Lemma 8.7.1. rem.i j; n/ rem.i; n/ n rem.j; n/; (8.11) C D C rem.ij; n/ rem.i; n/ n rem.j; n/: (8.12) D Proof. By Corollary 8.6.3, i rem.i; n/ and j rem.j; n/, so by the Congru- ⌘ ⌘ ence Lemma 8.6.4 i j rem.i; n/ rem.j; n/ .mod n/: C ⌘ C By Corollary 8.6.3 again, the remainders on each side of this congruence are equal, which immediately gives (8.11). An identical proof applies to (8.12). ⌅ “mcs” — 2015/5/18 — 1:43 — page 268 — #276 268 Chapter 8 Number Theory The set of integers in the range Œ0::n/ together with the operations n and n is C referred to as Zn, the ring of integers modulo n. As a consequence of Lemma 8.7.1, the familiar rules of arithmetic hold in Zn, for example: .i n j/ n k i n .j n k/: D These subscript-n’s on arithmetic operations really clog things up, so instead we’ll just write “(Zn)” on the side to get a simpler looking equation: .i j/ k i .j k/ .Zn/: D 8 In particular, all of the following equalities are true in Zn: .i j/ k i .j k/ (associativity of ); D .i j/ k i .j k/ (associativity of ); C C D C C C 1 k k (identity for ); D 0 k k (identity for ); C D C k .