Solution (source code)

= Solution

In <Hamiltonian mechanics>, the canonical <momenta> are
$$
p_r=m\dot r,\qquad p_\theta=mr^2\dot\theta,\qquad
p_\phi=mr^2\sin^2\theta\,\dot\phi.
$$
Hence
$$
\boxed{H=\frac{p_r^2}{2m}+\frac{p_\theta^2}{2mr^2}
 +\frac{p_\phi^2}{2mr^2\sin^2\theta}+V(r).}
$$