Modulo (modular arithmetic)
At 3 in the afternoon you put a joint of meat in the oven, and it needs to roast for ten hours. When do you take it out? Three plus ten makes thirteen, but you will not find such a number on a clock face. The hour hand points to one, because after twelve it starts again, and the meat will be ready at one in the morning. Ordinary numbers are not enough for a clock; it works only with the remainders after division by twelve. This kind of counting is called modulo 12, and the whole branch of maths that counts this way is modular arithmetic.
The remainder after division and the modulo operation
The notation
So the hour hand stands at three.
The clock face teaches us an important rule: the remainder after division by n is always from 0 to n − 1. After division by twelve that is 0 to 11, after division by seven it is 0 to 6. That is why our clock face has a zero at the top, not a twelve.
What about negative numbers? One hour before midnight the hand stands at eleven. One step back from zero is 11, so
Be careful: a calculator on a computer will often display −1 here. Mathematicians, however, insist on a remainder from 0 to n − 1, so they keep adding n to a negative number until it falls into that range, for example −13 + 12 + 12 = 11.
Congruence
The hand points at three at 3 o’clock, at 15:00 and at 27 hours after midnight. The numbers 3, 15 and 27 leave the same remainder when divided by twelve, and as far as a clock is concerned they cannot be told apart. Such numbers are called congruent, and between them we write the symbol with three bars:
Put another way: two numbers are congruent modulo n if their difference is divisible by n. For example, 27 − 15 = 12.
What is this good for? Say it is Saturday. What day will it be in 100 days? A week has seven days, so the days of the week repeat every seven, and all we need is the remainder after division by seven:
Fourteen whole weeks change nothing about the day of the week, so Saturday stays Saturday. Two days are left over, so:
In 100 days it will be the same day as in two days, that is Monday. In a calendar layout, 100 falls in the same column as 2.
Adding and multiplying with remainders
The remainder of a sum is the same as the sum of the remainders (and then we take the remainder of that). The same is true of multiplication. Why? Multiples of seven stay multiples of seven when we add or multiply, so the remainder of the result depends only on the remainders. Do you want the remainder of the product 100 · 100 after division by seven? You do not have to multiply. A hundred leaves remainder 2, so the product leaves the same remainder as 2 · 2 = 4. Check: 10,000 = 1428 · 7 + 4. It agrees.
Thanks to this we can shrink the numbers as we go: after each step we keep only the remainder and carry on with that. Let’s try it out on a question: which digit does 7100 end in? The last digit is the remainder after division by ten, so we are looking for
The last digits go 7, 9, 3, 1 and then 7 again. As soon as we hit a one, the loop closes and the digits repeat every four.
Every fourth power ends in one: 74, 78, 712 and so on. The exponent 100 is divisible by four,
It is very tempting to divide the exponent by ten here, because we are working modulo 10. But ten has nothing to do with the length of the loop. What decides is the length of the loop, and that is four.
Fast exponentiation
Let’s tackle something bigger: what remainder does 31000 leave after division by seven? The number 31000 has 478 digits, and it would barely fit on a sheet of paper. We will proceed as we did with the sevens. We work out the remainders of the first powers of three after division by seven, each time multiplying the previous remainder by three:
Take the fourth remainder: the previous one was 6, 6 · 3 = 18 and
So 31000 = (36)166 · 34. Each group of six gives remainder 1, all 166 of them together 1166 = 1, and the fourth power is left over:
The answer is four, and we worked it out in our heads. It is no accident that modulo 7 we got one at exactly the sixth power. For every prime number p and every number a not divisible by it,
And what if the loop is hard to find, or enormous? A trick with repeated squaring helps. Let’s work out
We get the powers 31, 32, 34, 38 by squaring the previous result and each time taking the remainder:
Finally we multiply only the powers we need:
Just to be sure: 313 = 1,594,323 = 227,760 · 7 + 3. It works.
What makes this so powerful? In cryptography the exponents have hundreds of digits and the loop is so long that we will never find it. But each squaring doubles the exponent (1, 2, 4, 8, …), so after a thousand steps we are at the exponent 21000, a number with 302 digits. An exponent with 300 digits can therefore be handled in roughly two thousand multiplications, instead of multiplying by three 10300 times. A computer does that in a fraction of a second. The RSA cipher and Diffie–Hellman key exchange are built on this.
Going back is not nearly so easy. If we know that 3x leaves remainder 4 after division by seven, we find x = 4 by trial and error. With a huge prime, though, trying numbers would take longer than the age of the universe, and nobody knows a faster method.
Division and inverses
We are done with addition, subtraction and multiplication. What about division? The result 1/3 is not an integer, so we will not find it among the remainders. But dividing by three means multiplying by the number that gives one when multiplied by three. And modulo 7 there is such a number among the remainders:
So five is the inverse of three modulo 7, and instead of dividing by three we multiply by five. Modulo 7 every non-zero number has an inverse: 1 and 1, 2 and 4, 3 and 5, 6 and 6. You can easily check: 2 · 4 = 8 ≡ 1, 6 · 6 = 36 ≡ 1.
But it does not always work out. Let’s try to find the inverse of two modulo 6. The multiples of two are even, and an even number leaves remainder 0, 2 or 4 after division by six, never 1. The inverse of two modulo 6 does not exist.
In general, a number a has an inverse modulo n if and only if a and n are coprime, that is, if their greatest common divisor equals one. When n is a prime, no smaller positive number shares a divisor with it, and every non-zero number has an inverse. That is why in cryptography we so often calculate modulo a prime.
For small numbers you can find the inverse by trial and error; for large ones you work it out with the extended Euclidean algorithm.
Check digits
You will meet modulo outside maths too, whenever you copy a long number. In the Czech Republic everyone has a birth number (rodné číslo), a personal identification number, and the law on population records says that it is a ten-digit number divisible by eleven with no remainder. This holds for people born from 1954, apart from roughly a thousand exceptions from before 1985. Take the made-up birth number 900101/1239. We can tell it is divisible by eleven from the alternating digit sum:
Zero is divisible by eleven, so the number 9001011239 is too. But if you copied one digit wrongly, say 1238 instead of 1239, the number would change by 1, and the remainder after division by eleven would no longer be zero. A typo in one digit changes the number by c · 10k, where c is 1 to 9, and such a difference is not divisible by eleven, because eleven is a prime and divides neither c nor any power of ten. A government office or a computer program can therefore tell at once that a mistake has been made somewhere.
The same principle works for bank account numbers in the IBAN format, only modulo 97. The Czech National Bank gives CZ65 0800 0000 1920 0014 5399 as an example IBAN. The first four characters are moved to the end, letters are replaced by numbers (A = 10, B = 11 and so on, so C = 12 and Z = 35), and we get the number
Its remainder after division by 97 comes out as 1, and that is the condition for a valid IBAN. If you swap two adjacent digits in the account number, 1290 instead of 1920, the remainder comes out as 76 and a program immediately discovers that the number is wrong. The reason is the same as for the birth number: 97 is a prime, and the difference caused by a typo in one digit or by swapping two adjacent digits is never divisible by it.
Books have thirteen-digit ISBNs, and these are checked modulo 10. The digits are multiplied alternately by one and by three, and the sum must be divisible by ten. For the made-up ISBN 978-80-1234-567-8:
The number 130 is divisible by ten, so the ISBN is fine. A typo in one digit changes the sum by 1 to 9, or by three times that, and none of these is divisible by ten.
Where modulo is useful next
We have already met calendars and clocks. The Caesar cipher shifts letters modulo 26, and the affine cipher needs an inverse modulo 26 for decryption. RSA and Diffie–Hellman rest on fast exponentiation modulo a huge number, and elliptic curves, on which, for example, Bitcoin signatures are based, again calculate the coordinates of points using only remainders.