Evaluate $\int_0^1 \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$ for $a,b\in\mathbb{Z}^+$ Evaluate $$\int_0^1 \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$$
where $a$ and $b$ are positive integers and $\{x\}:=x-\lfloor x\rfloor$
WLOG, we may assume that $a\le b$. Via the substitution $x \mapsto x/a$, we obtain
$$\int_0^a \left(\{x\}-\frac{1}{2}\right)\left(\left\{\frac{bx}{a}\right\}-\frac{1}{2}\right) dx$$
I know that we should partition the interval $(0,1)$ in such a way that the values of $\{x\}$ and $\{bx/a\}$ are constant in each subinterval. How do I think of such a partition? I also tried to express $a=gu$ and $b=gv$ where $u:=\gcd(a,b)$. But then again I face the same issue of partitioning $(0,1)$
 A: The answer is $\color{blue}{d^2/(12ab)}$ where $\color{blue}{d=\gcd(a,b)}$. A reasonably short solution is based on $$\newcommand{\saw}[1]{\left(\!\left(#1\right)\!\right)}\saw{x}:=\begin{cases}\{x\}-1/2,&x\notin\mathbb{Z}\\\hfill 0,\hfill&x\in\mathbb{Z}\end{cases}=-\sum_{n=1}^\infty\frac{\sin 2n\pi x}{n\pi}$$ in $L^2(0,1)$; the same holds if $x$ is replaced by $ax$ or $bx$, hence Parseval's theorem yields $$\int_0^1\saw{ax}\saw{bx}dx=\frac1{2\pi^2}\sum_{\substack{m,n>0\\am=bn}}\frac1{mn}=\frac1{2\pi^2}\sum_{k=1}^\infty\frac1{(bk/d)(ak/d)}=\frac{d^2}{12ab}$$ ($am=bn$ holds if and only if $m=bk/d$ and $n=ak/d$ for some $k\in\mathbb{Z}^+$).
There's also an elementary one, using $\saw{nx}=\sum_{k=0}^{n-1}\saw{x+k/n}$ for $n\in\mathbb{Z}^+$, so that $$\int_0^1\saw{ax}\saw{bx}dx=\sum_{m=0}^{a-1}\sum_{n=0}^{b-1}f(m/a,n/b),\\f(u,v):=\int_0^1\saw{x+u}\saw{x+v}dx=f(u-v,0),\\f(t,0)=\frac1{12}-\frac{t(1-t)}2\quad\text{for}\quad 0\leqslant t\leqslant 1.$$
A: This is a brute force approach:
$$\int_0^1 \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$$
Break the interval up into $ab$ parts to avoid the switch:
$$\sum_{k=1}^{ab}\int_{(k-1)/(ab)}^{k/(ab)} \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$$
Now $\{ax\}$ is just a straight line segment with a slope of $a$ starting at a height of $(k \bmod b)/b$.
$$\sum_{k=1}^{ab}\int_{0}^{1/(ab)} \left( \frac{k \bmod b}{b} + xa -\frac{1}{2}\right)\left(\frac{k \bmod a}{a} + xb -\frac{1}{2}\right) dx$$
$$\frac{1}{12a^2b^2} \sum_{k=1}^{ab} \left(\begin{array} {rl} 
     &(k \bmod a)(k \bmod b)\cdot 12
\\ + &(k \bmod a)(6 - 6b)
\\ + &(k \bmod b)(6 - 6a)
\\ + &3ab-3b-3a+4
\end{array}\right)$$
And now just a number theory problem.  Using $d=\gcd(a,b)$
$$\sum_{k=1}^{ab} (k \bmod a)(k \bmod b)
 = d\sum_{0 \le p < a \\ 0 \le q < b \\ p \equiv q \pmod d } pq
 = \frac{ab(d^2+3ab-3b-3a+2)}{12}
$$
$$
 \sum_{k=1}^{ab} (k \bmod a)
= b\sum_{k=0}^{a-1} k
= \frac{ab(a-1)}{2}
$$
So altogether:
$$\frac{1}{12a^2b^2} \left(\begin{array} {l} 
     \frac{ab(d^2+3ab-3b-3a+2)}{12}\cdot 12
\\ + \frac{ab(a-1)}{2}(6 - 6b)
\\ + \frac{ab(b-1)}{2}(6 - 6a)
\\ + ab(3ab-3b-3a+4)
\end{array}\right)$$
Which is:
$$\frac{d^2}{12ab}$$
