icon

Maths in a minute: Modular arithmetic

Share this page
Clock

You do modular arithmetic several times every day when you are thinking about time. Imagine, for example, you're going on a train trip at 11pm that lasts three hours. What time will you arrive? Not at 11+3 = 14 o'clock, but at 2 o'clock in the morning. That's because, on a 12-hour clock, you start counting from the beginning again after you get to 12. (On a 24-hour clock, you start again after you get to 24.) So, on a 12-hour clock you have:

4 + 9 = 1,
7 + 7 = 2,
5 + 12 = 5,

and so on. When you are subtracting hours, you do the same but backwards:

4 - 7 = 9
1 - 11 = 2
6 - 12 = 6.

You could play the same game using other numbers, apart from 12 and 24, to define your cycle. For example, in modular arithmetic modulo 5 you have

4 + 2 = 1
3 + 4 = 2
1 - 4 = 2
3 - 5 = 3

These sums can be a little tedious to work out if you're counting on your fingers, but luckily there is a general method. Let's say you're doing arithmetic modulo some natural number p and you're looking at some other natural number x. To find the value of x modulo p (the value of x on a clock with p hours), compute the remainder when dividing x by p: that's your result.

This also works when x is negative (noting that the remainder is defined to be always positive). For example, for p = 12 and x = -3 we have

-3 = (-1) x 12 + 9,

so the remainder is 9. Therefore -3 modulo 12 is equal to 9. (If you use the modulus function in some computer languages you have to be a little careful though, as some return a different value for negative numbers.)

If you want to add or subtract two numbers module some natural number p, you simply work out the result, call it x, in ordinary arithmetic and then find the value of x modulo p.

There is clearly something very cyclical about modular arithmetic. Whatever number p defines your arithmetic, you can think of it as counting forward or backward in clock with p hours. To put this in technical maths language, modular arithmetic modulo p gives you a cyclic group of order p. You can find out more about group theory in this article and about modular arithmetic on our sister site NRICH.