Computers usually store time as the number of seconds that have passed since the 1st of January, 1970 — the start of the “Unix era” — and use these timestamps in all computations that have to do with time.
We humans also keep track of time relative to some point in the past, which usually has a political or religious significance. For example, at the moment of writing, approximately 63882260594 seconds have passed since 1 AD — 6th century Eastern Roman monks’ best estimate of the day Jesus Christ was born.
But unlike computers, we do not always need all that information. Depending on the task at hand, the relevant part may be that it’s 2 pm right now, and it’s time to go to dinner; or that it’s Thursday, and so Subway’s sub of the day is an Italian BMT. Instead of the whole timestamp, we use its remainder containing just the information we need: it is much easier to deal with 1- or 2-digit numbers than 11-digit ones.
Problem. Today is Thursday. What day of the week will be exactly in a year?
If we enumerate each day of the week, starting with Monday, from to inclusive, Thursday gets number . To find out what day it is going to be in a year from now, we need to add to it and then reduce modulo . Conveniently, , so we know that it will be Friday unless it is a leap year (in which case it will be Saturday).
#Residues
Definition. Two integers and are said to be congruent modulo if divides their difference:
For example, the 42nd day of the year is the same weekday as the 161st since .
Congruence modulo is an equivalence relation that splits all integers into equivalence classes called residues. Each residue class modulo may be represented by any one of its members — although we commonly use the smallest nonnegative integer of that class (equal to the remainder for all nonnegative ).
Modular arithmetic studies these sets of residues, which are fundamental for number theory.
Problem. Our “week” now consists of days, and our year consists of days (no leap years). How many distinct days of the week there will be among one, two, three and so on whole years from now?
For simplicity, assume that today is Monday, so that the initial day number is zero, and after each year, it changes to
After years, it will be Since there are only days in a week, at some point, it will be Monday again, and the sequence of day numbers is going to cycle. The number of distinct days is the length of this cycle, so we need to find the smallest such thatFirst of all, if , it will be eternal Monday. Now, assuming the non-trivial case of :
- For a seven-day week, is prime. There is no smaller than such that is divisible by because can not be decomposed in such a product by the definition of primality. So, if is prime, we will cycle through all of weekdays.
- If is not prime, but is coprime with it (that is, and do not have common divisors), then the answer is still for the same reason: the divisors of do not help in zeroing out the product any faster.
- If and share some divisors, then it is only possible to get residues that are also divisible by them. For example, if the week is days long, and the year has or any other even number of days, then we will cycle through all even day numbers, and if the number of days is a multiple of , then we will only oscillate between and . Otherwise, we will go through all the remainders.
Therefore, in general, the answer is , where is the greatest common divisor of and .
#Fermat’s Theorem
Now, consider what happens if, instead of adding a number , we repeatedly multiply by it, writing out a sequence of
Again, since there is a finite number of residues, there is going to be a cycle. But what will its length be? Turns out, if is prime, it will span all non-zero residues.
Theorem. For any and a prime :
Proof. Let be the multinomial coefficient: the number of times the element appears after the expansion of . Then:Note that this is only true for prime . We can use this fact to test whether a given number is prime faster than by factoring it: we can pick a number at random, calculate , and check whether it is equal to or not.
This is called Fermat primality test, and it is probabilistic — only returning either “no” or “maybe” — since it may be that just happened to be equal to despite being composite, in which case you need to repeat the test with a different random until you are satisfied with the false positive probability.
Primality tests are commonly used to generate large primes (for cryptographic purposes). There are roughly primes among the first numbers (a fact that we are not going to prove), and they are distributed more or less evenly. One can just pick a random number from the required range, perform a primality check, and repeat until a prime is found, performing trials on average.
An extremely bad input to the Fermat test is the Carmichael numbers, which are composite numbers that satisfy for all relatively prime . But these are rare, and the chance of randomly bumping into it is low.
#Modular Division
Implementing most “normal” arithmetic operations with residues is straightforward. You only need to take care of integer overflows and remember to take modulo:
c = (a + b) % m;
c = (a - b + m) % m;
c = a * b % m;
But there is an issue with division: we can’t just bluntly divide two residues. For example, , but
To perform modular division, we need to find an element that “acts” like the reciprocal and multiply by it. This element is called a modular multiplicative inverse, and Fermat’s theorem can help us find it when the modulo is a prime. When we divide its equivalence twice by , we get: Therefore, is like for the purposes of multiplication, which is what we need from a modular inverse of .