Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
1 Motivation
2 Residue
3 Congruence
3.1 Examples
3.2 Sample Problem
3.2.1 Solution:
3.2.2 Another Solution:
4 Making Computation Easier
4.1 Addition
4.1.1 Problem
4.1.2 Solution
4.1.3 Why we only need to use remainders
4.1.4 Solution using modular arithmetic
4.1.5 Addition rule
4.1.6 Proof of the addition rule
4.2 Subtraction
4.2.1 Problem
4.2.2 Solution
4.2.3 Subtraction rule
4.3 Multiplication
4.3.1 Problem
4.3.2 Solution
4.3.3 Solution using modular arithmetic
4.3.4 Multiplication rule
4.4 Exponentiation
4.4.1 Problem #1
4.4.2 Problem #2
4.4.3 Problem #3
5 Summary of Useful Facts
6 Applications of Modular Arithmetic
7 Resources
8 See also
Motivation
Let's use a clock as an example, except let's replace the at the top of the clock with a .
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 1/8
9/7/2019 Art of Problem Solving
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
integers their own name: the residue classes modulo 5. 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!
Residue
We say that is the modulo- residue of when , and .
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 : In other words, this means in base 5, these integers have the same
residue modulo 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 and are congruent modulo when is a multiple of . In other words,
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 2/8
9/7/2019 Art of Problem Solving
when is an integer. Otherwise, , which means that and are not congruent
modulo .
Examples
because is a multiple of .
Sample Problem
Solution:
Another Solution:
Addition
Problem
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 3/8
9/7/2019 Art of Problem Solving
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
The units digit of this sum is , which must be the same as the units digit of the four-digit sum we computed earlier.
.
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):
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:
Addition rule
In general, when , and are integers and is a positive integer such that
And as we did in the problem above, we can apply more pairs of equivalent integers to both sides, just repeating this simple principle.
Let , and where and are integers. Adding the two equations we get:
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 4/8
9/7/2019 Art of Problem Solving
Subtraction
The same shortcut that works with addition of remainders works also with subtraction.
Problem
Solution
Thus,
so 1 is the remainder when the difference is divided by . (Perform the subtraction yourself, divide by , and see!)
Subtraction rule
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 boxes of soda in his truck. The cans of soda in each box are packed oddly so that there are cans of soda in each
box. Jerry plans to pack the sodas into cases of 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 is divided by .
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 5/8
9/7/2019 Art of Problem Solving
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.
Thus,
meaning there are sodas leftover. Yeah, that was much easier.
Multiplication rule
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.
Problem #1
What is the last digit of if there are 1000 7s as exponents and only one 7 in the middle?
We could solve this problem using mods. This can also be stated as . 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. is simply 1, so
therefore , which really is the last digit.
Problem #2
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
.
A pattern emerges! We see that (mod ). So for any positive integer , we have
(mod ). In particular, we can write
(mod ).
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 6/8
9/7/2019 Art of Problem Solving
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.
Problem #3
Can you find a number that is both a multiple of but not a multiple of and a perfect square?
No, you cannot. Rewriting the question, we see that it asks us to find an integer that satisfies .
Taking mod on both sides, we find that . Now, all we are missing is proof that no matter what is, will
never be a multiple of plus , so we work with cases:
Addition: .
Subtraction: .
Multiplication: .
Divisibility rules
Linear congruences
Resources
The AoPS Introduction to Number Theory (http://www.artofproblemsolving.com/Store/viewitem.php?item=intro:nt) by Mathew
Crawford.
The AoPS Introduction to Number Theory Course (http://www.artofproblemsolving.com/School/courseinfo.php?course_id=intr
o:numbertheory). Thousands of students have learned more about modular arithmetic and problem solving from this 12 week
class.
See also
Intermediate modular arithmetic
Olympiad modular arithmetic
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 7/8
9/7/2019 Art of Problem Solving
https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction 8/8