TITLE: An upper bound for the length of the continued fraction expansion of $\sqrt d$
QUESTION [10 upvotes]: Let $d\ge 2$ and let 
$$
\sqrt d =[a_0; \overline{a_1,\dots, a_\ell, 2a_0}]
$$
be its continued fraction expansion. Clearly, if $d=n^2+1$, then $\ell=0$, which gives the lower bound for $\ell$. 
Question. What is the best known upper bound for $\ell=\ell(d)$ as a function of $d$?
For instance, $\ell(d)=O(d)$ is fairly trivial, following from the well-known algorithm (see [Rockett-Szusz], for instance). However, I suspect something like $O(\sqrt d)$ (or $O(\sqrt d\log d$) must be known.

REPLY [13 votes]: It is known that $\ell(d)=O(\sqrt{d}\log d)$ and $\ell(d)=\Omega(\sqrt{d}/\log\log d)$. 
See Cohn's paper (free access) for more details. 
For numerical results and some further historical comments see Williams's paper.