Let \
denote integer division (the C# /
operator between two int
s) and let /
denote usual math division. Then, if x,y,z
are positive integers and we are ignoring overflow,
(x \ y) \ z
= floor(floor(x / y) / z) [1]
= floor((x / y) / z) [2]
= floor(x / (y * z))
= x \ (y * z)
where
a \ b = floor(a / b)
The jump from line [1]
to line [2]
above is explained as follows. Suppose you have two integers a
and b
and a fractional number f
in the range [0, 1)
. It is straightforward to see that
floor(a / b) = floor((a + f) / b) [3]
If in line [1]
you identify a = floor(x / y)
, f = (x / y) - floor(x / y)
, and b = z
, then [3]
implies that [1]
and [2]
are equal.
You can generalize this proof to negative integers (still ignoring overflow), but I'll leave that to the reader to keep the point simple.
On the issue of overflow - see Eric Lippert's answer for a good explanation! He also takes a much more rigorous approach in his blog post and answer, something you should look into if you feel I'm being too hand-wavy.