4

I can sum $F_n=\sum_{s=1}^{n} \sum_{r=1}^{n}|r-s|$ as

$$F_n=2\sum_{s=1}^{n} \sum_{r=s+1}^{n} (r-s)$$ $$=2\sum_{s=1}^n \sum_{r=s+1}^n r-2\sum_{s=1}^{n} \sum_{r=s+1}^{n} s$$ $$=\sum_{s=1}^n [(n-s)(n+s+1)-2(n-s)s]$$ $$=\sum_{s=1}^n (n-s)(n-s+1)$$ $$=\sum_{s=1}^n s(s-1)=\frac{n(n+1)((2n+1)}{6}-\frac{n(n+1)}{2}=\frac{n^3-n}{3}$$

The question is how to get a nice analytic form in terms of $n$ for $$S_n=\sum_{s=1}^{n} \sum_{r=1}^{n}|2r-s|$$ as a polynomial of $n$? What is the polynomial?

I know that $S_5=89, S_{10}=690$.

Z Ahmed
  • 46,319
  • 2
    $$ S_n=\lceil f(n)\rceil=\begin{cases}f(n),&n\text{ is even}\f(n)+1/4,&n\text{ is odd}\end{cases},\quad f(n)=\frac{8n^3+3n^2-2n}{12}. $$ – metamorphy May 28 '25 at 05:12
  • Perfect, thanks you so much @metamorphy, a great help. I will try to prove and also invite others to give it a try – Z Ahmed May 28 '25 at 05:34
  • 1
    One thing which is interesting is that the differences of third order are periodic. $(3,5,3,5,3,\cdots)$ – Claude Leibovici May 28 '25 at 06:49
  • Yes, that means even (odd) terms follow different trends (quadratics) that is why two results as mentioned by @metamorphy. – Z Ahmed May 28 '25 at 07:04
  • 1
    It would also be interesting to compute $\sum_{r=1}^n \sum_{s=1}^n \lvert \alpha s -r \rvert$ for real values $\alpha > 0$, but that seems to be a bit more complicated. – Martin R May 29 '25 at 07:36

2 Answers2

2

Here is a computation of the more general sum $$ S(n, k) = \sum_{r=1}^n \sum_{s=1}^n \lvert ks -r \rvert $$ where $n$ and $k$ are positive integers. We split the double sum in three parts:

  • $A = $ sum of the terms with $kr \le n$ and $1\le s \le kr$.
  • $B = $ sum of the terms with $kr \le n$ and $kr < s \le n$.
  • $C = $ sum of the terms with $kr > n$ and $1 \le s \le n$.

Let $d = n \bmod k$, then $0 \le d \le k-1$ and $\lfloor n/k \rfloor = (n-d)/k$. We have $$ A = \sum_{r=1}^{(n-d)/k} \sum_{s=1}^{kr}(kr-s) = \sum_{r=1}^{(n-d)/k} \frac{(kr-1)kr}{2} \, , \\ B = \sum_{r=1}^{(n-d)/k} \sum_{s=kr+1}^{n}(s-kr) = \sum_{r=1}^{(n-d)/k} \frac{(n-kr)(n-kr+1)}{2} \,, \\ C = \sum_{r=(n-d)/k+1}^n \sum_{s=1}^n (kr -s) = \sum_{r=(n-d)/k+1}^n \left(nkr -\frac{n(n+1)}{2}\right) \, . $$ A tedious but straightforward calculation gives $$ S(n, k) = \frac{3k^2-3k+2}{6k} n^3 + \frac{(k-1)^2}{2k} n^2 + \frac{k-3}{6}n + \frac{d(k-d)(2d+3-k)}{6k} \, . $$ The last term depends on $n$ but is bounded for fixed $k$.

Example: $k=2$. Then $d=0$ if $n$ is even and $d=1$ if $n$ is odd, and $$ S(n, 2) = \frac 23 n^3 + \frac 14 n^2 - \frac 16 n + \begin{cases} 0 & \text{if $n$ is even} \, ,\\ 1/4 & \text{if $n$ is odd} \, .\\ \end{cases} $$

Martin R
  • 128,226
0

$$S_n=\sum_{s=1}^{n} \sum_{r=1}^{n}|2r-s|=2\sum_{s=1}^n\left[\sum_{r=1}^ {\lfloor s/2 \rfloor} (s/2-r)+\sum_{r=1}^{\lfloor s/2 \rfloor+1} (r-s/2)\right]$$ works well and one gets $S_n$: $1,6,20,46,89,152,240,356,505,690,...$

As per the comments of @Claude Leibovici the third differences of this sequence oscillate as $\{3,5,3,5,3,5,3\}$, indicating separate expressions for $S_n$ when $n$ is even or odd. The final answer here below does conform to this feature.

For alalytic expression, let us define $t=\lfloor n/2 \rfloor$ $$S_n=\sum_{r=1}^{t}\left[\sum_{s=1}^{2r} (2r-s)+ \sum_{s=2r+1}^{n} (s-2r)\right]+\sum_{r=t+1}^n \sum_{s=1}^n (2r-s).$$ Let $|2r-s|=k$, then $$S_n=\sum_{r=1}^t\left[ \sum_{k=0}^{2r-1} k +\sum_{k=1}^{n-2r} k\right]+\sum_{r=t+1}^n ~\sum_{k= 2n-r}^{2n-1} k.$$ $$S_n=\sum_{r=1}^t [r(2r-1)+\frac{1}{2} (n-2r)(n-2r+1)]+ \sum_{r=t+1}^n \frac{1}{2}n (4n-r-1).$$ $$\implies S_n=\frac{n^3}{3}+n^2t-2nt^2+\frac{4t^2}{3}+\frac{n^2}{2}-nt+t^2-\frac{t}{3}.$$ $$\implies S_{2m}=\frac{m}{3}(16m^2+3m-1) \quad \text{and} \quad S_{2m-1}=\frac{m}{3}(16m^2-21m+8),$$ which are consistent with the results of @Martin R in his solution given here.

Thank you Prasad Sir

Z Ahmed
  • 46,319