MODULAR ARITHMETIC 1 Working With Integers The usual arithmetic operations of addition, subtraction and multiplication can be performed on integers, and the result is always another integer Division, on the other hand, is another story, since dividing one integer by another doesn t always produce another integer For example, the number 3 divides evenly into 6, producing a quotient of 2, but 3 does not divide evenly into 14 Attempting to divide 3 into 14 produces a quotient of q = 4 and a remainder of r = 2 Here you can obtain the quotient q = 4 as the integer part of the fraction 14 3 = 4 2 3 The remainder is then r = 14 3q = 14 12 = 2 The above procedure is quite general Theorem 1 (Division Theorem) Let a and b be any integers with a > 0 There are unique integers q (the quotient) and r (the remainder) satisfying (1) b = aq + r (2) 0 r < a Proof There are two assertions, existence and uniqueness, which must be proved separately We begin with existence Let S = {b ak : k Z} We first show that S contains a non-negative integer Taking k = b, we obtain b + a b S Since a 1, we obtain b + a b b + b 0 Thus S has at least one non-negative member Let S + be the set of all non-negative members of S By the Well Ordering Principle, 1 S + contains a smallest element Call it r Since r S, there is an integer q such that r = b aq, and rearranging gives item 1 Since r S +, we have r 0, so to prove item 2, we only have to show that r < a Suppose r a Then r a 0, and clearly r a S, so r a S + Since r a < r, this contradicts the choice or r as the smallest member of S + This establishes item 2, which completes the existence proof We now turn to uniqueness Suppose the pairs q 1, r 1 and q 2, r 2 both satisfy the conditions of the theorem We may assume without loss that r 1 r 2 We have (1) q 1 a + r 1 = b = q 2 a + r 2, so r 2 r 1 = (q 1 q 2 )a Therefore r 2 r 1 is a multiple of a But since 0 r 2 r 1 r 2 < a, we must have r 2 r 1 = 0, so r 1 = r 2 Inserting this into (1) gives q 1 = q 2, and uniqueness is established 1 The Well Ordering Principle asserts that every non-empty set of non-negative integers contains a smallest member It is equivalent to the Principle of Mathematical Induction 1
MODULAR ARITHMETIC 2 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Table 1 The Integers Modulo 5 The problem of calculating the quotient q and remainder r from the numbers a and b is the integer division problem There s an algorithm for efficiently solving the integer division problem even for very large values of a and/or b In fact, it s just the long division algorithm you learned in school Most high level computer languages include instructions for integer division For example with Sage, q and r are given by b//a and b%a respectively In written work, we often use the notation b mod a for the remainder r when a is divided by b Exercise 2 For each of the following pairs a and b, calculate the quotient and remainder when b is divided by a (1) a = 9, b = 23 (2) a = 9, b = 27 (3) a = 9, b = 23 (4) a = 10, b = 27 (5) a = 10, b = 123456789 (6) a = 54, b = 9876543 3 Working With Remainders Modular arithmetic works like the numbers on a clock, which repeat in cycles of 12 In the case of a clock the number 12 is the modulus, and you identify any two numbers which differ by a multiple of 12 Thus, the numbers 9, 3, 15, and 27 all register the same way on the clock face We say that these numbers are all the same modulo 12 Of course there s nothing magical about the number 12 The same kind of identifications could be carried out using another positive integer for the modulus For example, using a modulus of 5, you d identify any two numbers that differ by a multiple of 5 You can visualize this by imagining all the integers arranged in a 5 column table, a portion of which is shown in Table 1 When working modulo 5, numbers in the same column of Table 1 are identified, so that, for example, you don t distinguish between the numbers 4 and 11 In general, when working modulo n, you ll identify any two numbers which differ by a multiple of n To be precise, we ll write a b (mod n) to mean that a b is divisible by n The above expression is read a is congruent to b modulo n Again, you can imagine arranging the integers into a table with n columns as in Table 2, and identifying any two numbers that are in the same column
MODULAR ARITHMETIC 3 n n + 1 1 0 1 n 1 n n + 1 2n 1 Table 2 The Integers Modulo n The following theorem summarizes the key properties of congruence modulo n Its proof is left as an exercise Theorem 2 Let n be a positive integer (1) (Reflexivity) For every integer a we have a a (mod n) (2) (Symmetry) a b (mod n) if and only if b a (mod n) (3) (Transitivity) If a b (mod n) and b c (mod n) then a c (mod n) (4) If a b (mod n) and c d (mod n) then a + c b + d (mod n) and ac bd (mod n) We now make precise the idea of partitioning the integers into columns, that we discussed informally above We fix an positive integer n, which we call a modulus For any integer a, we define the congruence class of a modulo n as [a] n = {b Z : a b (mod n)} We will sometimes write [a], omitting the subscript n, when the modulus is clear from context Theorem 3 a b (mod n) if and only if [a] n = [b] n Proof Suppose first that [a] n = [b] n By reflexivity, we have b [b] n Since [a] n = [b] n, we have b [a] n By definition of congruence classes, we have a b (mod n) Conversely, suppose a b (mod n) Let c [a] n By definition of congruence classes, a c (mod n) By symmetry, b a (mod n), and so, by transitivity, b c (mod n) By definition of equivalence classes, c [b] n Since c represents an arbitrary member of [a] n, we have shown that [a] n [b] n A similar argument, reversing the roles of a and b, gives the reverse inclusion, [a] n = [b] n Corollary 4 Every integer is in exactly one congruence class modulo n Proof For every integer a, we have a [a] n, so to complete the proof, we must show that any two congruence classes are either equal or disjoint Suppose [a] n and [b] n are not disjoint Let c [a] n [b] n Thus a c (mod n) and b c (mod n), so, by reflexivity and transitivity, a b (mod n) By the previous theorem, [a] n = [b] n We define Z n to be the set of all congruence classes modulo n Theorem 5 Z n = {[0] n, [1] n,, [n 1] n }, and the congruence classes [0] n,, [n 1] n are distinct
MODULAR ARITHMETIC 4 + 4 0 1 2 3 0 0 1 2 3 1 1 2 3 0 2 2 3 0 1 3 3 0 1 2 4 0 1 2 3 0 0 0 0 0 1 0 1 2 3 2 0 2 0 2 3 0 3 2 1 Table 3 Addition and Multiplication Modulo 4 Proof Let a Z Dividing a by n, we obtain integers q and r with 0 r < n 1 and a = qn + r It follows that a r (mod n), so a [r] n This establishes the first assertion For the second assertion, suppose 0 r 1 < r 2 n 1 Then 0 r 2 r 1 < n, so n is not a divisor of r 2 r 1, and therefore r 1 r 2 (mod n), and so [r 1 ] n [r 2 ] n 4 Arithmetic in Z n We want to define addition and multiplication as binary operations on Z n by setting [a] n + [b] n = [a + b] n and [a] n [b] n = [ab] n To make sense of this, we must check that congruence classes on the right do not depend on the choice of representatives a and b of the congruence classes on the left For this, suppose that [a] n = [a ] n and [b] n = [b ] n We must show that [a + b] n = [a + b ] n and [ab] n = [a b ] n By Theorem 3 we have a a (mod n) and b b (mod n) It now follows from Theorem 2 that a + b a + b (mod n) and ab a b (mod n) Another application of Theorem 3 gives [a + b] n = [a + b ] n and [ab] n = [a b ] n Therefore, the addition and multiplication are unambiguously defined binary operations on Z n Associativity and commutativity of integer addition and multiplication carry over immediately to addition and multiplication on Z n Further, we have [0] n + [a] n = [a] n and [a] n + [ a] n = [0] n for every integer a, so Z n is an abelian group under addition of congruence classes By Theorem 5, every congruence class modulo n contains exactly one of the numbers 0, 1,, n 1 It is common practice to use these numbers as representatives of their congruence classes When we do this, we write, somewhat imprecisely, Z n = {1, 2,, n 1}, with the understanding that the integer j stands for the congruence class [j] n When taking this point of view, we sometimes denote addition and multiplication in Z + n by + n and n, respectively, to distinguish them from the usual integer operations Mod n addition or multiplication is performed by first adding or multiplying as ordinary integers, and then replacing the sum or product by the remainder upon division by n In symbols, j + n k = (j + k) mod n, j n k = (j k) mod n For any given modulus, you can, at least in principle, write out the entire addition and multiplication tables Table 3 gives the addition and multiplication tables modulo 4
MODULAR ARITHMETIC 5 Exercise 5 Build a addition and multiplication tables modulo 6 Exercise 6 Use your answer to Exercise 5 to find all solutions x (if any) to each of the following (1) 5x 1 (mod 6) (2) 4x 1 (mod 6) (3) 4x 2 (mod 6) 61 Modular Arithmetic in Sage The algebraic structure Z n can be constructed in Sage with Integers(n) Here are a few lines of Sage code to perform some calculations in Z 42 sage: Z42=Integers(42) sage: a=z42(27) sage: b=z42(31) sage: a,b (27, 31) sage: a+b 16 sage: a*b 39 sage: a^100 15