Summation over k of Floor of mk+x over n

Theorem
Let $m, n \in \Z$ such that $n > 0$.

Let $x \in \R$.

Then:


 * $\displaystyle \sum_{0 \mathop \le k \mathop < n} \left \lfloor{\dfrac {m k + x} n}\right \rfloor = \dfrac {\left({m - 1}\right) \left({n - 1}\right)} 2 + \dfrac {d - 1} 2 + d \left \lfloor{\dfrac x d}\right \rfloor$

where:
 * $\left \lfloor{x}\right \rfloor$ denotes the floor of $x$
 * $d$ is the greatest common divisor of $m$ and $n$.

Proof
By definition of modulo 1:
 * $\displaystyle \sum_{0 \mathop \le k \mathop < n} \left \lfloor{\dfrac {m k + x} n}\right \rfloor = \sum_{0 \mathop \le k \mathop < n} \dfrac {m k + x} n - \sum_{0 \mathop \le k \mathop < n} \left\{ {\dfrac {m k + x} n}\right\}$

where $\left\{ {y}\right\}$ in this context denotes the fractional part of $y$.

First we have:

Let $S$ be defined as:
 * $\displaystyle S := \sum_{0 \mathop \le k \mathop < n} \left\{ {\dfrac {m k + x} n}\right\}$

Thus:
 * $\displaystyle (1): \quad \sum_{0 \mathop \le k \mathop < n} \left \lfloor{\dfrac {m k + x} n}\right \rfloor = \dfrac {m \left({n - 1}\right)} 2 + x - S$

Let $d = \gcd \left\{ {m, n}\right\}$.

Let:

We have that:

Thus $S$ consists of $d$ copies of the same summation:

and so:

Thus: