Why does $\sum_{j=i+1}^{n}(n-i-j+1) = n^{2}-5 n-2-i^{2}-3i$ Reading through https://stackoverflow.com/q/40696784/15753188 , the following calculation is performed


I see the rightmost summation is summing $(n-i-j+1)$, $n-(i+1)$ times. My first thought is to multiply $(n-(i+1))$ and $(n-i-(i+1)+1)$ (replacing the $j$ with $i+1$), but this gives me a wildly different answer than $n^{2}-5 n-2-i^{2}-3i$.
 A: First, let's show that the equation doesn't hold.
If $n=1$, $$\sum_{i=1}^1 \sum_{j=i+1}^1(1-i-j+1) = 0$$
but $$\sum_{i=1}^1 (1^2-5-2-i^2-3i)=-10.$$

\begin{align}
\sum_{j=i+1}^n (n-i-j+1) &= \sum_{j=i+1}^n (n-i+1) - \sum_{j=i+1}^n j \tag{1}\\
&=(n-i+1)(n-i) - \frac{(n-i)(n+i+1)}{2}\\
&=(n-i) \cdot \frac{2n-2i+2 - n-i-1}{2}\\
&= (n-i)\cdot \frac{n-3i+1}2 \\
&=\frac12\cdot  \left(n^2 + (1-4i)n +3i^2-i\right)
\end{align}
The first term in $(1)$ is evaluated using multiplication but the second term is evaluated using an arithmetic sum. Multiplication by $k$ where $k$ is an integer means summing up the same number $k$ times.
A: That post has several mistakes in it.
To get the right answer, you need to be very careful with the indices, which can get tricky. If the lower bound is greater than the upper bound, a lot of identities don't work as one might expect (assuming that a sum from a lower bound that is greater than the upper bound equals 0).
We start with:
$$\sum_{i=1}^{n}\sum_{j=i+1}^{n}\sum_{k=i+j-1}^{n}1$$
To ensure that $i+j-1 \le n$, we change the upper bound of the second sum to $n-i+2$. This doesn't change the result.
$$\sum_{i=1}^{n}\sum_{j=i+1}^{n-i+2}\sum_{k=i+j-1}^{n}1$$
Next, we need to ensure that $i+1\le n-i+2$. To do this, we set the upper bound of the outer sum to $\operatorname{floor}((n+1)/2)$
$$\sum_{i=1}^{\operatorname{floor}((n+1)/2)}\sum_{j=i+1}^{n-i+2}\sum_{k=i+j-1}^{n}1$$
Then, since the body of the innermost sum is independent on the sum index, we can replace the sum by the amount of elements it is summing over ($n-(i+j-1)+1=n-i-j+2$) times the expression.
$$\sum_{i=1}^{\operatorname{floor}((n+1)/2)}\sum_{j=i+1}^{n-i+2}\left(n-i-j+2\right)$$
Then, we split the inner sum into a sum over $n-i+2$ and a sum over $j$.
$$\sum_{i=1}^{\operatorname{floor}((n+1)/2)}(\sum_{j=i+1}^{n-i+2}\left(n-i+2\right) - \sum_{j=1}^{n-i+2}j + \sum_{j=1}^{i}j)$$
Since it does not depend on the sum index, we can solve the first inner sum with a simple multiplication. The other two can be solved using the well known identity $1+2+3+...+n=n(n+1)/2$. After some simplification, we get here:
$$\frac{1}{2}\sum_{i=1}^{\operatorname{floor}((n+1)/2)}\left(n^{2}+3n+2+4i^{2}-\left(4n+6\right)i\right)$$
