Modular arithmetic/Introduction
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.
Contents
The Basic Idea
Let's use a clock as an example, except let's replace the at the top of the clock with a . Starting at noon, the hour hand points in order to the following:
This is the way in which we count in modulo 12. When we add to , we arrive back at . The same is true in any other modulus (modular arithmetic system). In modulo , we count
We can also count backwards in modulo 5. Any time we subtract 1 from 0, we get 4. So, the integers from to , when written in modulo 5, are
where is the same as in modulo 5. Because all integers can be expressed as , , , , or in modulo 5, we give these give integers their own name: the modulo 5 residues. In general, for a natural number that is greater than 1, the modulo residues are the integers that are whole numbers less than :
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!
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 :
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:
In general, two integers and are congruent modulo when is a multiple of . In other words, when is an integer. Otherwise, , which means that and are not congruent modulo .
Examples
- because is a multiple of
- because , which is an integer.
- because , which is not a multiple of .
- because , which is not an integer.
Sample Problem
Find the modulo residue of .
Solution:
Since R , we know that
and is the modulo residue of .
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 , then we can work directly with those remainders in modulo . This can be more easily understood with a few examples.
Addition
Problem
Suppose we want to find the units digit of the following sum:
We could find their sum, which is , and note that the units digit is . However, we could find the units digit with far less calculation.
Solution
We can simply add the units digits of the summands:
The units digit of this sum is , 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 and remainders:
When we add all four integers, we get
At this point we already see the units digits grouped apart and added to a multiple of (which will not affect the units digit of the sum):
Solution using modular arithmetic
Now let's look back at this solution, using modular arithmetic from the start. Note that
Because we only need the modulo residue of the sum, we add just the residues of the summands:
so the units digit of the sum is just .
Addition rule
In general, when , and are integers and is a positive integer such that
the following is always true:
.
And as we did in the problem above, we can apply more pairs of equivalent integers to both sides, just repeating this simple principle.
Subtraction
The same shortcut that works with addition of remainders works also with subtraction.
Problem
Find the remainder when the difference between and is divided by .
Solution
Note that and . So,
Thus,
so 1 is the remainder when the difference is divided by . (Perform the subtraction yourself, divide by , and see!)
Subtraction rule
When , and are integers and is a positive integer such that
the following is always true:
.
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 oas 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 is divided by .
Now, we can write each and in terms of multiples of and remainders:
This gives us a nice way to view their product:
We can already see that each part of the product is a multiple of except the product of the remainders when each and are divided by 12. That part of the product is , which leaves a remainder of when divided by . So, Jerry has sodas leftover after making as many cases of as possible.
Solution using modular arithmetic
First, we note that
Thus,
meaning there are sodas leftover. Yeah, that was much easier.
Multiplication rule
When , and are integers and is a positive integer such that
the following is always true:
.
Summary of 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.
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 mod :
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 mod are
This time, no pattern jumps out at us. Is there a way we can find the power of mod 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 mod , we write only the powers , where is a power of . We have the following (all congruences are modulo ):
(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 modulo 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.
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
- The AoPS Introduction to Number Theory by Mathew Crawford.
- The AoPS Introduction to Number Theory Course. Hundreds of students have learned more about modular arithmetic and problem solving from this 12 week class.