# How to evaluate an integral with the floor function?

I have the integral

$$\int_0^1\lfloor nx\rfloor\mathrm dx=\sum_{k=0}^{n-1}k\frac1n=\frac{n-1}2$$

where $n\in\Bbb N_{\ge 1}$ and $\lfloor{\cdot}\rfloor$ is the floor function. But when I try to evaluate in Mathematica with this code:

Refine[Integrate[Floor[n*x], {x, 0, 1}], n ∈ Integers && n > 0]


It stays unevaluated. How I can evaluate it?

One can use the Fourier series representation of the floor function: $\lfloor x\rfloor=\frac{1}{\pi}\sum _{k=1}^{\infty } \frac{1}{k}\sin (2 \pi k x)+x-\frac{1}{2}$. Integrating the $(x-1/2)$ we obtain your expected answer.

Integrate[(n x - 1/2), {x, 0, 1}]


$\frac{n}{2}-\frac{1}{2}$

For the rest, we can verify that it gives zero contribution.

res = 1/π Sum[Integrate[Sin[2 π k x n], {x, 0, 1}]/k, {k, 1,Infinity}]


$\frac{-3 \text{Li}_2\left(e^{-2 i n \pi }\right)-3 \text{Li}_2\left(e^{2 i n \pi }\right)+\pi ^2}{12 \pi ^2 n}$

FullSimplify[res, Assumptions -> n ∈ Integers]
(*0*)

FindSequenceFunction[
Table[
Integrate[Floor[n*x], {x, 0, 1}],
{n, 10}],
n] ==
Sum[k/n, {k, 0, n - 1}] ==
(n - 1)/2

(*  True  *)


If you specify a range for n, then you can use PiecewiseExand and integrate:

Integrate[PiecewiseExpand[Floor[n x], {0 < x < 1, 0 < n < 5}], {x, 0, 1}]


Maple gives the desired result straight away,

restart:
assume(n>0,n::integer):
int(floor(n*x),x=0..1); simplify(%) If we make the assumption $n>1$, then we get

assume(n>1,n::integer):
int(floor(n*x),x=0..1); 