Modular Arithmetic - Algorithmica
Modular Arithmetic

Modular Arithmetic

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 00 to 66 inclusive, Thursday gets number 33. To find out what day it is going to be in a year from now, we need to add 365365 to it and then reduce modulo 77. Conveniently, 365mod7=1365 \bmod 7 = 1, 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 aa and bb are said to be congruent modulo mm if mm divides their difference:

m(ab)    ab(modm) m \mid (a - b) \; \Longleftrightarrow \; a \equiv b \pmod m

For example, the 42nd day of the year is the same weekday as the 161st since (16142)=119=17×7(161 - 42) = 119 = 17 \times 7.

Congruence modulo mm is an equivalence relation that splits all integers into equivalence classes called residues. Each residue class modulo mm may be represented by any one of its members — although we commonly use the smallest nonnegative integer of that class (equal to the remainder xmodmx \bmod m for all nonnegative xx).

Modular arithmetic studies these sets of residues, which are fundamental for number theory.

Problem. Our “week” now consists of mm days, and our year consists of aa 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 d0d_0 is zero, and after each year, it changes to

dk+1=(dk+a)modm d_{k + 1} = (d_k + a) \bmod m After kk years, it will be dk=kamodm d_k = k \cdot a \bmod m Since there are only mm 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 kk such that ka0(modm) k \cdot a \equiv 0 \pmod m

First of all, if a0a \equiv 0, it will be eternal Monday. Now, assuming the non-trivial case of a≢0a \not \equiv 0:

  • For a seven-day week, m=7m = 7 is prime. There is no kk smaller than mm such that kak \cdot a is divisible by mm because mm can not be decomposed in such a product by the definition of primality. So, if mm is prime, we will cycle through all of mm weekdays.
  • If mm is not prime, but aa is coprime with it (that is, aa and mm do not have common divisors), then the answer is still mm for the same reason: the divisors of aa do not help in zeroing out the product any faster.
  • If aa and mm share some divisors, then it is only possible to get residues that are also divisible by them. For example, if the week is m=10m = 10 days long, and the year has a=42a = 42 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 55, then we will only oscillate between 00 and 55. Otherwise, we will go through all the 1010 remainders.

Therefore, in general, the answer is mgcd(a,m)\frac{m}{\gcd(a, m)}, where gcd(a,m)\gcd(a, m) is the greatest common divisor of aa and mm.

#Fermat’s Theorem

Now, consider what happens if, instead of adding a number aa, we repeatedly multiply by it, writing out a sequence of

dn=anmodm d_n = a^n \bmod m

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 mm is prime, it will span all (m1)(m - 1) non-zero residues.

Theorem. For any aa and a prime pp:

apa(modp) a^p \equiv a \pmod p Proof. Let P(x1,x2,,xn)=k(xi!)P(x_1, x_2, \ldots, x_n) = \frac{k}{\prod (x_i!)} be the multinomial coefficient: the number of times the element a1x1a2x2anxna_1^{x_1} a_2^{x_2} \ldots a_n^{x_n} appears after the expansion of (a1+a2++an)k(a_1 + a_2 + \ldots + a_n)^k. Then: ap=(1+1++1+1a times)p =x1+x2++xa=pP(x1,x2,,xa)(by definition) =x1+x2++xa=pp!x1!x2!xa!(which terms will not be divisible by p?) P(p,0,,0)++P(0,0,,p)(everything else will be canceled) =a \begin{aligned} a^p &= (\underbrace{1+1+\ldots+1+1}_\text{$a$ times})^p & \\\ &= \sum_{x_1+x_2+\ldots+x_a = p} P(x_1, x_2, \ldots, x_a) & \text{(by definition)} \\\ &= \sum_{x_1+x_2+\ldots+x_a = p} \frac{p!}{x_1! x_2! \ldots x_a!} & \text{(which terms will not be divisible by $p$?)} \\\ &\equiv P(p, 0, \ldots, 0) + \ldots + P(0, 0, \ldots, p) & \text{(everything else will be canceled)} \\\ &= a \end{aligned}

Note that this is only true for prime pp. We can use this fact to test whether a given number is prime faster than by factoring it: we can pick a number aa at random, calculate apmodpa^{p} \bmod p, and check whether it is equal to aa or not.

This is called Fermat primality test, and it is probabilistic — only returning either “no” or “maybe” — since it may be that apa^p just happened to be equal to aa despite pp being composite, in which case you need to repeat the test with a different random aa until you are satisfied with the false positive probability.

Primality tests are commonly used to generate large primes (for cryptographic purposes). There are roughly nlnn\frac{n}{\ln n} primes among the first nn 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 O(lnn)O(\ln n) trials on average.

An extremely bad input to the Fermat test is the Carmichael numbers, which are composite numbers nn that satisfy an11(modn)a^{n-1} \equiv 1 \pmod n for all relatively prime aa. 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, 82=4\frac{8}{2} = 4, but

8mod52mod5=324 \frac{8 \bmod 5}{2 \bmod 5} = \frac{3}{2} \neq 4 To perform modular division, we need to find an element that “acts” like the reciprocal 1a=a1\frac{1}{a} = a^{-1} and multiply by it. This element is called a modular multiplicative inverse, and Fermat’s theorem can help us find it when the modulo pp is a prime. When we divide its equivalence twice by aa, we get: apa    ap11    ap2a1 a^p \equiv a \implies a^{p-1} \equiv 1 \implies a^{p-2} \equiv a^{-1} Therefore, ap2a^{p-2} is like a1a^{-1} for the purposes of multiplication, which is what we need from a modular inverse of aa.