Solution (source code)

= Solution

Expand the <norm> of the orthonormal-<basis> expression. All cross terms vanish and $\|e_k\|=1$, so
$$
\boxed{\|P_nx\|^2=\sum_{k=1}^n|\langle x,e_k\rangle|^2.}
$$