Sunday, August 30, 2015

modular arithmetic - Insight into $pmod {2^n pm 1}$

When I was in elementary school I was taught that in order to find if a number is divisible by 3, if the number is big, we could add up all the separate digits that
made it up. If the result is divisible by 3, the original number is divisible by 3 as well. This was the basis for a nice insight.



First, what is the explanation for this. Let' s say we have the number 4728




$$
4728 \pmod 3 = (4720 + 8) \pmod 3 = (4720 \pmod 3 + 8 \pmod 3) \pmod 3 = [(472 \times 10) \pmod 3 + 8 \pmod 3] \pmod 3 = [([(472 \pmod 3)(10 \pmod 3)] \pmod 3) + 8 \pmod 3] \pmod 3 = [(472 \pmod 3) \pmod 3 + 8 \pmod 3] \pmod 3 = (472 \pmod 3 + 8 \pmod 3) \pmod 3 = [(472 + 8) \pmod 3] \pmod 3 = (472 \pmod 3 + 8 \pmod 3) \pmod 3 $$



... and then it becomes a recursive operation... till we get to



$$(4 + 7 + 2 + 8) \pmod 3 = 21 \pmod 3 = 0$$



$$4728 \pmod 3 = 21 \pmod 3 = 0$$




Of course, this trick is possible because $10 \pmod 3 = 1$... and this is when the insight begins.



Other Modulos



What would happen if we consider a number that is not 3. For starters, let's use 7:



$$
4728 \pmod 7 = (4720 + 8) \pmod 7 = (4720 \pmod 7 + 8 \pmod 7) \pmod 7 = ((472 \times 10) \pmod 7 + 8 \pmod 7) \pmod 7 = (([(472 \pmod 7)(10 \pmod 7)] \pmod 7) + 8 \pmod 7] \pmod 7 = [(472 \pmod 7) \times 3] + 8 \pmod 7] \pmod 7.
$$




In this case we won't be able to simplify to add up all the separate digits but we can see how the expression has already changed to involve $\pmod 7$ of the numbers of tens that we have in the original one, only that multiplied by 3 (because $10 \pmod 7 = 3$)... if we continue going down this path we will end up with this:



$$
4728 \pmod 7 = [([( 4 \times 3 ) + 7] \times 3 + 2) \times 3 + 8] \pmod 7 = 185 \pmod 7 = 3.
$$



If we tried to explain this, consider that for each 10 units that you are working with, you will compensate with 3 units to calculate modulo, because $10 - 7 = 3$.



So, take number 26. You take the 6 units, and then compensate for whatever is needed for the 20 units. Each 10 units will have to be compensated with 3 units added up to the 6 units. So,




$$
(6 + 2 \times 3) \pmod 7 = 12 \pmod 7 = 5
$$



Changing Bases



Let's consider $4729 \pmod 7$ but now, instead of using the decimal system, let's switch into octal system. 4729 becomes 011171. And $8 \pmod 7 = 1$, that will make it much simpler.



$$
011171 \pmod 7 = (011170 \pmod 7 + 1 \pmod 7) \pmod 7 = ((01117 \times 010) \pmod 7 + 1 \pmod 7) \pmod 7 = ([(01117 \pmod 7)(010 \pmod 7)] \pmod 7 + 1 \pmod 7) \pmod 7 = ([(01117 \pmod 7) \times 1] \pmod 7 + 1 \pmod 7) \pmod 7 = (01117 \pmod 7 + 1 \pmod 7) \pmod 7

$$



... and at this point we are again met with our recursive definition where we will develop $01117 \pmod 7$ using the same technique and end up with:



$$
(1 + 1 + 1 + 7 + 1) \pmod 7 = 11 \pmod 7 = 4.
$$



This all happens because, like when we were doing $\pmod 3$ and $10 \pmod 3 = 1$, when calculating $\pmod 7$, $8 \pmod 7 = 1$.




So, by changing the base of the numeric system for the analysis we were able to make an operation that involved multiplications by 3 and additions into a simple addition operation.



Subtracting also works



Consider $4729 \pmod {11}$



We could use base 16 for our analysis (which would be very comfortable when writing a program on a computer) and will use a "compensation factor" of 5 (because $16 - 11 = 5$).



4729 becomes 0x1279.




$$
4729 \pmod {11} = [([(1 \times 5 + 2) \times 5 + 7) \times 5 + 9] \pmod {11} = 219 \pmod {11} = 10
$$



Great... now let's do the same analysis but instead of using 16 as the base numeric system, let's go back to decimal.



You remember how I explained that for every 10 units we are compensating adding some units? When using $\pmod 3$, we were adding one unit per every 10, when doing $\pmod 7$ we were adding 3 units per every 10. If we are doing $\pmod {11}$ (which is bigger than 10), instead of adding units, we will compensate by subtracting units. Now, for every 10 units, instead of adding units, we substract 1 unit (because $10 - 11 = -1$)



Take $26 \pmod {11}$, we take 6 units and then compensate by subtracting 1 unit per every 10 units. So compensating for 20 should be -2




$$
26 \pmod {11} = (6 + -2) \pmod {11} = 4 \pmod {11} = 4.
$$



Let's go back to 4729:



$$
4729 \pmod {11} = ((((4 \times -1 + 7) \times -1 + 2) \times -1 + 9) \pmod {11} = (-4 + 7 - 2 + 9) \pmod {11} = 10 \pmod {11} = 10
$$




Right on target



Trying with $\pmod {15}$? Base is 16 and you have an addition-only operation.... trying with $\pmod {17}$ and using base 16 it becomes an addition-subtraction operation.



Might also consider bigger numbers



If you are doing $\pmod {15}$, you could take 16 as the base numeric system and use 1 as the compensation factor, but you could also use 256 as the numeric system (as in, go with 8 bits at at a time) and use 1 as the compensation factor too, because $256 \pmod {15} = 1$.



$$
4729 \pmod 15 = 0x1279 \pmod {15} = (0x12 + 0x79) \pmod {15} = (18 + 121) \pmod {15} = 139 \pmod {15} = 4.

$$



So, when working with computers, $\pmod {2^n \pm 1}$ should be a piece of cake... at least simpler than making a huge division and finding the remainder.



$\pmod {2^n \pm 1}$



For the sake of simplicity to explain the concept, as the details have already been covered, let's consider calculating $668371941 \pmod {255}$ and $668371941 \pmod {257}$. Using 256 as our base system:



$$
668371941 \pmod {255} = (0x27 + 0xd6 + 0x8b + 0xe5) \pmod {255} = (39 + 214 + 139 + 229) \pmod {255} = 621 \pmod {255} = 111

$$



Now $668371941 \pmod {257}$



$$
668371941 \pmod {257} = (-0x27 + 0xd6 - 0x8b + 0xe5) \pmod {257} = (-39 + 214 - 139 + 229) \pmod {257} = 265 \pmod {257} = 8
$$



So... had anybody noted this? If not, is this worth an article? Because I have no connection to the academia and I certainly lack the mathematical skills to write an article to prove it (other than what I posted here).

No comments:

Post a Comment

analysis - Injection, making bijection

I have injection $f \colon A \rightarrow B$ and I want to get bijection. Can I just resting codomain to $f(A)$? I know that every function i...