TITLE: Sum of remainders of Triangular numbers
QUESTION [8 upvotes]: Let $p=8k+7$ be a prime number, prove that:
$$\sum_{k=1}^{p-1} \left\{ \frac{2T(k)}{p}\right\} = \sum_{k=1}^{p-1} \left\{ \frac{k}{p}\right\}$$
, where $T(k)$ is the $k-$th Triangular number and $\{\frac{k}{p}\}$ is the decimal part of $\frac{k}{p}$.

It's fairly easy to notice that the RHS is equal to $\frac{p-1}{2}$, as all the fractions are less than 1, but I'm having troubles with the LHS. It's easy to notice that the terms are symmetric wrt the $\frac{p-1}{2}$ term, but that's all I found. I tried writing $2T(n) = n(n+1) = n^2 + n = (n+1)^2 - (n+1)$, but none of this helped me.
Also it's easy to notice that: $\{\frac{i}{p}\} = \frac{x}{p}$, where $x$ is the remainder when $i$ is divided by $p$. I tried a lot of examples, but I can't see a correlation between these numbers. Also as the sum of the $x$'s is less than $p^2$ I tried working modulo $p^2$ and proving that their sum is equal to $\frac{p(p-1)}{2}$, but again to no avail.
In fact I can't make use of the fact that $p$ is of the given form, as it seems that for primes other than those from the given form the identity doesn't hold.

REPLY [3 votes]: Let $p$ be a prime number then
$$\sum_{k=1}^{p-1} \left\{ \frac{k^2+k}{p}\right\} = \frac{1}{p}\sum_{j=1}^{p-1} j\left({4j+1\over p}\right)+\frac{p-1}{2}.$$
In fact, let $r_n(m)$ be the remainder of the division of $m$ by $n$ then
$$\sum_{k=1}^{p-1} \left\{ \frac{k^2+k}{p}\right\}
={1\over p}\sum_{k=1}^{p-1} r_p(k^2+k)$$
Note that $r_p(k^2+k)=j$ for some $j\in\{1,\dots,p-1\}$ iff $k^2+k=j$ (mod $p$), that is iff $\Delta=1+4j$ is a square modulo $p$ iff $\left({4j+1\over p}\right)=1$ where
$\left({\cdot \over p}\right)$ is the Legendre symbol. Therefore the number of
$k\in\{1,\dots,p-1\}$ such that $r_p(k^2+k)=j$ is $\left(\left({4j+1\over p}\right)+1\right)$ (it gives $0$ or $2$) and
$$\sum_{k=1}^{p-1} r_p(k^2+k)=
\sum_{j=1}^{p-1} j\left(\left({4j+1\over p}\right)+1\right)=
\sum_{j=1}^{p-1} j\left({4j+1\over p}\right)+\frac{p^2-p}{2}.
$$

Now let us assume that $p$ is congruent to 7 modulo 8. By the above formula, in order to prove the proposed problem, it suffices to show that
  $$S_1=\sum_{j=0}^{p-1} j\left({4j+1\over p}\right)=0.$$

Let
\begin{align*}
&A=\sum_{j=0}^{p-1} j\left({j\over p}\right),
\ \ \ \ \ \ \ B=\sum_{j=0}^{p-1} \left({j\over p}\right),\\
&U_i=\sum_{j=0}^{p-1} j\left({2j+i\over p}\right),
V_i=\sum_{j=0}^{p-1} \left({2j+i\over p}\right) \mbox{for $i=0,1$},\\
&S_i=\sum_{j=0}^{p-1} j\left({4j+i\over p}\right),
T_i=\sum_{j=0}^{p-1} \left({4j+i\over p}\right)  \mbox{for $i=0,1,2,3$}.
\end{align*}
Then $B=V_i=T_i=0$ because $p$ does not divide $2$ and $4$ (see this). 
Moreover $p\equiv 7$ (mod $8$) implies
$$\left({-1\over p}\right)=(-1)^{(p-1)/2}=-1\quad\mbox{and}\quad
\left({2\over p}\right)=(-1)^{\lfloor(p+1)/4\rfloor}=1.$$
Now, $U_0=S_0=A$ and
\begin{align*}
\sum_{r=0}^{2p-1} r\left({r\over p}\right)&=
\sum_{i=0}^1\sum_{j=0}^{p-1}(2j+i)\left({2j+i\over p}\right)
=2U_0+2U_1+V_1=2A+2U_1,\\
\sum_{r=0}^{2p-1} r\left({r\over p}\right)&=
\sum_{i=0}^1\sum_{r=0}^{p-1} (ip+r)\left({ip+r\over p}\right)
=A+pB+A=2A,
\end{align*}
which imply that $S_2=U_1=0$. Moreover 
\begin{align*}
\sum_{r=0}^{4p-1} r\left({r\over p}\right)&=
\sum_{i=0}^3\sum_{j=0}^{p-1}(4j+i)\left({4j+i\over p}\right)
=4S_0+4S_1+4S_2+4S_3+T_1+T_2+T_3=4A+4S_1+4S_3,\\
\sum_{r=0}^{4p-1} r\left({r\over p}\right)&=
\sum_{i=0}^3\sum_{r=0}^{p-1} (ip+r)\left({ip+r\over p}\right)
=A+pB+A+2pB+A+3pB+A=4A,
\end{align*}
which imply that $S_1+S_3=0$.
Finally
\begin{align*}
S_1
&=\sum_{j=1}^{p-1} j\left({4j+1\over p}\right)
=\sum_{j=1}^{p-1} (p-j)\left({4(p-j)+1\over p}\right)
=-p\sum_{j=1}^{p-1}\left({4j-1\over p}\right)
+\sum_{j=1}^{p-1}j\left({4j-1\over p}\right)\\
&=-(p-1)\sum_{j=0}^{p-2}\left({4j+3\over p}\right)
+\sum_{j=0}^{p-2}j\left({4j+3\over p}\right)\\
&=(p-1)\left({-1\over p}\right)-(p-1)T_3-(p-1)\left({-1\over p}\right)+S_3=S_3
\end{align*}
and we obtain that $S_1=S_3=0$.