Solution (source code)

= Solution

A degree-$n$ <Legendre polynomial> $P_n$ is orthogonal, for integration on $[-1,1]$, to every <polynomial> of degree at most $n-1$, and has $n$ distinct roots in $(-1,1)$. Divide $p$ by $P_n$:
$$
p=P_nq+r,\qquad \deg q\leq n-1,\quad\deg r\leq n-1.
$$
The <Orthogonality of Legendre polynomials> gives $\int p=\int r$. At every quadrature node $x_j$, $p(x_j)=r(x_j)$. The assumed exactness for $r$ therefore proves \b[exactness up to degree $2n-1$], the characteristic property of <Gaussian quadrature>.