How do you calculate $a$ mod $b$ when $a < b$

104 Views Asked by At

For example, I know $13 \pmod 4$ is $1$, but how do I calculate $4\pmod{13}$?

2

There are 2 best solutions below

0
On

In general, given two integers $a,b$, when you want to calculate $a\pmod b$, you have to use the euclidean division. This statement says that if $b\neq 0$, there are unique integers $q,r$ such that $a=bq+r$ and $0\le r<|b|$. Hence $a\equiv r\pmod b$.

In your particular case, $a=4$, $b=13$, and $4=13.0+4$, i.e, $q=0$ and $r=4$, then $4\equiv 4\pmod {13}$.

0
On

If $\,a\ge0\,$ then $a\bmod b\,$ may be computed by repeatedly subtracting $b$ from $a$ until you land in the sought remainder interval $[0,\,b\!-\!1].\,$ If $\,0\le a<b\,$ then it already lies in that interval so it is the remainder, so no subtractions are needed. Then $\ a = 0\cdot b + a\,$ is the division with remainder. (When $\,a<0\,$ then repeatedly add $\,b).$