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:


 * $\ds \sum_{0 \mathop \le k \mathop < n} \floor {\dfrac {m k + x} n} = \dfrac {\paren {m - 1} \paren {n - 1} } 2 + \dfrac {d - 1} 2 + d \floor {\dfrac x d}$

where:
 * $\floor x$ denotes the floor of $x$
 * $d$ is the greatest common divisor of $m$ and $n$.

Proof
By definition of modulo 1:
 * $\ds \sum_{0 \mathop \le k \mathop < n} \floor {\dfrac {m k + x} n} = \sum_{0 \mathop \le k \mathop < n} \dfrac {m k + x} n - \sum_{0 \mathop \le k \mathop < n} \fractpart {\dfrac {m k + x} n}$

where $\fractpart y$ in this context denotes the fractional part of $y$.

First we have:

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

Thus:
 * $(1): \quad \ds \sum_{0 \mathop \le k \mathop < n} \floor {\dfrac {m k + x} n} = \dfrac {m \paren {n - 1} } 2 + x - S$

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

Let:

We have that:

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

and so:

Thus: