Solution (source code)

= Solution

Under the <gauge transformation>,
$$
\widetilde{\mathbf B}=\nabla\times(\mathbf A+\nabla f)=\mathbf B
$$
because the <curl> of a <gradient> vanishes, and
$$
\widetilde{\mathbf E}
=-\nabla(\phi-\partial_tf)-\partial_t(\mathbf A+\nabla f)
=\mathbf E.
$$
The transformed <Lagrangian> is
$$
\widetilde L=L+q(\partial_tf+\dot{\mathbf r}\cdot\nabla f)
=L+\frac d{dt}(qf).
$$
A total time derivative changes the action only by endpoint terms, whose variation vanishes for fixed endpoints, so the <Euler-Lagrange equations> are unchanged.

The new momentum is
$$
\widetilde{\mathbf p}=m\dot{\mathbf r}+q\widetilde{\mathbf A}
=\mathbf p+q\nabla f.
$$
Use the <type-two generating function for a canonical transformation>
$$
F_2(\mathbf r,\widetilde{\mathbf p},t)
=\mathbf r\cdot\widetilde{\mathbf p}-qf(\mathbf r,t).
$$
It gives $\mathbf p=\partial F_2/\partial\mathbf r=\widetilde{\mathbf p}-q\nabla f$, $\widetilde{\mathbf r}=\partial F_2/\partial\widetilde{\mathbf p}=\mathbf r$, and
$$
\widetilde H=H+\partial_tF_2=H-q\partial_tf.
$$
This equals $|\widetilde{\mathbf p}-q\widetilde{\mathbf A}|^2/(2m)+q\widetilde\phi$, so the gauge change is a time-dependent <canonical transformation>.