Like many seemingly abstract mathematical concepts, the concept of modular arithmetic is rooted in some common everyday practices. Probably even now, while reading this topic, you can see the time displayed on the screen of your laptop or smartphone. You're used to the fact that in 24-hour format, three hours after 22:00 is 1:00, not 25:00. The hour is reset to zero whenever it reaches 24. 12-hour format is a bit more complicated in this regard — a minute after 12:59 pm, we see on our clock "1:00 pm". In a way, 12 plays the role of 0 in this notation.
The most obvious advantage of these resets is avoiding large numbers. We can say that modular arithmetic is a system for dealing with restricted ranges of integers. But it turns out that — when rigorously defined — it can be much more useful and lie in the heart of modern computer security algorithms.
Congruences
Definition. Let be a positive integer. We say that the integers and are congruent modulo if their difference is divisible by . We write . The number is called the modulus.
For instance, because More familiarly, minutes is 2 hours and 24 minutes.
Note that and can also be negative, and this fact also has parallels in everyday language. Asat 8:50 we can say "it's ten to nine".
Let's fix some modulus .
Recall that every integer can be represented as:As , we conclude that for every integer there is a unique integer congruent to modulo and lying in the range .
We have also denoted as . Sometimes we refer to as a residue of modulo .
Modulo reduction
Let's describe the simplest way to think about modular arithmetic. We do usual operations — addition, subtraction, and multiplication (we'll talk about division later), but replace all operands and all intermediate results with their residues modulo — that is, numbers from the range , congruent to these operands modulo . Sometimes such replacement is called reduction modulo . A real-world image for the reduction is the hand of a clock, wrapping around and returning to the same place again. Look at the Shepherd Gate Clock at the entrance to the Greenwich Observatory, remarkable for using the 24-hour dial starting at zero:
You can think about addition modulo 24 as moving the short hand of this clock. Suppose it's 14:00 (that is, 2 pm) now. To add 13 to 14 modulo 24, think where the short hand will be 13 hours later. It will point to 3:00 (3 am), right?
You can write this as .
Using the same trick to add 30 to 14 modulo 24, we see that the hand of the clock makes one complete turn around in 24 hours and then moves ahead for another 6 hours, giving us the result 20:00 (8 pm). So, .
Another example will use modulus 60 and contain both addition and multiplication:
We add to to obtain , then reduce and modulo , getting and correspondingly. Then we multiply by and reduce the result modulo .
In fact, we could as well calculate , reduce this number modulo and obtain . The final result doesn't depend on whether you reduced the intermediate results modulo or not. This follows from the substitution rules:
For example, these properties say that because and raising to power is a repeated multiplication. But it's much more convenient to replace not with an arbitrary number congruent to it, but with the smallest positive number with this property — with the residue of modulo :
So, . For a very large number (try to estimate the number of digits needed to write it down!), we can easily say what remainder it gives when divided by . Isn't it surprising?
Modular division
So far we discussed only modular addition and multiplication. What about the other two arithmetic operations?
Subtraction is a composition of multiplication by -1 and addition: so it has a similar property: Division is much more tricky. We cannot just apply to integers and the ordinary division and reduce the result modulo , as the result won't always be an integer. So we introduce the following definition.
Definition. We say that an integer is inverse to modulo , if .
Does every have an inverse modulo ? No, because we can rewrite as for some . So, having an inverse modulo means the existence of and such that:
The left-hand side of this equality must be divisible by , so there's no such and when .
On the other hand, when , you can find and using the extended Euclid's algorithm. Note that the algorithm won't necessarily give us from the range , but we know that's not a problem — we can always replace the result with its residue modulo .
Let's consider two examples. has no inverse modulo , as . But is relatively prime to and extended Euclid's algorithm gives us the following representation:so, is inverse to itself modulo .
So, modular division can be defined as multiplication by a modular inverse.
Equivalence classes
So far we regarded modular arithmetic as the system dealing with integers from a predefined range and replacing the integers leaving this range with their residues. But there is another possible interpretation, in which modular arithmetic deals with all the integers, but divides them into equivalence classes — that is, infinite subsets of integers, each containing only numbers congruent to one another modulo . For example, the equivalence classes modulo 3 are
{..., −9, −6, −3, 0, 3, 6, 9, ...}
{..., −8, −5, −2, 1, 4, 7, 10...}
{...−7, −4, −1, 2, 5, 8, 11, ...}
When doing operations, any member of an equivalence class is substitutable for any other. This follows from the substitution rules — look at them again. In our colorful notation, and are members of the same class and so share the same color, a similar observation is true for and . We know, for example, that 1 + 2 = 3, but the substitution rules allow us to generalize this statement and say that any "green" number plus any "blue" number gives a "red" number. We may state that "the green class plus the blue class equals the red class". Of course, usually, mathematicians don't assign colors to classes (although something like "indigo plus mulberry equals citrine would certainly sound interesting!), but refer to the classes through their representatives (arbitrary elements of the classes).
Conclusion
- Integers and are congruent modulo if their difference is divisible by .
- For every integer there is a unique integer congruent to modulo and lying in the range , known as 's residue modulo .
- Modular addition, multiplication, and subtraction can be viewed as corresponding ordinary operations with modulo reduction of results (that is, replacement of these results for their residues).
- Modular inverses, when they exist, can be found by Euclid's algorithm.
- Another interpretation of modular arithmetic involves equivalence classes of integers.