4

Can I simplify this formula to use only one module operation?

$$(x + (y \text{ mod } z)) \text{ mod }z$$

Intuitively I simplified it so:

$$(x + y) \text{ mod } z$$

This seems to do the same as the original. However, I don't know why it works nor if this is right.

0x2b3bfa0
  • 143
  • 5
  • 2
    Taking the remainder mod $z$ once or any number of times $n$ ($\geq1$) yields the same result. So taking the remainder once for $x$ and twice for $y$ (your first line), is the same as taking the remainder only once for $x$ and once for $y$ (your second one) – Evariste Feb 07 '17 at 13:25
  • 1
    If you have an expression built using constants, variables, $+$ and $\times$, taking the remainder modulo some number $n$ at the end is the same things as taking the remainder at the end and anywhere else you want (i.e. if you take the remainder at the end, taking it anywhere is doesn't change the result). But that's only true mathematically, on a computer that represents integers modulo $2^{32}$, taking the remainder more often can allow you to avoid overflows. – xavierm02 Feb 07 '17 at 13:39
  • 1
    For example: $((2^{31}% 3) \times (2^{31}% 3))% 3 = (2 \times 2)%3 = 1$ while $(2^{31} \times 2^{31})%3 = 0%3 = 0$ (because all operations are implicitly mod $2^{32}$). – xavierm02 Feb 07 '17 at 13:44

5 Answers5

5

Mathematically, this is correct. Let $y$ = $mz+y'$, with $0 {\le}y'{\lt}z$. Then $x+y$ = $x+y'+mz$, so $x+y$ and $x+y'$ clearly have the same remainder modulo $z$.

However, if you are performing this calculation on a computer, beware: If $x$ and $y$ are of integer type (signed or unsigned), it is possible that adding $x$ to $y$ overflows while adding $x$ to $y'$ does not, which may impact the result.

PMar
  • 62
  • 1
  • I'm very happy with all the answers, however I'll accept this because is the one that fits more what I asked. – 0x2b3bfa0 Feb 07 '17 at 13:58
2

You are indeed right. Define $x, y$:
$x,y=nz+a$, for $0\leq a<z$ Plug in the original equation: $$(n_xz+a_x)+(n_yz+a_y)\mod{z}$$ Add both terms: $$n_{sum}z+a_x+a_y\mod{z}$$ First term cancels: $$a_x+a_y\mod{z}$$ The same obviously happens if we cancel $n_yz$ first: $$(n_xz+a_x)+(n_yz+a_y\mod{z})\mod{z}$$ $$=n_xz+a_x+a_y\mod{z}$$ $$=a_x+a_y\mod{z}$$

Zonko
  • 322
  • 1
  • 13
1

Yes it works.

(x + (y % z)) % z

= x % z + y % z % z .......(1)

Taking modulo twice or once of any number don't makes change.

So y % z % z = y % z

From (1),

= x % z + y % z

= (x + y) % z

1

The way you are writing this, it looks like you are actually talking about the operator % in some c-like programming language. In that case, be warned that if negative numbers are involved, the % operator can yield negative results (see https://en.wikipedia.org/wiki/Modulo_operation for details). With negative results, the two terms can produce different values that are the same mod z.

For example, my Visual Studio for C# says

$(3 + (-5 \% 3)) \% 3 = 1$, but $ (3 + (-5)) \% 3 = -2$.

Of course $1 \equiv -2 \pmod 3$, but if you compare values in your program, then they will compare as different.

Ingix
  • 14,494
1

$$(x + (y \text{ mod } z)) \text{ mod }z = $$ $(y \text{ mod } z) \implies y=qz + r \implies r = y - qz = (y \text{ mod } z)$ $$(x + (y - qz)) \text{ mod }z =$$ $$ (x + y - qz) \text{ mod }z =$$ $(x + y - qz) \text{ mod }z \implies (x + y - qz) = nz + p \implies p= (x + y - qz)-nz $ $$ (x + y - qz)-nz=$$ $$ x + y - (q -n)z =$$ $(q -n) = m$ $$ x + y + mz$$