Solution (source code)

= Solution

Take any $q\in\Pi_n$. Write $f-q=(f-p_n)+(p_n-q)$. The second term belongs to $\Pi_n$, so the assumed orthogonality gives $\langle f-p_n,p_n-q\rangle=0$. Expanding the squared <norm> yields the <Pythagorean theorem> in this <inner product>:
$$
\|f-q\|^2=\|f-p_n\|^2+\|p_n-q\|^2\geq\|f-p_n\|^2.
$$
Taking square roots proves
$$
\boxed{p_n\text{ is a best least-squares approximation}.}
$$
Equality can hold only when $\|p_n-q\|=0$, hence $p_n=q$, so the best polynomial is unique. Completeness of the ambient space is unnecessary for this orthogonality argument.