Modular arithmetic/Introduction
Modular arithmetic is a special type of arithmetic that involves only integers. Given integers , , and , with , we say that is congruent to modulo , or (mod ), if the difference is divisible by .
For a given positive integer , the relation (mod ) is an equivalence relation on the set of integers. This relation gives rise to an algebraic structure called the integers modulo (usually known as "the integers mod ," or for short). This structure gives us a useful tool for solving a wide range of number-theoretic problems, including finding solutions to Diophantine equations, testing whether certain large numbers are prime, and even some problems in cryptology.
Arithmetic Modulo n
Useful Facts
Consider four integers and a positive integer such that and . In modular arithmetic, the following identities hold:
- Addition: .
- Subtraction: .
- Multiplication: .
- Division: , where is a positive integer that divides and .
- Exponentiation: where is a positive integer.
Examples
The Integers Modulo n
The relation (mod ) allows us to divide the set of integers into sets of equivalent elements. For example, if , then the integers are divided into the following sets:
Notice that if we pick two numbers and from the same set, then and differ by a multiple of , and therefore (mod ).
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 refers to the set containing ; that is, the set of all integer multiples of . The symbol refers to the second set listed above, and the third. The symbol refers to the same set as , and so on.
Instead of thinking of the objects , , and 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 , or . More generally, if is a positive integer, then we can define
,
where for each , is defined by
Addition, Subtraction, and Multiplication Mod n
We define addition, subtraction, and multiplication in according to the following rules:
for all . (Addition)
for all . (Subtraction)
for all . (Multiplication)
So for example, if , then we have
Notice that, in each case, we reduce to an answer of the form , where . 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, ) possible values. (Some people find it useful to reduce an answer such as to , 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 to , with hours having the same number if and only if they are a multiple of hours apart. So we can use subtraction mod 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
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 : a "number circle." Just as we model addition and subtraction by moving along a number line, we can model addition and subtraction mod 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 , it is important to check that these operations are well-defined. This is because each of the sets that make up contains many different numbers, and therefore has many different names. For example, observe that in , we have and . It is reasonable to expect that if we perform the addition , we should get the same answer as if we compute , since we are simply using different names for the same objects. Indeed, the first addition yields the sum , 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 . Consider, for example, the problem
What are the tens and units digits of ?
We could (in theory) solve this problem by trying to compute , 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 . In other words, all of the information we need can be found using arithmetic mod .
We begin by writing down the first few powers of :
A pattern emerges! We see that (mod ). So for any positive integer , we have (mod ). In particular, we can write
(mod ).
By the "multiplication" property above, then, it follows that
(mod ).
Therefore, by the definition of congruence, differs from by a multiple of . Since both integers are positive, this means that they share the same tens and units digits. Those digits are and , respectively.
A General Algorithm
In the example above, we were fortunate to find a power of -- namely, -- that is congruent to mod . What if we aren't this lucky? Suppose we want to solve the following problem:
What are the tens and units digits of ?
Again, we will solve this problem by computing modulo . The first few powers of are
This time, no pattern jumps out at us. Is there a way we can find the power of without taking this list all the way out to the 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 , we write only the powers , where is a power of . We have
(Observe that this process yields a pattern of its own, if we carry it out far enough!)
Now, observe that, like any positive integer, can be written as a sum of powers of two:
We can now use this powers-of-two expansion to compute :
So the tens and units digits of are and , respectively.
We can use this method to compute modulo , for any integers and , with . The beauty of this algorithm is that the process takes, at most, approximately steps -- at most steps to compute the values for a power of two less than , and at most steps to multiply the appropriate powers of according to the binary representation of .
This method can be further refined using Euler's Totient Theorem.
Algebraic Properties of the Integers Mod n
The integers modulo 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 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 of , and add copies of this element, we get
,
since is a multiple of . So it is possible to add several copies of a nonzero element of and get zero. This phenomenon, which is called torsion, does not occur in the reals, the rationals, or the integers.
Another curious feature of is that a polynomial over can have a number of roots greater than its degree. Consider, for example, the polynomial congruence
.
We might be tempted to solve this congruence by factoring the expression on the left:
.
Indeed, this factorization yields two solutions to the congruence: , and . (Note that two values of that are congruent modulo are considered the same solution.)
However, since , the original congruence is equivalent to
.
This time, factoring the expression on the left yields
.
And we find that there are two more solutions! The values and 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 ? To understand this, let's take a closer look at the congruence
.
If we were solving this as an equation over the reals, we would immediately conclude that either must be zero, or must be zero in order for the product to equal zero. However, this is not the case in ! It is possible to multiply two nonzero elements of and get zero. For example, we have
But wait! Suppose we take a close look at this last product, and we set and . Then we have -- 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 , then or . For this reason, we call the ring of real numbers a domain. However, a similar fact does not apply in general in ; therefore, is not in general a domain.
Theorems and Applications
Divisibility Tests
Modular arithmetic is the basis for several well-known divisibility tests. Consider, for example, the test for divisibility by :
Let be a positive integer. Then is divisible by if and only if the sum of the base-ten digits of is divisible by .
Arithmetic mod can be used to give an easy proof of this criterion:
Suppose that the base-ten representation of is
,
where is a digit for each . Then the numerical value of is given by
.
Now we know that, since , we have (mod ). So by the "exponentiation" property above, we have (mod ) for every .
Therefore, by repeated uses of the "addition" and "multiplication" properties, we can write
(mod ).
Therefore, we have
(mod ).
That is, differs from the sum of its digits by a multiple of . It follows, then, that is a multiple of if and only if the sum of its digits is a multiple of .
A virtually identical argument shows that a positive integer is divisible by if and only if the sum of its base-ten digits is divisible by . We can also use the technique shown above to devise a test for divisibility by .
Resources
- The AoPS Introduction to Number Theory by Mathew Crawford.