Modular Arithmetic

in

Let’s talk about modular arithmetic the math equivalent of that one friend who always shows up late but somehow manages to be on time according to their own watch. It’s a weird and wonderful world where numbers wrap around like a never-ending party loop.

So what is it, exactly? Well, let’s say you have two numbers: 10 and 5. If we divide 10 by 3 (which gives us a remainder of 1), then we can write that as an equation:

10 = 3 * 3 + 1

Now, imagine if instead of dividing all the way to get a whole number quotient, we just stopped at the remainder. That’s modular arithmetic! We call this operation “taking the remainder” or “finding the residue.” So in our example:

10 1 (modulo 3)

This means that when you divide 10 by 3, the result is equivalent to a remainder of 1. And here’s where it gets funky if we add or subtract multiples of 3 from either side of this equation, the result will still be congruent (meaning they have the same residue). For example:

– 7 + 10 = 17
– 17 0 (modulo 3) because when we divide 17 by 3, the remainder is 1 and then we add another multiple of 3 to get a whole number quotient. So in other words:
17 = 5 * 3 + 1
And if we subtract that same multiple (in this case, 5) from both sides:
0 = (-5) * 3 + 17
– This is why modular arithmetic can be so useful in computer science! It allows us to do calculations with numbers that wrap around like a circle. For example, if we have an array of integers and we want to find the index of a particular element (let’s say it’s at index 10), but our array only has room for up to 9 elements what can we do? Well, instead of trying to fit that extra element in there somehow, we can use modular arithmetic to “wrap around” the end of the array and find the equivalent index from the beginning. So if our array is:
[1, 2, 3, 4, 5]
And we want to find the index of element 10 (which doesn’t exist), we can use modular arithmetic to get an equivalent index that does exist:
10 = 9 * 1 + 1
So our “wrapped around” index would be:
(-1) * 1 + 9 (because if we subtract the multiple of 1 from both sides, it’s equivalent to adding a negative multiple)
– And that gives us an index of 8! This is just one example of how modular arithmetic can help us solve problems in computer science. It might seem like a weird and unnecessary concept at first glance, but once you get the hang of it, it’s actually pretty cool. So go ahead wrap your head around some modular math and see where it takes you!

SICORPS