Sum of Squares of Harmonic Numbers Let $H_n$ be the $n^{th}$ harmonic number,
$$ H_n  = \sum_{i=1}^{n} \frac{1}{i}
$$
Question: Calculate the following
$$\sum_{j=1}^{n} H_j^2.$$
I have attempted a generating function approach but could not solve this.  
 A: This is an interesting exercise in partial summation. For first, we have:
$$\begin{eqnarray*}\sum_{j=1}^{n}H_j &=& n H_n-\sum_{j=1}^{n-1} \frac{j}{j+1} = n H_n - (n-1)+\sum_{j=1}^{n-1}\frac{1}{j+1}\\&=& n H_n-n+1+(H_n-1) = (n+1)\,H_n-n\tag{1}\end{eqnarray*} $$
hence:
$$\begin{eqnarray*}\color{red}{\sum_{j=1}^n H_j^2} &=& \left((n+1)H_n^2-nH_n\right)-\sum_{j=1}^{n-1}\frac{(j+1)\,H_j-j}{j+1}\\&=&\left((n+1)H_n^2-nH_n\right)-\sum_{j=1}^{n-1}H_j+(n-1)-(H_n-1)\\&=&(n+1)\,H_n^2-nH_n-(n+1)\,H_n+n+H_n+(n-1)-H_n+1\\&=&\color{red}{(n+1)\,H_n^2-(2n+1)\,H_n+2n\phantom{\sum_{j=0}^{+\infty}}}.\tag{2}\end{eqnarray*}$$
Notice the deep analogy with:
$$\int \log^2 x\,dx = x\log^2 x -2x\log x +2x.$$
A: $$\sum_{k =1}^xH_k^2 = (x+1)H_x^2 - (2x+1)H_x + 2x$$
Before we prove the above solution
consider this following
$$\begin{align*}
\color{blue}{\sum_{k =1}^{x}H_k}
&=\sum_{k =1}^{x}\sum_{n =1}^{k}\frac 1{n}=\sum_{k =1}^{x}\frac 1{n}\sum_{k =n}^{x}1 = \sum_{k =1}^{x}\frac 1{n}(x-n+1)\\ &=\sum_{k =1}^{x}\frac {(x+1)-n}{n} =(x+1)\sum_{k =1}^{x}\frac 1{n} - \sum_{k =1}^{x}1 = \color{blue}{(x+1)H_x - x = \sum_{k =1}^{x}H_k}
\end{align*}$$
Consider,
$$\sum_{k =1}^xa_kb_k = s_xb_x- \sum_{k = 1}^{x-1}s_k(b_{k+1}-b_k)\text{ Where } s_u =\sum_{k =1}^ua_k$$
Put: $a_k = b_k = H_k$
$$\begin{align*}
\color{blue}{\sum_{k =1}^xH^2_k}
& = s_xH_k- \sum_{k =1}^{x-1}s_k(H_{k+1}- H_k)\\
& =((x+1)H_x-x)H_k -\sum_{k =1}^{x-1}[(k+1)H_k - k](\frac 1{k+1})\\
& = (x+1)H^2_x - xH_x - \sum_{k =1}^{x-1}H_k + \sum_{k =1}^{x-1}\left(1-\frac {1}{k+1}\right)\\
& = (x+1)H^2_x-xH_x -xH_{x-1} + 2x - 2- \sum_{k =2}^{x}\frac 1{k}\\
& = \color{blue}{(x+1)H^2_x-(2x+1)H_x +2x}
\end{align*}$$
image
A: Let's do this using interchanges of the order of summation:
$$\begin{align}
\sum_{n=1}^NH_n^2
&=\sum_{n=1}^N\sum_{h,k=1}^n{1\over hk}\\
&=\sum_{h,k=1}^N\sum_{n=\max(h,k)}^N{1\over hk}\\
&=\sum_{h,k=1}^N{N+1-\max(h,k)\over hk}\\
&=(N+1)H_N^2-\sum_{h,k=1}^N{\max(h,k)\over hk}\\
&=(N+1)H_N^2-\sum_{m=1}^N{m\over m^2}-2\sum_{m=2}^N\sum_{k=1}^{m-1}{m\over mk}\\
&=(N+1)H_N^2-H_N-2\sum_{k=1}^{N-1}\sum_{m=k+1}^N{1\over k}\\
&=(N+1)H_N^2-H_N-2\sum_{k=1}^{N-1}{N-k\over k}\\
&=(N+1)H_N^2-H_N-2NH_{N-1}+2(N-1)\\
&=(N+1)H_N^2-H_N-2N(H_N-1/N)+2(N-1)\\
&=(N+1)H_N^2-(2N+1)H_N+2N
\end{align}$$
