Solving the Pell equation