Difference between revisions of "Modular arithmetic"

(ported divisibility rule proof to its own article)
(moving information to sub-articles)
Line 12: Line 12:
  
  
 
== Stuff to be reorganized ==
 
 
Given integers <math>a</math>, <math>b</math>, and <math>n</math>, with <math>n > 0</math>, we say that <math>a</math> is ''congruent to'' <math>b</math> ''modulo'' <math>n</math>, or <math>a \equiv b</math> (mod <math>n</math>), if the difference <math>{a - b}</math> is divisible by <math>n</math>.
 
 
For a given positive integer <math>n</math>, the relation <math>a \equiv b</math> (mod <math>n</math>) is an [[equivalence relation]] on the set of integers.  This relation gives rise to an algebraic structure called '''the integers modulo <math>n</math>''' (usually known as "the integers mod <math>n</math>," or <math>\mathbb{Z}_n</math> for short).  This structure gives us a useful tool for solving a wide range of number-theoretic problems, including finding solutions to [[Diophantine equation|Diophantine equations]], testing whether certain large numbers are prime, and even some problems in cryptology.
 
  
  
Line 31: Line 25:
 
* 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>.
 
* 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.
 
* Exponentiation: <math>a^e\equiv b^e\pmod {m}</math> where <math>e</math> is a positive integer.
 +
  
 
==== Examples ====
 
==== Examples ====
Line 39: Line 34:
  
 
* <math>7a\equiv 14\pmod {49}\implies a\equiv 2\pmod {7}</math>
 
* <math>7a\equiv 14\pmod {49}\implies a\equiv 2\pmod {7}</math>
 +
  
 
=== The Integers Modulo n ===
 
=== The Integers Modulo n ===
Line 83: Line 79:
  
 
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.)
 
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.)
 +
  
 
==== The Natural Appeal of Modular Arithmetic ====
 
==== The Natural Appeal of Modular Arithmetic ====
Line 99: Line 96:
  
 
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.
 
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.
 +
  
 
==== A Word of Caution ====
 
==== A Word of Caution ====
Line 105: Line 103:
  
 
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]].
 
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]].
 +
  
 
=== Computation of Powers Mod n ===
 
=== Computation of Powers Mod n ===
Line 127: Line 126:
  
 
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.
 
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.
 +
  
 
==== A General Algorithm ====
 
==== A General Algorithm ====
Line 175: Line 175:
  
 
This method can be further refined using [[Euler's Totient Theorem]].
 
This method can be further refined using [[Euler's Totient Theorem]].
 
=== Algebraic Properties of the Integers Mod n ===
 
 
The integers modulo <math>n</math> form an algebraic structure called a [[ring]] -- a structure in which we can add, subtract, and multiply elements.
 
 
Anyone who has taken a high school algebra class is familiar with several examples of rings, including the ring of integers, the ring of rational numbers, and the ring of real numbers.  The ring <math>\mathbb{Z}_n</math> has some algebraic features that make it quite different from the more familiar rings listed above.
 
 
First of all, notice that if we choose a nonzero element <math>\overline{a}</math> of <math>\mathbb{Z}_n</math>, and add <math>n</math> copies of this element, we get
 
 
<math>\overline{a} + \overline{a} + \cdots + \overline{a} = n \cdot \overline{a} = \overline{na} = \overline{0}</math>,
 
 
since <math>na</math> is a multiple of <math>n</math>.  So it is possible to add several copies of a nonzero element of <math>\mathbb{Z}_n</math> and get zero.  This phenomenon, which is called '''torsion''', does not occur in the reals, the rationals, or the integers.
 
 
Another curious feature of <math>\mathbb{Z}_n</math> is that a polynomial over <math>\mathbb{Z}_n</math> can have a number of roots greater than its degree.  Consider, for example, the polynomial congruence
 
 
<math>x^2 - 2x - 15 \equiv 0 \pmod{21}</math>.
 
 
We might be tempted to solve this congruence by factoring the expression on the left:
 
 
<math>(x - 5)(x + 3) \equiv 0 \pmod{21}</math>.
 
 
Indeed, this factorization yields two solutions to the congruence:  <math>x \equiv 5 \pmod{21}</math>, and <math>x \equiv -3 \equiv 18 \pmod{21}</math>.  (Note that two values of <math>x</math> that are congruent modulo <math>21</math> are considered the same solution.)
 
 
However, since <math>15 \equiv 99 \pmod{21}</math>, the original congruence is equivalent to
 
 
<math>x^2 - 2x - 99 \equiv 0 \pmod{21}</math>.
 
 
This time, factoring the expression on the left yields
 
 
<math>(x - 11)(x + 9) \equiv 0 \pmod{21}</math>.
 
 
And we find that there are two more solutions!  The values <math>x \equiv 11 \pmod{21}</math> and <math>x \equiv -9 \equiv 12 \pmod{21}</math> both solve the congruence.  So our congruence has at least four solutions -- two more than we might expect based on the degree of the polynomial.
 
 
Why do the "rules" of algebra that work so well for the real numbers seem to fail in <math>\mathbb{Z}_{21}</math>?  To understand this, let's take a closer look at the congruence
 
 
<math>(x - 5)(x + 3) \equiv 0 \pmod{21}</math>.
 
 
If we were solving this as an equation over the reals, we would immediately conclude that either <math>x - 5</math> must be zero, or <math>x + 3</math> must be zero in order for the product to equal zero.  However, this is not the case in <math>\mathbb{Z}_{21}</math>!  It is possible to multiply two nonzero elements of <math>\mathbb{Z}_{21}</math> and get zero.  For example, we have
 
 
<math>\overline{3} \cdot \overline{7} = \overline{0}</math>
 
 
<math>\overline{9} \cdot \overline{7} = \overline{0}</math>
 
 
<math>\overline{6} \cdot \overline{14} = \overline{0}</math>
 
 
But wait!  Suppose we take a close look at this last product, and we set <math>x - 5 \equiv 6 \pmod{21}</math> and <math>x + 3 \equiv 14 \pmod{21}</math>.  Then we have <math>x \equiv 11 \pmod{21}</math> -- another of the solutions of our congruence!  (One can check that the other two factorizations don't lead to any valid solutions; however, there are many other factorizations of zero that need to be checked.)
 
 
In the ring of real numbers, it is a well-known fact that if <math>ab = 0</math>, then <math>a = 0</math> or <math>b = 0</math>.  For this reason, we call the ring of real numbers a '''domain'''.  However, a similar fact does ''not'' apply in general in <math>\mathbb{Z}_n</math>; therefore, <math>\mathbb{Z}_n</math> is not in general a domain.
 
  
  
  
=== Topics ===
 
* [[Fermat's Little Theorem]]
 
* [[Euler's Totient Theorem]]
 
* [[Phi function]]
 
  
 
=== See also ===
 
=== See also ===
Line 235: Line 183:
 
* [[Number theory]]
 
* [[Number theory]]
 
* [[Quadratic residues]]
 
* [[Quadratic residues]]
 
 
== Miscellany ==
 
 
=== The binary operation "mod" ===
 
 
Related to the concept of congruence, mod <math>n</math> is the binary operation '''<math>a</math> mod <math>n</math>''', which is used often in computer programming.
 
 
Recall that, by the [[Division Algorithm]], given any two integers <math>a</math> and <math>n</math>, with <math>n > 0</math>, we can find integers <math>q</math> and <math>r</math>, with <math>0 \leq r < n </math>, such that <math>a = nq + r</math>.  The number <math>q</math> is called the ''quotient'', and the number <math>r</math> is called the ''remainder''.  The operation ''<math>a</math> mod <math>n</math>'' returns the value of the remainder <math>r</math>.  For example:
 
 
<math>15</math> mod <math>6 = 3</math>, since <math>15 = 6 \cdot 2 + 3</math>.
 
 
<math>35</math> mod <math>7 = 0</math>, since <math>35 = 7 \cdot 5 + 0</math>.
 
 
<math>-10</math> mod <math>8 = 6</math>, since <math>-10 = 8 \cdot -2 + 6</math>.
 
 
Observe that if <math>a</math> mod <math>n = r</math>, then we also have <math>a \equiv r</math> (mod <math>n</math>).
 
 
 
An example exercise with modular arithmetic:
 
 
'''Problem:'''
 
 
Let
 
 
<math>D=d_1d_2d_3d_4d_5d_6d_7d_8d_9</math>
 
 
be a nine-digit positive integer (each digit not necessarily distinct). Consider
 
 
<math>E=e_1e_2e_3e_4e_5e_6e_7e_8e_9</math>,
 
 
another nine-digit positive integer with the property that each digit ei when substituted for di makes the
 
modified D divisible by 7. Let
 
 
<math>F=f_1f_2f_3f_4f_5f_6f_7f_8f_9</math> be a third nine-digit positive integer with the same relation to E as E has to D.
 
 
Prove that every <math>di - fi</math> is divisible by 7.
 
 
 
 
'''Solution:'''
 
 
Any positive integer <math>D=d_1d_2d_3d_4d_5d_6d_7d_8d_9</math> can be expressed <math>(10^8)d_1+(10^7)d_2+...(10^0)d_9</math>.
 
 
Since '''10=3 mod 7''', and since it holds that if '''a=b mod c''' then <math>a^n=b^n</math> '''mod c''', then D can be expressed much more simply mod 7; that is,
 
<math>D= 2d1 +3d2 +1d3 -2d4 -3d5 -d6 +2d7 +3d8 +d9 </math>= '''x mod 7'''.
 
 
Each number in E must make the modified D equal 0 mod 7, so for each <math>d_i</math>, <math>e_i = \frac{x+7k}{c}-d_i</math>, where c is the coefficient of <math>d_i</math>
 
and k is an element of {-2,-1,0,1,2}. The patient reader should feel free to verify that this makes '''D = 0 mod 7'''.
 
 
In terms of <math>d_i</math> terms, then, we find each <math>c_ie_i = x - c_id_i + 7k</math>.
 
 
Then <math>E= 2e_1 +3e_2 +1e_3 -2e_4 -3e_5 -e_6 +2e_7 +3e_8 +e_9</math> '''mod 7''' can be expressed
 
<math>E= (x-2d_1)+(x-3d_2)+(x-d_3)+...+(x-d_9) = (9x)-D</math> '''mod 7 = (9x)- x = 8x = x mod 7'''.
 
(note that the 7s, which do not change the mod value, have been eliminated).
 
 
Each number in F must make the modified E equal '''0 mod 7''', so for each <math>e_i</math>, <math>f_i = \frac{x+7k_2}{c_i} -e_i = \frac{x+7k_2}{c_i} - (\frac{x+7k_1}{c_i} -d_i)</math>.
 
 
By design and selection of k, all <math>(f_i)</math> are integers, and <math>d_i - f_i</math> is always an integer because it is the difference of two integers.
 
 
<math>d_i - f_i = \frac{7k_2-7k_1}{c_i}</math>
 
 
<math>c_i</math> is a member of the set {1, 2, 3}. Since no <math>c_i</math> divides 7, 7 may be factored and <math>7\frac{k_2-k_1}{c_i} = d_i - f_i</math> is the product of two integers.
 
 
Let <math>A=\frac{k_2-k_1}{c_i}</math>    then <math>d_i - f_i =</math> '''7A mod 7 = 0 mod 7'''  for all <math>(d_i,f_i)</math>, QED.
 

Revision as of 16:24, 28 June 2006

Modular arithmetic is a special type of arithmetic that involves only integers. Since modular arithmetic is such a broadly useful tool in number theory, we divide its explanations into several levels:


Resources

Introductory Resources




Arithmetic Modulo n

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.


Examples

  • ${7}\equiv {1} \pmod {2}$
  • $49^2\equiv 7^4\equiv (1)^4\equiv 1 \pmod {6}$
  • $7a\equiv 14\pmod {49}\implies a\equiv 2\pmod {7}$


The Integers Modulo n

The relation $a \equiv b$ (mod $n$) allows us to divide the set of integers into sets of equivalent elements. For example, if $n = 3$, then the integers are divided into the following sets:

$\{ \ldots, -6, -3, 0, 3, 6, \ldots \}$

$\{ \ldots, -5, -2, 1, 4, 7, \ldots \}$

$\{ \ldots, -4, -1, 2, 5, 8, \ldots \}$

Notice that if we pick two numbers $a$ and $b$ from the same set, then $a$ and $b$ differ by a multiple of $3$, and therefore $a \equiv b$ (mod $3$).

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 $\overline{0}$ refers to the set containing $0$; that is, the set of all integer multiples of $3$. The symbol $\overline{1}$ refers to the second set listed above, and $\overline{2}$ the third. The symbol $\overline{3}$ refers to the same set as $\overline{0}$, and so on.

Instead of thinking of the objects $\overline{0}$, $\overline{1}$, and $\overline{2}$ 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 $3$, or $\mathbb{Z}_3$. More generally, if $n$ is a positive integer, then we can define

$\mathbb{Z}_n = \{\overline{0}, \overline{1}, \overline{2}, \ldots, \overline{n-1} \}$,

where for each $k$, $\overline{k}$ is defined by

$\overline{k} = \{ m \in \mathbb{Z} \mbox{ such that } m \equiv k \pmod{n} \}.$

Addition, Subtraction, and Multiplication Mod n

We define addition, subtraction, and multiplication in $\mathbb{Z}_n$ according to the following rules:

$\overline{a} + \overline{b} = \overline{a+b}$ for all $a, b \in \mathbb{Z}$. (Addition)

$\overline{a} - \overline{b} = \overline{a-b}$ for all $a, b \in \mathbb{Z}$. (Subtraction)

$\overline{a} \cdot \overline{b} = \overline{ab}$ for all $a, b \in \mathbb{Z}$. (Multiplication)

So for example, if $n = 7$, then we have

$\overline{3} + \overline{2} = \overline{3+2} = \overline{5}$

$\overline{4} + \overline{4} = \overline{4+4} = \overline{8} = \overline{1}$

$\overline{4} \cdot \overline{3} = \overline{4 \cdot 3} = \overline{12} = \overline{5}$

$\overline{6} \cdot \overline{6} = \overline{6 \cdot 6} = \overline{36} = \overline{1}$

Notice that, in each case, we reduce to an answer of the form $\overline{k}$, where $0 \leq k < 7$. 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, $n$) possible values. (Some people find it useful to reduce an answer such as $\overline{5}$ to $\overline{-2}$, which is negative but has a smaller absolute value.)


The Natural Appeal of Modular Arithmetic

Observe that we use modular arithmetic even when solving some of the most basic, everyday problems. For example:

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?

We know that the hours of the day are numbered from $1$ to $12$, with hours having the same number if and only if they are a multiple of $12$ hours apart. So we can use subtraction mod $12$ to answer this question.

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

$\overline{2} - \overline{6} = \overline{-4} = \overline{8}.$

So Cody must quit studying at 8 AM.

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 $\mathbb{Z}_n$: a "number circle." Just as we model addition and subtraction by moving along a number line, we can model addition and subtraction mod $n$ 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.


A Word of Caution

Because of the way we define operations in $\mathbb{Z}_n$, it is important to check that these operations are well-defined. This is because each of the sets that make up $\mathbb{Z}_n$ contains many different numbers, and therefore has many different names. For example, observe that in $\mathbb{Z}_7$, we have $\overline{1} = \overline{8}$ and $\overline{2} = \overline{9}$. It is reasonable to expect that if we perform the addition $\overline{8} + \overline{9}$, we should get the same answer as if we compute $\overline{1} + \overline{2}$, since we are simply using different names for the same objects. Indeed, the first addition yields the sum $\overline{17} = \overline{3}$, which is the same as the result of the second addition.

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.


Computation of Powers Mod n

The "exponentiation" property given above allows us to perform rapid calculations modulo $n$. Consider, for example, the problem

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 $\overline{7}$:

$\overline{7}, \overline{49}, \overline{43}, \overline{1}, \overline{7}, \overline{49}, \overline{43}, \overline{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.


A General Algorithm

In the example above, we were fortunate to find a power of $7$ -- namely, $7^4$ -- that is congruent to $1$ mod $100$. What if we aren't this lucky? Suppose we want to solve the following problem:

What are the tens and units digits of $13^{404}$?

Again, we will solve this problem by computing $\overline{13}^{404}$ modulo $100$. The first few powers of $\overline{13}$ are

$\overline{13}, \overline{69}, \overline{97}, \overline{61}, \overline{93}, \ldots$

This time, no pattern jumps out at us. Is there a way we can find the $404^{th}$ power of $\overline{13}$ without taking this list all the way out to the $404^{th}$ term -- or even without patiently waiting for the list to yield a pattern?

Suppose we condense the list we started above; and instead of writing down all powers of $\overline{13}$, we write only the powers $\overline{13}^k$, where $k$ is a power of $2$. We have

$\overline{13}^1 = \overline{13}$

$\overline{13}^2 = \overline{69}$

$\overline{13}^4 = \overline{69}^2 = \overline{61}$

$\overline{13}^8 = \overline{61}^2 = \overline{21}$

$\overline{13}^{16} = \overline{21}^2 = \overline{41}$

$\overline{13}^{32} = \overline{41}^2 = \overline{81}$

$\overline{13}^{64} = \overline{81}^2 = \overline{61}$

$\overline{13}^{128} = \overline{61}^2 = \overline{21}$

$\overline{13}^{256} = \overline{21}^2 = \overline{41}$

(Observe that this process yields a pattern of its own, if we carry it out far enough!)

Now, observe that, like any positive integer, $404$ can be written as a sum of powers of two:

$404 = 256 + 128 + 16 + 4$

We can now use this powers-of-two expansion to compute $\overline{13}^{404}$:

$\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}.$

So the tens and units digits of $13^{404}$ are $6$ and $1$, respectively.

We can use this method to compute $M^e$ modulo $n$, for any integers $M$ and $e$, with $e > 0$. The beauty of this algorithm is that the process takes, at most, approximately $2 \log_2 e$ steps -- at most $\log_2 e$ steps to compute the values $\overline{M}^k$ for $k$ a power of two less than $e$, and at most $\log_2 e$ steps to multiply the appropriate powers of $\overline{M}$ according to the binary representation of $e$.

This method can be further refined using Euler's Totient Theorem.



See also