TITLE: Generating functions - deriving a formula for the sum $1^2 + 2^2 +\cdots+n^2$
QUESTION [10 upvotes]: I would like some help with deriving a formula for the sum $1^2 + 2^2 +\cdots+n^2$ using generating functions. 
I have managed to do this for $1^2 + 2^2 + 3^2 +\cdots$ by putting
$$f_0(x) = \frac{1}{1-x} = 1 + x + x^2 + x^3 +\cdots$$
$$f_1(x) = x \frac{d}{dx}[f_0(x)] = \frac{1}{(1-x)^2} = 0 + x + 2x^2 + 3x^3 +\cdots$$
$$f_2(x) = x \frac{d}{dx}[f_1(x)] = \frac{x^2+x}{(1-x)^3} = 0^2 + 1^2x + 2^2x^3 + 3^2x^3+\cdots,$$
and I assume I'm supposed to be able to do something similar in this case, but things get trickier when it's bounded by n and I keep getting stuck.

REPLY [3 votes]: Hint: The following perspective with focus on operator methods might also be useful.

We can successively apply the $\left(x\frac{d}{dx}\right)$-operator to a generating function
\begin{align*}
  A(x)=\sum_{n=0}^{\infty}a_nx^n
  \end{align*}
to obtain
\begin{align*}
  \left(x\frac{d}{dx}\right)A(x)&=\sum_{n=0}^{\infty}na_nx^n\\
  \left(x\frac{d}{dx}\right)^2A(x)&=\sum_{n=0}^{\infty}n^2a_nx^n
  \end{align*}

Multiplication of $A(x)$ with $\frac{1}{1-x}$ results in summing up the coefficients $a_n$
\begin{array}{crl}
  (a_n)_{n\geq 0}\qquad &\qquad A(x)=&\sum_{n=0}^{\infty}a_nx^n\\
  \left(\sum_{k=0}^{n}a_k\right)_{n\geq 0}\qquad&\qquad\frac{1}{1-x}A(x)=&\sum_{n=0}^{\infty}\left(\sum_{k=0}^{n}a_k\right)x^n
  \end{array}
It's also convenient to use the coefficient of operator $[x^n]$ to denote the coefficient of $x^n$ in a generating series.

Putting all together and applying it to the geometric series $\frac{1}{1-x}=\sum_{n=0}^{\infty}x^n$ we finally obtain
\begin{align*}
  \sum_{k=0}^nk^2&=[x^n]\frac{1}{1-x}\left(x\frac{d}{dx}\right)^2\frac{1}{1-x}\\
  &=[x^n]\frac{x(1+x)}{(1-x)^4}\\
  &=\left([x^{n-1}+x^{n-2}]\right)\sum_{k=0}^{\infty}\binom{-4}{n}(-x)^n\tag{1}\\
  &=\left([x^{n-1}+x^{n-2}]\right)\sum_{k=0}^{\infty}\binom{n+3}{3}x^n\tag{2}\\
  &=\binom{n+2}{3}+\binom{n+1}{3}\\
  &=\frac{1}{6}n(n+1)(2n+1)
  \end{align*}

Comment:

In (1) we use the binomial series expansion and $[x^n]x^kA(x)=[x^{n-k}]A(x)$
In (2) we use $\binom{-n}{k}=\binom{n+k-1}{k}(-1)^k=\binom{n+k-1}{n-1}(-1)^k$