Solution (source code)

= Solution

Interpret the weighted function space as measurable functions modulo equality <almost everywhere>, so the integral form is a genuine <inner product>. Assume $q\in\Pi_k\subseteq V$. If the residual is <orthogonal> to $\Pi_k$, then for $p\in\Pi_k$,
$$
\|f-p\|^2=\|(f-q)+(q-p)\|^2=\|f-q\|^2+\|q-p\|^2\ge\|f-q\|^2.
$$
Thus $q$ is a <best approximation>. Conversely, if it is best, take any $r\in\Pi_k$ and vary through $q+tr$. The <polynomial>
$$
\|f-q-tr\|^2=\|f-q\|^2-2t\langle f-q,r\rangle+t^2\|r\|^2
$$
has a <minimum> at $t=0$, so its <derivative> there vanishes. Hence $\boxed{\langle f-q,r\rangle=0}$ for every $r\in\Pi_k$. The same <Pythagorean identity> proves uniqueness of the <best approximation>.