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.