Modular Arithmetic In today’s lesson we will: remember how we used to do division with remainder look at numbers that repeat learn why remainders are useful use remainders to solve problems
Division – what’s the answer? If I were to ask you, “What is 10 ÷ 4?”, what would you say? Now, you might say 2.5 Think back to your primary school days – all of the answers were whole numbers. The answer would have been “2 remainder 2”. For the rest of the lesson we’re going to look at remainders, and why they’re useful.
Modular Arithmetic Modular arithmetic is division with remainders, where we’re particularly interested in the remainder part – also called the modulus Why the remainder, though? That seems like the least important part of the answer. Think about dividing by four, how many possible remainders are there? The remainder can only be 0, 1, 2 or 3 (as 4 is divisible by four and the remainder is back to 0), so there are four possible remainders. Modular arithmetic is therefore a good way of creating a calculation with a limited number of outputs
Repeating Numbers If you look at each digit in a counter, after getting to 9 they go back to 0. Times and angles do a similar thing, although you possibly haven’t thought about it in this way. If it’s now 10 o’clock, what time will it be in four hours’ time? We know that a standard clock only shows twelve hours, but 10 + 4 = 14, so what do we do? We divide the time by twelve and use the remainder; 14 ÷ 12 is 1 remainder 2, so the time would be 2 o’clock
Repeating Numbers If we’re using a compass to find directions and we’re currently heading on a bearing of 280º, which way would we be facing if we turned 100º clockwise? Adding 280 and 100 gives us 380, but the compass only goes up to 360. Dividing 380 by 360 gives 1 remainder 20, so the new direction is 20º. It also means that 20º is the same as 380º, which is also the same as 740º and 1100º, etc. – they all point in the same direction. Modular arithmetic is therefore very useful for things that repeat or are cyclical in nature.
Example Imagine that you were creating a web-page for the French department and you wanted to cycle through red, white and blue for the heading colours. If we divide the number of the heading by three and use the remainder so that 0 = red, 1 = white and 2 = blue: Heading Number Remainder when Heading No. ÷ 3 Colour Red 1 White 2 Blue 3 4 5 6
Another example… When I was at school, the PE teachers used to put us in teams by lining us up and moving along the line, cycling through the team names, e.g. “A, B, C, A, B, C…”, etc. If I wanted to write a program that did this, where would I started? How many teams are there? There are three, so we could work through the people dividing their position by three and looking at the remainder, which can only be 0, 1 or 2 – i.e. one of three possible values.
Remainder if position is divided by 3 Team with this position Another example… students = ("Tom", "Dick", "Harry", "Rita", "Sue", "Bob", "Peter", "Paul", "Mary") teams = ("A", "B", "C") Student Student Position Remainder if position is divided by 3 Team with this position Tom A Dick 1 B Harry 2 C Rita 3 Sue 4 Bob 5 Peter 6 Paul 7 Mary 8