4

I was recently looking up solutions to taking the modulus of a very large number ($\sim22$ digits). Because computers can't handle this operation on such large numbers I went looking and found a novel solution to taking a number's modulus in general:

Say you want to calculate $12345 \bmod 7$ (which equals $4$). You could go digit by digit and do the following:

  1. $1 \bmod 7 = 1$.
  2. Take the prior result ($1$) and put it in front of the next digit ($2$). This gives you $12$: $12 \bmod 7 = 5$.
  3. Take the prior result ($5$) and put it in front of the next digit ($3$). This gives you $53$: $53 \bmod 7 = 4$.
  4. Take the prior result ($4$) and put it in front of the next digit ($4$). This gives you $44$: $44 \bmod 7 = 2$.
  5. Take the prior result ($2$) and put it in front of the next digit ($5$). This gives you $25$: $25 \bmod 7 = 4$.

And voilà - the answer is $4$, however I couldn't exactly understand how to prove why. Any insights are greatly appreciated!

Bonnaduck
  • 4,058
  • 3
    What is used here is just the fact that if $x=a \times 10^n + b$, with $b < 10^n$, and if $a' \equiv a [7]$, then $x \equiv a' \times 10^n + b$ (which is quite obvious). – TheSilverDoe Nov 07 '21 at 16:55
  • 1
    @TheSilverDoe Please don't use comments to write an answer. What you explained is already most of a good answer, so post it as such. – quarague Nov 07 '21 at 16:57
  • This is a special case of the universal divisibility test described in the Remark dupe (which is essentially an optimized modular form of longhand division while ignoring the quotients) – Bill Dubuque Nov 07 '21 at 19:16

1 Answers1

3

The following equivalences and summations:

\begin{align} 12345\pmod 7 &= \color{red}{1}0000 + 2345 \tag{step 1}\\ &= \color{red}{12}000 + 345 \tag{step 2}\\ &= 7000 + \color{red}{5}000 + 345\\ &\equiv 5000 + 345 \pmod 7\\ &= \color{red}{53}00 + 45\tag{step 3}\\ &= 4900 + \color{red}{4}00 + 45\\ &\equiv 400 + 45\pmod 7\\ &= \color{red}{44}0 + 5\tag{step 4}\\ &= 420 + \color{red}{2}0 + 5\\ &\equiv \color{red}{2}0 + 5 \pmod 7\\ &= 25\tag{step 5}\\ &\equiv 4 \pmod 7 \end{align}

are the same as your `digit by digit' calculation.

In a now deleted comment, a user suggested you compare this technique to long division. Note that we need only use the leading two digits to reduce the order of magnitude of the large number, step by step.

Edit: to answer OP's comment

David Diaz
  • 2,218