Difference between revisions of "Modular arithmetic/Introduction"

m (cut old info)
(Subtraction rule)
 
(73 intermediate revisions by 32 users not shown)
Line 1: Line 1:
'''Modular arithmetic''' is a special type of arithmetic that involves only [[integers]].
+
'''[[Modular arithmetic]]''' is a special type of arithmetic that involves only [[integers]].  This goal of this article is to explain the basics of modular arithmetic while presenting a progression of more difficult and more interesting problems that are easily solved using modular arithmetic.
  
  
  
== The Basic Idea ==
+
==Introductory Video==
Let's use a clock as an example, except let's replace the <math>\displaystyle 12</math> at the top of the clock with a <math>\displaystyle 0</math>. Starting at noon, the hour hand points in order to the following:
+
https://youtu.be/7an5wU9Q5hk?t=777
  
 +
== Motivation ==
 +
Let's use a clock as an example, except let's replace the <math>12</math> at the top of the clock with a <math>0</math>.
  
<math>\displaystyle 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, \ldots </math>
+
<asy>picture pic;
 +
path a;
 +
a = circle((0,0), 100);
 +
draw (a);
 +
draw((0,0), linewidth(4));
  
 +
pair b;
 +
b = (0,100);
  
This is the way in which we count in '''modulo 12'''. When we add <math>\displaystyle 1</math> to <math>\displaystyle 11</math>, we arrive back at <math>\displaystyle 0</math>. The same is true in any other [[modulus]] (modular arithmetic system).  In modulo <math>\displaystyle 5</math>, we [[counting | count]]
+
for (int i = 0; i < 12; ++i)
 +
{
 +
label (pic, (string) i, b);
 +
b = rotate(-30,(0,0)) * b;
 +
}
 +
pic = scale(0.8) * pic;
 +
add(pic);
  
 +
draw ((0,0) -- 50*dir(90));
 +
draw ((0,0) -- 70*dir(90));</asy>
  
<math>\displaystyle 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, \ldots</math>
+
Starting at noon, the hour hand points in order to the following:
  
 +
<math>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, \ldots </math>
  
We can also count backwards in modulo 5.  Any time we subtract 1 from 0, we get 4.  So, the integers from <math>\displaystyle -12</math> to <math>\displaystyle 0</math>, when written in modulo 5, are
 
  
 +
This is the way in which we count in '''modulo 12'''.  When we add <math>1</math> to <math>11</math>, we arrive back at <math>0</math>.  The same is true in any other [[modulus]] (modular arithmetic system).  In modulo <math>5</math>, we [[counting | count]]
  
<math>\displaystyle 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0,</math>
 
  
 +
<math>0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, \ldots</math>
  
where <math>\displaystyle -12</math> is the same as <math>\displaystyle 3</math> in modulo 5.  Because all integers can be expressed as <math>\displaystyle 0</math>, <math>\displaystyle 1</math>, <math>\displaystyle 2</math>, <math>\displaystyle 3</math>, or <math>\displaystyle 4</math> in modulo 5, we give these give integers their own name: the modulo 5 '''[[residue | residues]]'''.  In general, for a natural number <math>\displaystyle n</math> that is greater than 1, the modulo <math>\displaystyle n</math> residues are the integers that are [[whole number | whole numbers]] less than <math>\displaystyle n</math>:
 
  
 +
We can also count backwards in modulo 5.  Any time we subtract 1 from 0, we get 4.  So, the integers from <math>-12</math> to <math>0</math>, when written in modulo 5, are
  
<math>\displaystyle 0, 1, 2, \ldots, n-1. </math>
+
 
 +
<math>3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0,</math>
 +
 
 +
 
 +
where <math>-12</math> is the same as <math>3</math> in modulo 5.  Because all integers can be expressed as <math>0</math>, <math>1</math>, <math>2</math>, <math>3</math>, or <math>4</math> in modulo 5, we give these integers their own name: the '''[[residue class]]es''' modulo 5.  In general, for a natural number <math>n</math> that is greater than 1, the modulo <math>n</math> residues are the integers that are [[whole number | whole numbers]] less than <math>n</math>:
 +
 
 +
 
 +
<math>0, 1, 2, \ldots, n-1. </math>
  
  
 
This just relates each integer to its [[remainder]] from the [[Division Theorem]].  While this may not seem all that useful at first, counting in this way can help us solve an enormous array of [[number theory]] problems much more easily!
 
This just relates each integer to its [[remainder]] from the [[Division Theorem]].  While this may not seem all that useful at first, counting in this way can help us solve an enormous array of [[number theory]] problems much more easily!
  
 +
==Residue==
 +
We say that <math>a</math> is the modulo-<math>m</math> <b>residue</b> of <math>n</math> when <math>n\equiv a\pmod m</math>, and <math>0\le a<m</math>.
  
 +
== Congruence ==
 +
There is a mathematical way of saying that all of the integers are the same as one of the modulo 5 residues.  For instance, we say that 7 and 2 are '''[[congruent]]''' modulo 5.  We write this using the symbol <math>\equiv</math>: In other words, this means in base 5, these integers have the same residue modulo 5:
  
== Congruence ==
+
<center><math>2\equiv 7\equiv 12 \pmod{5}. </math></center>
There is a mathematical way of saying that all of the integers are the same as one of the modulo 5 residues.  For instance, we say that 7 and 2 are '''[[congruent]]''' modulo 5.  We write this using the symbol <math>\displaystyle \equiv</math>:
 
  
  
<center><math>\displaystyle 7 \equiv 2 \pmod{5}. </math></center>
+
The '''(mod 5)''' part just tells us that we are working with the integers modulo 5.  In modulo 5, two integers are congruent when their difference is a [[multiple]] of 5.
 +
In general, two integers <math>a</math> and <math>b</math> are congruent modulo <math>n</math> when <math>a - b</math> is a multiple of <math>n</math>.  In other words, <math>a \equiv b \pmod{n}</math> when <math>\frac{a-b}{n}</math> is an integer.  Otherwise, <math>a \not\equiv b \pmod{n}</math>, which means that <math>a</math> and <math>b</math> are '''not congruent''' modulo <math>n</math>.
  
  
The '''(mod 5)''' part just tells us that we are working with the integers modulo 5.  In modulo 5, two integers are congruent when their difference is a [[multiple]] of 5.  Thus each of the following integers is congruent modulo 5:
 
<center><math>\displaystyle -12 \equiv -7 \equiv -2 \equiv 3 \equiv 8 \equiv 13 \equiv 18 \equiv 23 \pmod{5} </math></center>
 
In general, two integers <math>\displaystyle a</math> and <math>\displaystyle b</math> are congruent modulo <math>\displaystyle n</math> when <math>\displaystyle a - b</math> is a multiple of <math>\displaystyle n</math>.  In other words,
 
  
 +
=== Examples ===
 +
* <math>31 \equiv 1 \pmod{10}</math> because <math>31 - 1 = 30</math> is a multiple of <math>10</math>.
  
  
 +
* <math>43 \equiv 22 \pmod{7}</math> because <math>\frac{43 - 22}{7} = \frac{21}{7} = 3</math>, which is an integer.
  
== Arithmetic Modulo n ==
 
  
=== Useful Facts ===
+
* <math>8 \not\equiv -8 \pmod{3}</math> because <math>8 - (-8) = 16</math>, which is not a multiple of <math>3</math>.
  
Consider four integers <math>{a},{b},{c},{d}</math> and a positive integer <math>{m}</math> such that <math>a\equiv b\pmod {m}</math> and <math>c\equiv d\pmod {m}</math>. In modular arithmetic, the following [[identity | identities]] hold:
 
  
* Addition: <math>a+c\equiv b+d\pmod {m}</math>.
+
* <math>91 \not\equiv 18 \pmod{6}</math> because <math>\frac{91 - 18}{6} = \frac{73}{6}</math>, which is not an integer.
* Subtraction: <math>a-c\equiv b-d\pmod {m}</math>.
 
* Multiplication: <math>ac\equiv bd\pmod {m}</math>.
 
* Division: <math>\frac{a}{e}\equiv \frac{b}{e}\pmod {\frac{m}{\gcd(m,e)}}</math>, where <math>e</math> is a positive integer that divides <math>{a}</math> and <math>b</math>.
 
* Exponentiation: <math>a^e\equiv b^e\pmod {m}</math> where <math>e</math> is a positive integer.
 
  
==== Examples ====
 
  
* <math>{7}\equiv {1} \pmod {2}</math>
 
  
* <math>49^2\equiv 7^4\equiv (1)^4\equiv 1 \pmod {6}</math>
+
=== Sample Problem ===
 +
Find the modulo <math>4</math> residue of <math>311</math>.
 +
==== Solution: ====
 +
Since <math>311 \div 4 = 77</math> R <math>3</math>, we know that
 +
<center><math>311 \equiv 3 \pmod{4}</math></center>
 +
and <math>3</math> is the modulo <math>4</math> residue of <math>311</math>.
  
* <math>7a\equiv 14\pmod {49}\implies a\equiv 2\pmod {7}</math>
+
==== Another Solution: ====
 +
Since <math> 311 = 300 + 11 </math>, we know that
 +
<center><math>311 \equiv 0+11 \pmod{4}</math></center>
 +
We can now solve it easily
 +
<center><math>11 \equiv 3 \pmod{4}</math></center>
 +
and <math>3</math> is the modulo <math>4</math> residue of <math>311</math>
  
=== The Integers Modulo n ===
+
==== Another Solution: ====
 +
We know <math>308</math> is a multiple of <math>4</math> since <math>8</math> is a multiple of <math>4</math>. Thus, <math>311-308=3</math> and <math>3</math> is the modulo <math>4</math> residue of <math>311</math>.
  
The relation <math>a \equiv b</math> (mod <math>n</math>) allows us to divide the set of integers into sets of equivalent elementsFor example, if <math>n = 3</math>, then the integers are divided into the following sets:
+
== Making Computation Easier ==
 +
We don't always need to perform tedious computations to discover solutions to interesting problems.  If all we need to know about are remainders when integers are divided by <math>n</math>, then we can work directly with those remainders in modulo <math>n</math>.  This can be more easily understood with a few examples.
  
<math>\{ \ldots, -6, -3, 0, 3, 6, \ldots \}</math>
+
=== Addition ===
 +
==== Problem ====
 +
Suppose we want to find the [[units digit]] of the following [[sum]]:
  
<math>\{ \ldots, -5, -2, 1, 4, 7, \ldots \}</math>
 
  
<math>\{ \ldots, -4, -1, 2, 5, 8, \ldots \}</math>
+
<center><math>2403 + 791 + 688 + 4339.</math></center>
  
Notice that if we pick two numbers <math>a</math> and <math>b</math> from the same set, then <math>a</math> and <math>b</math> differ by a multiple of <math>3</math>, and therefore <math>a \equiv b</math> (mod <math>3</math>).
 
  
We sometimes refer to one of the sets above by choosing an element from the set, and putting a bar over it.  For example, the symbol <math>\overline{0}</math> refers to the set containing <math>0</math>; that is, the set of all integer multiples of <math>3</math>.  The symbol <math>\overline{1}</math> refers to the second set listed above, and <math>\overline{2}</math> the third.  The symbol <math>\overline{3}</math> refers to the same set as <math>\overline{0}</math>, and so on.
+
We could find their sum, which is <math>8221</math>, and note that the units digit is <math>1</math>.  However, we could find the units digit with far less calculation.
  
Instead of thinking of the objects <math>\overline{0}</math>, <math>\overline{1}</math>, and <math>\overline{2}</math> as sets, we can treat them as algebraic objects -- like numbers -- with their own operations of addition and multiplication.  Together, these objects form '''the integers modulo <math>3</math>,''' or <math>\mathbb{Z}_3</math>.  More generally, if <math>n</math> is a positive integer, then we can define
+
==== Solution ====
 +
We can simply add the units digits of the addends:
  
<math>\mathbb{Z}_n = \{\overline{0}, \overline{1}, \overline{2}, \ldots, \overline{n-1} \}</math>,
 
  
where for each <math>k</math>, <math>\overline{k}</math> is defined by
+
<center><math>3 + 1 + 8 + 9 = 21.</math></center>
  
<math>\overline{k} = \{ m \in \mathbb{Z} \mbox{ such that } m \equiv k \pmod{n} \}.</math>
 
  
=== Addition, Subtraction, and Multiplication Mod n ===
+
The units digit of this sum is <math>1</math>, which ''must'' be the same as the units digit of the four-digit sum we computed earlier.
  
We define addition, subtraction, and multiplication in <math>\mathbb{Z}_n</math> according to the following rules:
+
==== Why we only need to use remainders ====
 +
We can rewrite each of the integers in terms of multiples of <math>10</math> and remainders:<br>
 +
<math>2403 = 240 \cdot 10 + 3</math><br>
 +
<math>791 = 79 \cdot 10 + 1</math><br>
 +
<math>688 = 68 \cdot 10 + 8</math><br>
 +
<math>4339 = 433 \cdot 10 + 9</math>.<br>
 +
When we add all four integers, we get
  
<math>\overline{a} + \overline{b} = \overline{a+b}</math> for all <math>a, b \in \mathbb{Z}</math>.  (Addition)
 
  
<math>\overline{a} - \overline{b} = \overline{a-b}</math> for all <math>a, b \in \mathbb{Z}</math>.  (Subtraction)
+
<center><math> (240 \cdot 10 + 3) + (79 \cdot 10 + 1) + (68 \cdot 10 + 8) + (433 \cdot 10 + 9)</math></center>
  
<math>\overline{a} \cdot \overline{b} = \overline{ab}</math> for all <math>a, b \in \mathbb{Z}</math>.  (Multiplication)
+
<center><math>= (240 + 79 + 68 + 433) \cdot 10 + (3 + 1 + 8 + 9)</math></center>
  
So for example, if <math>n = 7</math>, then we have
 
  
<math>\overline{3} + \overline{2} = \overline{3+2} = \overline{5}</math>
+
At this point, we already see the units digits grouped apart and added to a multiple of <math>10</math> (which will not affect the units digit of the sum):
  
<math>\overline{4} + \overline{4} = \overline{4+4} = \overline{8} = \overline{1}</math>
+
<center><math>= 820 \cdot 10 + 21 = 8200 + 21 = 8221</math>.</center>
  
<math>\overline{4} \cdot \overline{3} = \overline{4 \cdot 3} = \overline{12} = \overline{5}</math>
+
==== Solution using modular arithmetic ====
 +
Now let's look back at this solution, using modular arithmetic from the start.  Note that<br>
 +
<math>2403 \equiv 3 \pmod{10}</math><br>
 +
<math>791 \equiv 1 \pmod{10}</math><br>
 +
<math>688 \equiv 8 \pmod{10}</math><br>
 +
<math>4339 \equiv 9 \pmod{10}</math><br>
 +
Because we only need the modulo <math>10</math> residue of the sum, we add just the residues of the summands:
  
<math>\overline{6} \cdot \overline{6} = \overline{6 \cdot 6} = \overline{36} = \overline{1}</math>
+
<center><math>2403 + 791 + 688 + 4339 \equiv 3 + 1 + 8 + 9 \equiv 21 \equiv 1 \pmod{10},</math></center>
 +
so the units digit of the sum is just <math>1</math>.
  
Notice that, in each case, we reduce to an answer of the form <math>\overline{k}</math>, where <math>0 \leq k < 7</math>.  We do this for two reasons:  to keep possible future calculations as manageable as possible, and to emphasize the point that each expression takes one of only seven (or in general, <math>n</math>) possible values.  (Some people find it useful to reduce an answer such as <math>\overline{5}</math> to <math>\overline{-2}</math>, which is negative but has a smaller absolute value.)
+
==== Addition rule ====
 +
In general, when <math>a, b, c</math>, and <math>d</math> are integers and <math>m</math> is a positive integer such that<br>
 +
<center><math>a \equiv c \pmod{m} </math></center>
 +
<center><math>b \equiv d \pmod{m} </math></center>
 +
the following is always true:
  
==== The Natural Appeal of Modular Arithmetic ====
+
<center><math>a + b \equiv c + d \pmod{m} </math>.</center>
 +
And as we did in the problem above, we can apply more pairs of equivalent integers to both sides, just repeating this simple principle.
  
Observe that we use modular arithmetic even when solving some of the most basic, everyday problems.  For example:
+
====Proof of the addition rule====
  
''Cody is cramming for an exam that will be held at 2 PM.  It is the morning of the day of the exam, and Cody did not get any sleep during the night.  He knows that it will take him exactly one hour to get to school from the time he wakes up, and he insists upon getting at least five hours of sleep. At what time in the morning should Cody stop studying and go to sleep?''
+
Let <math>a-c=m\cdot k</math>, and <math>b-d=m\cdot l</math> where <math>l</math> and <math>k</math> are integers.  
 +
Adding the two equations we get:
 +
<cmath>
 +
\begin{eqnarray*}
 +
mk+ml&=&(a-c)+(b-d)\\
 +
m(k+l)&=&(a+b)-(c+d)
 +
\end{eqnarray*}
 +
</cmath>
  
We know that the hours of the day are numbered from <math>1</math> to <math>12</math>, with hours having the same number if and only if they are a multiple of <math>12</math> hours apart.  So we can use subtraction mod <math>12</math> to answer this question.
+
Which is equivalent to saying <math>a+b\equiv c+d\pmod{m}</math>
  
We know that since Cody needs five hours of sleep plus one hour to get to school, he must stop studying six hours before the exam. We can find out what time this is by performing the subtraction
+
=== Subtraction ===
 +
The same shortcut that works with addition of remainders works also with subtraction.
  
<math>\overline{2} - \overline{6} = \overline{-4} = \overline{8}.</math>
+
==== Problem ====
 +
Find the remainder when the difference between <math>60002</math> and <math>601</math> is divided by <math>6</math>.
  
So Cody must quit studying at 8 AM.
+
==== Solution ====
 +
Note that <math>60002 = 10000 \cdot 6 + 2</math> and <math>601 = 100 \cdot 6 + 1</math>.  So,<br>
 +
<math>60002 \equiv 2 \pmod{6}</math><br>
 +
<math>601 \equiv 1 \pmod{6}</math><br>
 +
Thus,
 +
<center><math>60002 - 601 \equiv 2 - 1 \equiv 1 \pmod{6}, </math></center>
 +
so 1 is the remainder when the difference is divided by <math>6</math>. (Perform the subtraction yourself, divide by <math>6</math>, and see!)
  
Of course, we are able to perform calculations like this routinely without a formal understanding of modular arithmetic.  One reason for this is that the way we keep time gives us a natural model for addition and subtraction in <math>\mathbb{Z}_n</math>:  a "number circle."  Just as we model addition and subtraction by moving along a number line, we can model addition and subtraction mod <math>n</math> by moving along the circumference of a circle.  Even though most of us never learn about modular arithmetic in school, we master this computational model at a very early age.
+
==== Subtraction rule ====
 +
When <math>a, b, c</math>, and <math>d</math> are integers and <math>m</math> is a positive integer such that<br>
 +
<center><math>a \equiv c \pmod{m} </math></center>
 +
<center><math>b \equiv d \pmod{m} </math></center>
 +
the following is always true:
  
==== A Word of Caution ====
+
<center><math>a - b \equiv c - d \pmod{m} .</math></center>
  
Because of the way we define operations in <math>\mathbb{Z}_n</math>, it is important to check that these operations are well-defined.  This is because each of the sets that make up <math>\mathbb{Z}_n</math> contains many different numbers, and therefore has many different names.  For example, observe that in <math>\mathbb{Z}_7</math>, we have <math>\overline{1} = \overline{8}</math> and <math>\overline{2} = \overline{9}</math>It is reasonable to expect that if we perform the addition <math>\overline{8} + \overline{9}</math>, we should get the same answer as if we compute <math>\overline{1} + \overline{2}</math>, since we are simply using different names for the same objects.  Indeed, the first addition yields the sum <math>\overline{17} = \overline{3}</math>, which is the same as the result of the second addition.
+
=== Multiplication ===
 +
Modular arithmetic provides an even larger advantage when multiplying than when adding or subtractingLet's take a look at a problem that demonstrates the point.
  
The "Useful Facts" above are the key to understanding why our operations yield the same results even when we use different names for the same sets.  The task of checking that an operation or function is well-defined, is one of the most important basic techniques in [[abstract algebra]].
+
==== Problem ====
 +
Jerry has <math>44</math> boxes of soda in his truck.  The cans of soda in each box are packed oddly so that there are <math>113</math> cans of soda in each box.  Jerry plans to pack the sodas into cases of <math>12</math> cans to sell. After making as many complete cases as possible, how many sodas will Jerry have leftover?
  
=== Computation of Powers Mod n ===
+
==== Solution ====
 +
First, we note that this [[word problem]] is asking us to find the remainder when the product <math>44 \cdot 113</math> is divided by <math>12</math>.
  
The "exponentiation" property given above allows us to perform rapid calculations modulo <math>n</math>.  Consider, for example, the problem
+
Now, we can write each <math>44</math> and <math>113</math> in terms of multiples of <math>12</math> and remainders:<br>
 +
<math>44 = 3 \cdot 12 + 8</math><br>
 +
<math>113 = 9 \cdot 12 + 5</math><br>
 +
This gives us a nice way to view their product:
  
''What are the tens and units digits of <math>7^{1942}</math>?''
 
  
We could (in theory) solve this problem by trying to compute <math>7^{1942}</math>, but this would be extremely time-consuming.  Moreover, it would give us much more information than we need.  Since we want only the tens and units digits of the number in question, it suffices to find the remainder when the number is divided by <math>100</math>.  In other words, all of the information we need can be found using arithmetic mod <math>100</math>.
+
<cmath>44 \cdot 113 = (3 \cdot 12 + 8)(9 \cdot 12 + 5)</cmath>
 +
Using [[FOIL]], we get that this equals
 +
<cmath>(3 \cdot 9) \cdot 12^2 + (3 \cdot 5 + 8 \cdot 9) \cdot 12 + (8 \cdot 5)</cmath>
  
We begin by writing down the first few powers of <math>\overline{7}</math>:
 
  
<math>\overline{7}, \overline{49}, \overline{43}, \overline{1}, \overline{7}, \overline{49}, \overline{43}, \overline{1}, \ldots</math>
+
We can already see that each part of the product is a multiple of <math>12</math>, except the product of the remainders when each <math>44</math> and <math>113</math> are divided by 12.  That part of the product is <math>8 \cdot 5 = 40</math>, which leaves a remainder of <math>4</math> when divided by <math>12</math>.  So, Jerry has <math>4</math> sodas leftover after making as many cases of <math>12</math> as possible.
  
A pattern emerges!  We see that <math>7^4 = 2401 \equiv 1</math> (mod <math>100</math>).  So for any positive integer <math>k</math>, we have <math>7^{4k} = (7^4)^k \equiv 1^k \equiv 1</math> (mod <math>100</math>)In particular, we can write
+
==== Solution using modular arithmetic ====
 +
First, we note that<br>
 +
<math>44 \equiv 8 \pmod{12}</math><br>
 +
<math>113 \equiv 5 \pmod{12}</math><br>
 +
Thus,
 +
<center><math>44 \cdot 113 \equiv 8 \cdot 5 \equiv 40 \equiv 4 \pmod{12},</math></center>
 +
meaning there are <math>4</math> sodas leftoverYeah, that was much easier.
  
<math>7^{1940} = 7^{4 \cdot 485} \equiv 1</math> (mod <math>100</math>).
+
==== Multiplication rule ====
 +
When <math>a, b, c</math>, and <math>d</math> are integers and <math>m</math> is a positive integer such that<br>
 +
<center><math>a \equiv c \pmod{m} </math></center>
 +
<center><math>b \equiv d \pmod{m} </math></center>
 +
The following is always true:
  
By the "multiplication" property above, then, it follows that
+
<center><math>a \cdot b \equiv c \cdot d \pmod{m} </math>.</center>
  
<math>7^{1942} = 7^{1940} \cdot 7^2 \equiv 1 \cdot 7^2 \equiv 49</math> (mod <math>100</math>).
 
  
Therefore, by the definition of congruence, <math>7^{1942}</math> differs from <math>49</math> by a multiple of <math>100</math>Since both integers are positive, this means that they share the same tens and units digits.  Those digits are <math>4</math> and <math>9</math>, respectively.
+
=== Exponentiation ===
 +
Since [[exponentiation]] is just repeated multiplication, it makes sense that modular arithmetic would make many problems involving exponents easierIn fact, the advantage in computation is even larger and we explore it a great deal more in the [[intermediate modular arithmetic]] article.
  
==== A General Algorithm ====
+
Note to everybody: Exponentiation is very useful as in the following problem:
  
In the example above, we were fortunate to find a power of <math>7</math> -- namely, <math>7^4</math> -- that is congruent to <math>1</math> mod <math>100</math>.  What if we aren't this lucky? Suppose we want to solve the following problem:
+
==== Problem #1====
 +
What is the last digit of <math>(...((7)^7)^7)...)^7</math> if there are 1000 7s as exponents and only one 7 in the middle?
  
''What are the tens and units digits of <math>13^{404}</math>?''
+
We can solve this problem using mods. This can also be stated as <math>7^{7^{1000}}</math>.  After that, we see that 7 is congruent to -1 in mod 4, so we can use this fact to replace the 7s with -1s, because 7 has a pattern of repetitive period 4 for the units digit.  <math>(-1)^{1000}</math> is simply 1, so therefore <math>7^1=7</math>, which really is the last digit.
  
Again, we will solve this problem by computing <math>\overline{13}^{404}</math> modulo <math>100</math>.  The first few powers of <math>\overline{13}</math> are
+
==== Problem #2====
 +
What are the tens and units digits of <math>7^{1942}</math>?
  
<math>\overline{13}, \overline{69}, \overline{97}, \overline{61}, \overline{93}, \ldots</math>
+
We could (in theory) solve this problem by trying to compute <math>7^{1942}</math>, but this would be extremely time-consuming.  Moreover, it would give us much more information than we need.  Since we want only the tens and units digits of the number in question, it suffices to find the remainder when the number is divided by <math>100</math>.  In other words, all of the information we need can be found using arithmetic mod <math>100</math>.
  
This time, no pattern jumps out at us.  Is there a way we can find the <math>404^{th}</math> power of <math>\overline{13}</math> without taking this list all the way out to the <math>404^{th}</math> term -- or even without patiently waiting for the list to yield a pattern?
+
We begin by writing down the first few powers of <math>7</math> mod <math>100</math>:
  
Suppose we condense the list we started above; and instead of writing down all powers of <math>\overline{13}</math>, we write only the powers <math>\overline{13}^k</math>, where <math>k</math> is a power of <math>2</math>.  We have
+
<math>7, 49, 43, 1, 7, 49, 43, 1, \ldots</math>
  
<math>\overline{13}^1 = \overline{13}</math>
+
A pattern emerges!  We see that <math>7^4 = 2401 \equiv 1</math> (mod <math>100</math>).  So for any positive integer <math>k</math>, we have <math>7^{4k} = (7^4)^k \equiv 1^k \equiv 1</math> (mod <math>100</math>).  In particular, we can write
  
<math>\overline{13}^2 = \overline{69}</math>
+
<math>7^{1940} = 7^{4 \cdot 485} \equiv 1</math> (mod <math>100</math>).
  
<math>\overline{13}^4 = \overline{69}^2 = \overline{61}</math>
+
By the "multiplication" property above, then, it follows that
  
<math>\overline{13}^8 = \overline{61}^2 = \overline{21}</math>
+
<math>7^{1942} = 7^{1940} \cdot 7^2 \equiv 1 \cdot 7^2 \equiv 49</math> (mod <math>100</math>).
  
<math>\overline{13}^{16} = \overline{21}^2 = \overline{41}</math>
+
Therefore, by the definition of congruence, <math>7^{1942}</math> differs from <math>49</math> by a multiple of <math>100</math>.  Since both integers are positive, this means that they share the same tens and units digits.  Those digits are <math>4</math> and <math>9</math>, respectively.
  
<math>\overline{13}^{32} = \overline{41}^2 = \overline{81}</math>
+
==== Problem #3====
  
<math>\overline{13}^{64} = \overline{81}^2 = \overline{61}</math>
+
Can you find a number that is both a multiple of <math>2</math> but not a multiple of <math>4</math> and a perfect square?
  
<math>\overline{13}^{128} = \overline{61}^2 = \overline{21}</math>
+
No, you cannot. Rewriting the question, we see that it asks us to find an integer <math>n</math> that satisfies <math>4n+2=x^2</math>.
  
<math>\overline{13}^{256} = \overline{21}^2 = \overline{41}</math>
+
Taking mod <math>4</math> on both sides, we find that <math>x^2\equiv 2\pmod{4}</math>. Now, all we are missing is proof that no matter what <math>x</math> is, <math>x^2</math> will never be a multiple of <math>4</math> plus <math>2</math>, so we work with cases:
  
(Observe that this process yields a pattern of its own, if we carry it out far enough!)
+
<math>x\equiv 0\pmod{4}\implies x^2\equiv 0\pmod{4}</math>
  
Now, observe that, like any positive integer, <math>404</math> can be written as a sum of powers of two:
+
<math>x\equiv 1\pmod{4}\implies x^2\equiv 1\pmod{4}</math>
  
<math>404 = 256 + 128 + 16 + 4</math>
+
<math>x\equiv 2\pmod{4}\implies x^2\equiv 4\equiv 0\pmod{4}</math>
  
We can now use this powers-of-two expansion to compute <math>\overline{13}^{404}</math>:
+
<math>x\equiv 3\pmod{4}\implies x^2\equiv 9\equiv 1\pmod{4}</math>
  
<math>\overline{13}^{404} = \overline{13}^{256} \cdot \overline{13}^{128} \cdot \overline{13}^{16} \cdot \overline{13}^4 = \overline{41} \cdot \overline{21} \cdot \overline{41} \cdot \overline{61} = \overline{61}.</math>
+
This assures us that it is impossible to find such a number.
  
So the tens and units digits of <math>13^{404}</math> are <math>6</math> and <math>1</math>, respectively.
+
== Summary of Useful Facts ==
  
We can use this method to compute <math>M^e</math> modulo <math>n</math>, for any integers <math>M</math> and <math>e</math>, with <math>e > 0</math>.  The beauty of this algorithm is that the process takes, at most, approximately <math>2 \log_2 e</math> steps -- at most <math>\log_2 e</math> steps to compute the values <math>\overline{M}^k</math> for <math>k</math> a power of two less than <math>e</math>, and at most <math>\log_2 e</math> steps to multiply the appropriate powers of <math>\overline{M}</math> according to the binary representation of <math>e</math>.
+
Consider four integers <math>{a},{b},{c},{d}</math> and a positive integer <math>{m}</math> such that <math>a\equiv b\pmod {m}</math> and <math>c\equiv d\pmod {m}</math>. In modular arithmetic, the following [[identity | identities]] hold:
  
This method can be further refined using [[Euler's Totient Theorem]].
+
* Addition: <math>a+c\equiv b+d\pmod {m}</math>.
 +
* Subtraction: <math>a-c\equiv b-d\pmod {m}</math>.
 +
* Multiplication: <math>ac\equiv bd\pmod {m}</math>.
 +
* Division: <math>\frac{a}{e}\equiv \frac{b}{e}\pmod {\frac{m}{\gcd(m,e)}}</math>, where <math>e</math> is a positive integer that divides <math>{a}</math> and <math>b</math>.
 +
* Exponentiation: <math>a^e\equiv b^e\pmod {m}</math> where <math>e</math> is a positive integer.
  
  
Line 212: Line 300:
  
 
== Resources ==
 
== Resources ==
* The AoPS [http://www.artofproblemsolving.com/Books/AoPS_B_Item.php?page_id=10 Introduction to Number Theory] by [[Mathew Crawford]].
+
* The AoPS [http://www.artofproblemsolving.com/Store/viewitem.php?item=intro:nt Introduction to Number Theory] by [[Mathew Crawford]].
 
+
* The AoPS [http://www.artofproblemsolving.com/School/courseinfo.php?course_id=intro:numbertheory Introduction to Number Theory Course].  Thousands of students have learned more about modular arithmetic and [[problem solving]] from this 12 week class.
  
 
== See also ==
 
== See also ==
 
* [[Intermediate modular arithmetic]]
 
* [[Intermediate modular arithmetic]]
 
* [[Olympiad modular arithmetic]]
 
* [[Olympiad modular arithmetic]]
 +
 +
 +
[[Category:Introductory Mathematics Topics]]

Latest revision as of 13:43, 20 October 2020

Modular arithmetic is a special type of arithmetic that involves only integers. This goal of this article is to explain the basics of modular arithmetic while presenting a progression of more difficult and more interesting problems that are easily solved using modular arithmetic.


Introductory Video

https://youtu.be/7an5wU9Q5hk?t=777

Motivation

Let's use a clock as an example, except let's replace the $12$ at the top of the clock with a $0$.

[asy]picture pic; path a; a = circle((0,0), 100); draw (a); draw((0,0), linewidth(4));  pair b; b = (0,100);  for (int i = 0; i < 12; ++i) { label  (pic, (string) i, b); b = rotate(-30,(0,0)) * b; } pic = scale(0.8) * pic; add(pic);  draw ((0,0) -- 50*dir(90)); draw ((0,0) -- 70*dir(90));[/asy]

Starting at noon, the hour hand points in order to the following:

$1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, \ldots$


This is the way in which we count in modulo 12. When we add $1$ to $11$, we arrive back at $0$. The same is true in any other modulus (modular arithmetic system). In modulo $5$, we count


$0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, \ldots$


We can also count backwards in modulo 5. Any time we subtract 1 from 0, we get 4. So, the integers from $-12$ to $0$, when written in modulo 5, are


$3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0,$


where $-12$ is the same as $3$ in modulo 5. Because all integers can be expressed as $0$, $1$, $2$, $3$, or $4$ in modulo 5, we give these integers their own name: the residue classes modulo 5. In general, for a natural number $n$ that is greater than 1, the modulo $n$ residues are the integers that are whole numbers less than $n$:


$0, 1, 2, \ldots, n-1.$


This just relates each integer to its remainder from the Division Theorem. While this may not seem all that useful at first, counting in this way can help us solve an enormous array of number theory problems much more easily!

Residue

We say that $a$ is the modulo-$m$ residue of $n$ when $n\equiv a\pmod m$, and $0\le a<m$.

Congruence

There is a mathematical way of saying that all of the integers are the same as one of the modulo 5 residues. For instance, we say that 7 and 2 are congruent modulo 5. We write this using the symbol $\equiv$: In other words, this means in base 5, these integers have the same residue modulo 5:

$2\equiv 7\equiv 12 \pmod{5}.$


The (mod 5) part just tells us that we are working with the integers modulo 5. In modulo 5, two integers are congruent when their difference is a multiple of 5. In general, two integers $a$ and $b$ are congruent modulo $n$ when $a - b$ is a multiple of $n$. In other words, $a \equiv b \pmod{n}$ when $\frac{a-b}{n}$ is an integer. Otherwise, $a \not\equiv b \pmod{n}$, which means that $a$ and $b$ are not congruent modulo $n$.


Examples

  • $31 \equiv 1 \pmod{10}$ because $31 - 1 = 30$ is a multiple of $10$.


  • $43 \equiv 22 \pmod{7}$ because $\frac{43 - 22}{7} = \frac{21}{7} = 3$, which is an integer.


  • $8 \not\equiv -8 \pmod{3}$ because $8 - (-8) = 16$, which is not a multiple of $3$.


  • $91 \not\equiv 18 \pmod{6}$ because $\frac{91 - 18}{6} = \frac{73}{6}$, which is not an integer.


Sample Problem

Find the modulo $4$ residue of $311$.

Solution:

Since $311 \div 4 = 77$ R $3$, we know that

$311 \equiv 3 \pmod{4}$

and $3$ is the modulo $4$ residue of $311$.

Another Solution:

Since $311 = 300 + 11$, we know that

$311 \equiv 0+11 \pmod{4}$

We can now solve it easily

$11 \equiv 3 \pmod{4}$

and $3$ is the modulo $4$ residue of $311$

Another Solution:

We know $308$ is a multiple of $4$ since $8$ is a multiple of $4$. Thus, $311-308=3$ and $3$ is the modulo $4$ residue of $311$.

Making Computation Easier

We don't always need to perform tedious computations to discover solutions to interesting problems. If all we need to know about are remainders when integers are divided by $n$, then we can work directly with those remainders in modulo $n$. This can be more easily understood with a few examples.

Addition

Problem

Suppose we want to find the units digit of the following sum:


$2403 + 791 + 688 + 4339.$


We could find their sum, which is $8221$, and note that the units digit is $1$. However, we could find the units digit with far less calculation.

Solution

We can simply add the units digits of the addends:


$3 + 1 + 8 + 9 = 21.$


The units digit of this sum is $1$, which must be the same as the units digit of the four-digit sum we computed earlier.

Why we only need to use remainders

We can rewrite each of the integers in terms of multiples of $10$ and remainders:
$2403 = 240 \cdot 10 + 3$
$791 = 79 \cdot 10 + 1$
$688 = 68 \cdot 10 + 8$
$4339 = 433 \cdot 10 + 9$.
When we add all four integers, we get


$(240 \cdot 10 + 3) + (79 \cdot 10 + 1) + (68 \cdot 10 + 8) + (433 \cdot 10 + 9)$
$= (240 + 79 + 68 + 433) \cdot 10 + (3 + 1 + 8 + 9)$


At this point, we already see the units digits grouped apart and added to a multiple of $10$ (which will not affect the units digit of the sum):

$= 820 \cdot 10 + 21 = 8200 + 21 = 8221$.

Solution using modular arithmetic

Now let's look back at this solution, using modular arithmetic from the start. Note that
$2403 \equiv 3 \pmod{10}$
$791 \equiv 1 \pmod{10}$
$688 \equiv 8 \pmod{10}$
$4339 \equiv 9 \pmod{10}$
Because we only need the modulo $10$ residue of the sum, we add just the residues of the summands:

$2403 + 791 + 688 + 4339 \equiv 3 + 1 + 8 + 9 \equiv 21 \equiv 1 \pmod{10},$

so the units digit of the sum is just $1$.

Addition rule

In general, when $a, b, c$, and $d$ are integers and $m$ is a positive integer such that

$a \equiv c \pmod{m}$
$b \equiv d \pmod{m}$

the following is always true:

$a + b \equiv c + d \pmod{m}$.

And as we did in the problem above, we can apply more pairs of equivalent integers to both sides, just repeating this simple principle.

Proof of the addition rule

Let $a-c=m\cdot k$, and $b-d=m\cdot l$ where $l$ and $k$ are integers. Adding the two equations we get: \begin{eqnarray*} mk+ml&=&(a-c)+(b-d)\\ m(k+l)&=&(a+b)-(c+d) \end{eqnarray*}

Which is equivalent to saying $a+b\equiv c+d\pmod{m}$

Subtraction

The same shortcut that works with addition of remainders works also with subtraction.

Problem

Find the remainder when the difference between $60002$ and $601$ is divided by $6$.

Solution

Note that $60002 = 10000 \cdot 6 + 2$ and $601 = 100 \cdot 6 + 1$. So,
$60002 \equiv 2 \pmod{6}$
$601 \equiv 1 \pmod{6}$
Thus,

$60002 - 601 \equiv 2 - 1 \equiv 1 \pmod{6},$

so 1 is the remainder when the difference is divided by $6$. (Perform the subtraction yourself, divide by $6$, and see!)

Subtraction rule

When $a, b, c$, and $d$ are integers and $m$ is a positive integer such that

$a \equiv c \pmod{m}$
$b \equiv d \pmod{m}$

the following is always true:

$a - b \equiv c - d \pmod{m} .$

Multiplication

Modular arithmetic provides an even larger advantage when multiplying than when adding or subtracting. Let's take a look at a problem that demonstrates the point.

Problem

Jerry has $44$ boxes of soda in his truck. The cans of soda in each box are packed oddly so that there are $113$ cans of soda in each box. Jerry plans to pack the sodas into cases of $12$ cans to sell. After making as many complete cases as possible, how many sodas will Jerry have leftover?

Solution

First, we note that this word problem is asking us to find the remainder when the product $44 \cdot 113$ is divided by $12$.

Now, we can write each $44$ and $113$ in terms of multiples of $12$ and remainders:
$44 = 3 \cdot 12 + 8$
$113 = 9 \cdot 12 + 5$
This gives us a nice way to view their product:


\[44 \cdot 113 = (3 \cdot 12 + 8)(9 \cdot 12 + 5)\] Using FOIL, we get that this equals \[(3 \cdot 9) \cdot 12^2 + (3 \cdot 5 + 8 \cdot 9) \cdot 12 + (8 \cdot 5)\]


We can already see that each part of the product is a multiple of $12$, except the product of the remainders when each $44$ and $113$ are divided by 12. That part of the product is $8 \cdot 5 = 40$, which leaves a remainder of $4$ when divided by $12$. So, Jerry has $4$ sodas leftover after making as many cases of $12$ as possible.

Solution using modular arithmetic

First, we note that
$44 \equiv 8 \pmod{12}$
$113 \equiv 5 \pmod{12}$
Thus,

$44 \cdot 113 \equiv 8 \cdot 5 \equiv 40 \equiv 4 \pmod{12},$

meaning there are $4$ sodas leftover. Yeah, that was much easier.

Multiplication rule

When $a, b, c$, and $d$ are integers and $m$ is a positive integer such that

$a \equiv c \pmod{m}$
$b \equiv d \pmod{m}$

The following is always true:

$a \cdot b \equiv c \cdot d \pmod{m}$.


Exponentiation

Since exponentiation is just repeated multiplication, it makes sense that modular arithmetic would make many problems involving exponents easier. In fact, the advantage in computation is even larger and we explore it a great deal more in the intermediate modular arithmetic article.

Note to everybody: Exponentiation is very useful as in the following problem:

Problem #1

What is the last digit of $(...((7)^7)^7)...)^7$ if there are 1000 7s as exponents and only one 7 in the middle?

We can solve this problem using mods. This can also be stated as $7^{7^{1000}}$. After that, we see that 7 is congruent to -1 in mod 4, so we can use this fact to replace the 7s with -1s, because 7 has a pattern of repetitive period 4 for the units digit. $(-1)^{1000}$ is simply 1, so therefore $7^1=7$, which really is the last digit.

Problem #2

What are the tens and units digits of $7^{1942}$?

We could (in theory) solve this problem by trying to compute $7^{1942}$, but this would be extremely time-consuming. Moreover, it would give us much more information than we need. Since we want only the tens and units digits of the number in question, it suffices to find the remainder when the number is divided by $100$. In other words, all of the information we need can be found using arithmetic mod $100$.

We begin by writing down the first few powers of $7$ mod $100$:

$7, 49, 43, 1, 7, 49, 43, 1, \ldots$

A pattern emerges! We see that $7^4 = 2401 \equiv 1$ (mod $100$). So for any positive integer $k$, we have $7^{4k} = (7^4)^k \equiv 1^k \equiv 1$ (mod $100$). In particular, we can write

$7^{1940} = 7^{4 \cdot 485} \equiv 1$ (mod $100$).

By the "multiplication" property above, then, it follows that

$7^{1942} = 7^{1940} \cdot 7^2 \equiv 1 \cdot 7^2 \equiv 49$ (mod $100$).

Therefore, by the definition of congruence, $7^{1942}$ differs from $49$ by a multiple of $100$. Since both integers are positive, this means that they share the same tens and units digits. Those digits are $4$ and $9$, respectively.

Problem #3

Can you find a number that is both a multiple of $2$ but not a multiple of $4$ and a perfect square?

No, you cannot. Rewriting the question, we see that it asks us to find an integer $n$ that satisfies $4n+2=x^2$.

Taking mod $4$ on both sides, we find that $x^2\equiv 2\pmod{4}$. Now, all we are missing is proof that no matter what $x$ is, $x^2$ will never be a multiple of $4$ plus $2$, so we work with cases:

$x\equiv 0\pmod{4}\implies x^2\equiv 0\pmod{4}$

$x\equiv 1\pmod{4}\implies x^2\equiv 1\pmod{4}$

$x\equiv 2\pmod{4}\implies x^2\equiv 4\equiv 0\pmod{4}$

$x\equiv 3\pmod{4}\implies x^2\equiv 9\equiv 1\pmod{4}$

This assures us that it is impossible to find such a number.

Summary of Useful Facts

Consider four integers ${a},{b},{c},{d}$ and a positive integer ${m}$ such that $a\equiv b\pmod {m}$ and $c\equiv d\pmod {m}$. In modular arithmetic, the following identities hold:

  • Addition: $a+c\equiv b+d\pmod {m}$.
  • Subtraction: $a-c\equiv b-d\pmod {m}$.
  • Multiplication: $ac\equiv bd\pmod {m}$.
  • Division: $\frac{a}{e}\equiv \frac{b}{e}\pmod {\frac{m}{\gcd(m,e)}}$, where $e$ is a positive integer that divides ${a}$ and $b$.
  • Exponentiation: $a^e\equiv b^e\pmod {m}$ where $e$ is a positive integer.


Applications of Modular Arithmetic

Modular arithmetic is an extremely flexible problem solving tool. The following topics are just a few applications and extensions of its use:


Resources

See also

Invalid username
Login to AoPS