Is there a formula in $d$ for units of $\mathbb{Z}[\sqrt{d}]$? Dirichlet's Unit Theorem tells us all units in the integer ring of a real quadratic field, say $\mathbb{Z}[\sqrt{d}]$, are powers of a single "fundamental unit". Meaning there exists $\alpha=a+b\sqrt{d}$ such that $N(\alpha)=a^2-db^2=\pm1$ and that for any other unit $\beta$ there exists an integer $k$ such that $\beta=\alpha^k$.
What I'm wondering is whether there's a formula for $a$ and $b$ in terms of $d$. For $d=2$ for example, we have $a=b=1$ and $\alpha=1+\sqrt{2}$. 
I understand also that $a^2-db^2=\pm1$ is essentially a solution to Pell's equation. One can find a table of solutions for $d=1,...,128$ on Wikipedia $-$ but no formula. That same wikipedia article also points out that the smallest solutions for some particular $d$ can be very large relative to $d$. For example, with $d=313$, the smallest solution is $$a, b = 32188120829134849, 1819380158564160.$$
That's not encouraging for the existence of a formula. 
This came up because in a theorem on $\mathbb{Z}[\sqrt{2}]$ I'm hoping to generalize to $\mathbb{Z}[\sqrt{d}]$ in general. The most encouraging route for proving the generalization makes heavy use of the value of $b$.
What branch of math should one even walk down in hopes of spotting an answer?
 A: There is almost certainly no formula for $\mathbb{Z}[\sqrt{d}]$ in general. However, there are families of real quadratics for which we can say something about the fundamental unit. Particularly we want solutions to 
$$N(\alpha)=N(a+b\sqrt{d})=a^2-b^2d=\pm1.$$
We can solve this parametrically choosing $d=t^2+1$. It follows that $\alpha=t+\sqrt{d}$ is a unit. And we can see it is the principal unit by noting that the coeffiicent of $\sqrt{d}$ cannot be made any closer to zero. In this example, choosing $t=1$ gives the unit $1+\sqrt{2}$ in $\mathbb{Z}[\sqrt{2}]$. So for $d=2,5,10,17,26,...$ we can suppose that $b=1$. By similar logic one can show also that $b=1$ when $d=t^2-1$. 
We could view this the other way round too: we first assume $b=1$. From here, we note that $a^2-d=\pm1$ rearranges into $d=a^2\mp 1$. Or again, choosing $b=2$ we obtain $d=(a^2\mp1)/4$ which is solved by $a=2t+1$ and choosing "$-1$" for the "$\mp1$". Simplifying it all gives us $d=t^2+t$. Thus we get a statement like "If $d$ is of the form $t^2+t$ for some integer $t$, but is not of the form $t^2\pm1$ for any other integer $t$, it follows that the fundamental unit is of the form $a+2\sqrt{d}$." 
More generally, if $d$ is of the form $t(b^2t\pm2$) then we have a unit of the form
$$\alpha=(b^2t\pm1)+b\sqrt{d}.$$
This will be our principle unit if $b$ is the smallest positive integer such that $d=t(b^2t\pm2)$ is solvable in an integer $t$. This gives us a sort of sieve process:
$$\begin{array}{c|c|c|c|c|c|c}
d & b & t^2\pm1 & t^2+t & t(9t\pm2) & t(16t\pm 2) & ...\\ \hline
1 & [\text{square}] &  &  &  &  & \\ 
2 & 1 & \times & \times &  &  & \\ 
3 & 1 & \times &  &  &  & \\ 
4 & [\text{square}] &  &  &  &  & \\ 
5 & 1 & \times &  &  &  & \\ 
6 & 2 &  & \times &  &  & \\ 
7 & 3 &  &  & \times &  & \\ 
8 & 1 & \times &  &  &  & \\ 
9 & [\text{square}] &  &  &  &  & \\ 
10 & 1 & \times &  &  &  & \\
11 & 3 &  &  & \times &  & \\ 
12 & 2 &  & \times &  &  & \\ 
13 &  &  &  &  &  & \\ 
14 & 4 &  &  &  & \times & \\ 
15 & 1 & \times &  &  &  & \\ 
16 & [\text{square}] &  &  &  &  & \\ 
17 & 1 & \times &  &  &  & \\ 
18 & 4 &  &  &  & \times & \\ 
19 &  &  &  &  &  & \\ 
20 & 2 &  & \times &  &  & \\ 
... &  &  &  &  &  & \\ 
\end{array}$$
Interestingly, some values of $d$ are missed entirely. For $d\le 20$ we have the exceptions $d=13,19$ with units $649+180\sqrt{13}$ and $170+39\sqrt{19}$ respectively (from https://en.wikipedia.org/wiki/Pell%27s_equation). The exceptions continue
$$13,19,21,22,28,29,31,33,39,41,43,44,45,46,52,53,54,55,57,58,59,61,67,69,...$$
Since all these families grow like $\Theta(t^2)$ they unfortunately will cover a smaller and smaller percentage of the positive integers. Also $d=313$ is, in fact, on the list.
