I am trying to find the possible values of this expression.
(a mod 2*x)-(a mod x)
I suspect they might be 0 or x, but I'm really not sure. I can't seem to be able to write down a proper argument.
I am trying to find the possible values of this expression.
(a mod 2*x)-(a mod x)
I suspect they might be 0 or x, but I'm really not sure. I can't seem to be able to write down a proper argument.
You are correct that the possible values are 0 and x
, assuming that both a
and x
are positive. The logic is as follows.
Let a
have the form
a = p*x + b
Then it is easy to see that a mod x = b
.
For a mod 2*x
, if p = 2*r
(p
is even) then
a = 2*r*x + b = (2*x)*r + b
so that a mod 2*x = b
and p = 2*r + 1
(p
is odd) then
a = (2*r + 1)*x + b = 2*r*x + x + b = (2*x)*r + x + b
so that a mod 2*x = x + b
. Combining these results, the difference is either b - b = 0
(when p
is even) or (x + b) - b = x
(when p
is odd).