I have been taking a break from calculus to do some review for myself before I continue posting more explanations. In the mean time, I'll be sure to post about some other topics. Recently, I ended up having to do some modular arithmetic for a program I was building. Although, I knew how it worked from taking discrete math a few years back, building this program showed me one practical use among many for modular arithmetic. Before I explain that, let me first talk about what it is and how it works.
So, what is modular arithmetic? Basically, its a system of arithmetic where from $0$ to $n$, the numbers loop back to $0$ when they reach $n$, that is to say the result of a an expression using modulo can only be $[0, n)$. There probably exists several better ways to say this but, for now lets just go with that.
This can be better demonstrated using a clock (a quite common example when explaining this). On a clock we have the numbers $1 - 12$ and when we get to twelve o' clock, we loop back around to one. So in this case, we loop around after $12$ numbers so we have a modulus of $12$, we'll refer to the modulus as $n$. So, for example when it is 13:00 on the 24-hr clock, we can find the time on our standard 12-hr clock by using mod, i.e. $13 \% 12 = 1$ (We will be using the $\%$ to represent modulo or mod going forward). Continuing that example, $14 \% 12 = 2$ and so on. We can continue this pattern around the clock until and the numbers will just keep looping, i.e. $24 \% 12 = 0$ or midnight and $25 \% 12 = 1$.
At this point you may have realized that we are actually just getting the remainder of the number $\div n$. Let's call that number $b$ as we continue. We now have $a \equiv b \% n$ or $a$ is congruent to $b (mod n)$. This leads to the idea that $b - a$ is divisible by $n$. You should now have a basic idea of how modular arithmetic works.
Anyways, I paused in the middle of writing this and forgot what direction I was going so, I guess I'm going to stop here. In the project I did, we used mod to confine objects on the screen to a digital canvas. That is to say, "newObjectPosition = currentObjectPosition (mod screenDimensions)" in a sort of pseudo code representation. On my next post, I'll try to do it all in one session (or at least keep my thoughts saved somewhere) and, as usual, any questions, comments and/or suggestions are appreciated.
No comments:
Post a Comment