Solution (source code)

= Solution

The nonzero <orthogonal polynomials> $Q_0,\ldots,Q_n$ have distinct degrees and form an orthogonal basis of $\mathcal P_n$. Define
$$
p_n^*=\sum_{k=0}^n
\frac{\langle f,Q_k\rangle}{\langle Q_k,Q_k\rangle}Q_k.
$$
For each $j\leq n$,
$$
\langle f-p_n^*,Q_j\rangle
=\langle f,Q_j\rangle
-\frac{\langle f,Q_j\rangle}{\langle Q_j,Q_j\rangle}
\langle Q_j,Q_j\rangle=0.
$$
Thus the residual $f-p_n^*$ is <orthogonal> to all of $\mathcal P_n$.

For any $p\in\mathcal P_n$, write
$$
f-p=(f-p_n^*)+(p_n^*-p).
$$
The two terms are orthogonal, so the <Pythagorean theorem in an inner-product space> gives
$$
\lVert f-p\rVert^2
=\lVert f-p_n^*\rVert^2+\lVert p_n^*-p\rVert^2
\geq\lVert f-p_n^*\rVert^2.
$$
Equality holds only for $p=p_n^*$. This proves the formula and uniqueness of the <least-squares polynomial in an orthogonal-polynomial basis>.