Modular Arithmetic
Last updated
Last updated
Modular arithmetic is "clock math" - that is, when numbers wrap around back to 0 if they get too big. You could think about it like a remainder: for example can be read as "what is the remainder of 21 when it is divided by 10?" (it's 1, by the way.)
This is an important concept in many aspects of computer science, namely cryptography and error correction among many others.
If d divides x and d divides y, then d divides (y-x). (Reminder: )
Modular Equivalence: If you're looking at a clock and it becomes 25:00, you know it's actually the same as 1:00. Even if they're technically not the same number, they can be treated the same way.
More formally: x is congruent to y modulo m: ****
Important Notation Distinction: is the class of numbers that follow the mod rule . It can be used to write equivalences (). However, is just a number (the remainder when dividing x by m).
Greatest Common Denominator (GCD Mod Corollary): Modular arithmetic can be used to identify an important property of the GCD, which is that .
Given that , . This result shouldn't be too surprising, and suggests that adding a constant value to both sides won't change the congruence, just like any other equation.
Now, let's explore three famous algorithms for computing useful information using modular arithmetic: Euclid's Algorithm for GCD and inverses, the Chinese Remainder Theorem, and Fermat's Little Theorem.
Base Case: If y is 0, then any value for x is the GCD since everything can divide 0 to get 0.
Inductive Case: Proof of the GCD Mod Corollary.
For an example of a computation, check out the Extended Algorithm section below (the computation is extremely similar).
Great! We got the GCD. So what?
Remember that if the GCD of x and m is 1, then there is an inverse of x. In more concrete terms, we can state Euclid's Extended GCD Theorem (Bezout's Theorem) as such:
The process can be tedious to compute by hand, but here's a nice video that walks through that process:
The Chinese Remainder Theorem (CRT) guarantees existence and uniqueness of a solution to a system of modular congruences. More formally stated:
Alternative Statement of CRT:
For an application of Fermat's Little Theorem, head over to RSA Cryptography!
Multiplication works pretty much how you'd expect it to work after seeing how addition works. Formally defined, if , then . Intuitively, this means that if you want to multiply a big number, you can take the mod of the big number before multiplying it, and that will be equivalent to multiplying before taking the mod.
As an example, let's try to figure out what day it is in 8 years. We know that there will be 2 leap years with 366 days each, and 6 normal years with 365 days each. We don't really feel like computing , so we can instead take the mod of 365 and 366 first. This yields the much simpler expression . Therefore, the day in 8 years will be one day after today.
In normal number spaces, the multiplicative inverse of x is a y such that . This concept still applies to modular arithmetic!
If we have , then the multiplicative inverse is defined as a number y such that .
For example, let's take a look at . We can multiply both sides by 2 in order to get . At this point, we can use the to reduce the 8 into a 1 and the 10 into a 3, resulting in .
There are some values where it's impossible to get an equivalence into the form . This usually happens when there is a common factor (like ). In other words, if the greatest common divisor of x and m is 1, then x has a multiplicative inverse modulo m. (x is relatively prime to y).
Euclid's Algorithm is a recursive procedure for calculating the greatest common denominator. Remember that by the GCD Mod Corollary. We can prove that this works using induction:
This is a rather efficient algorithm: at every iteration, the value of x and y decrease dramatically- at least by a factor of 2. This makes it (in other words, we need one division for each bit that is needed to represent ).
In other words, the GCD can be written as a scalar multiple of x and y. Since we remember that the definition of the inverse is that for some integers a and b, Euclid's Extended Theorem checks out for showing that the inverse exists if the GCD is 1.
There is a unique solution such that , , and . Here are a few ways to word it (see which one clicks better):
Let be positive coprime integers. (Any two of them must be relatively prime.) Then, for any combination of integers , a unique x exists such that for all .
Let be pairwise co-prime, i.e. and are co-prime for all . The Chinese Remainder Theorem (CRT) tells us that there exist solutions to the following system of congruences:
Not only do we know that there is a unique solution , but we can actually write out its exact value! Here it is:
In this sum, , and is the product of all primes . This sum is congruent to for all valid values of .
The Chinese Remainder Theorem often ties well together with the Extended Euclidean Algorithm, since we would need to find lots of inverse mods for each . Also like the Extended Euclidean Algorithm, it's very hard to demonstrate the computation on a static webpage so here's another good video walkthrough!
Fermat's Little Theorem (not to be confused with Fermat's Last Theorem) makes the observation that exponentiation is periodic when modulo is done by a prime number. This makes it reasonable to compute obscenely large numbers, like , when in a mod space.
For prime and an integer , .
If we multiply both sides by , we can actually drop the restriction to and allow it to be any integer. This version is sometimes more useful than the normal definition:
For prime and any integer , .