0

I saw the property of mod where

(A*B)%m = (A%m * B%m) %m

And this property is used in the below algorithm to find the mod of very large numbers.

1.Get one variable to store the answer initialized to zero.

2.Scan the string from left to right,

3.every time multiply the answer by 10 and add the next number and take the modulo and store this as the new answer.

But I'm unable to understand this algorithm . How the property is connected to the algorithm here?

It will be helpful if used an example to understand the underneath math behind the algorithm , for example 12345%11

Bill Dubuque
  • 272,048
  • 1
    It's just$$12345=(((1\times10+2)\times10+3)\times10+4)\times10+5\ ,$$calculating one step at a time and reducing modulo $11$ after every step. – David Sep 24 '22 at 04:49
  • how does (AB)%m = (A%m B%m) %m this property involves in here. whether it means that the modulo is distributed to all the products? – aravind ks Sep 24 '22 at 05:59
  • This is simply modular evaluation of a radix polynomial in nested Horner form, as described here and here in the linked dupes. – Bill Dubuque Sep 24 '22 at 07:55

1 Answers1

0

To compute $abcde\bmod m$, your algorithm will successively compute $0\bmod m$, $a\bmod m$, $ab\bmod m$, $abc\bmod m$, etc. At each step, it will use your property $(A*B)\bmod m=((A\bmod m)*(B\bmod m))\bmod m$, more specifically: $$\text{modulo }m,\quad A*10\equiv(A\bmod m)*10$$ e.g. if you already know $ab\equiv A\bmod m$, then $(ab*10)\equiv((A\bmod m)*10)\bmod m$ and $abc=(ab)*10+c\equiv((A\bmod m)*10)+c\bmod m$.

In your example:

  • [...]
  • $12\equiv1\bmod{11}$
  • $123=12*10+3\equiv1*10+3=13\equiv2\bmod{11}$
  • $1234=123*10+4\equiv2*10+4=24\equiv2\bmod{11}$
  • $12345=1234*10+5\equiv2*10+5=25\equiv3\bmod{11}$.
Anne Bauval
  • 34,650