Checking a fundamental unit of a real quadratic field

Solution 1:

Here’s the story, though I leave it to others to furnish a proof or give a proper reference.

For $d\ge2$ and not a square, you get all solutions to the Pell equation $m^2-dn^2=\pm1$ by looking at the continued fraction expansion of $\sqrt d$. If $k=\lfloor\sqrt d\rfloor$, then your expansion looks like this: $$ \sqrt d=k+\bigl[\frac1{\delta_1+}\>\frac1{\delta_2+}\cdots\frac1{\delta_{r-2}+}\>\frac1{\delta_{r-1}+}\>\frac1{2k+}\>\bigr]\,, $$ where the part in brackets repeats infinitely. Then, every time you evaluate a convergent to the continued fraction just before the appearance of the $2k$, you’ll get a solution of Pell from the numerator $m$ and the denominator $n$. For instance, $\sqrt7=2+\frac1{1+}\,\frac1{1+}\,\frac1{1+}\,\frac1{4+}\,\cdots$, repeating with a period of length four. You evaluate $2+\frac1{1+}\,\frac1{1+}\,\frac1{1}=8/3$, and lo and behold, the first solution of $m^2-7n^2=\pm1$ is $m=8$, $n=3$.

For $d$ squarefree and incongruent to $1$ modulo $4$, this gives you all the units in $\Bbb Q(\sqrt{d}\,)$, but for $d\equiv1\pmod4$, it may happen that what you get is the cube of a unit. But as I recall, in that case, the primitive unit’s coordinates always will show up as the numerator and denominator of an earlier convergent.