TITLE: Continued fraction of a square root
QUESTION [38 upvotes]: If I want to find the continued fraction of $\sqrt{n}$ how do I know which number to use for $a_0$? Is there a way to do it without using a calculator or anything like that? What's the general algorithm for computing it? I tried to read the wiki article but was overwhelmed and lost. I tried Googling but couldn't find a website that actually explained this question.
If anyone has a good site that answers these questions either, please let me know. Thanks!

REPLY [2 votes]: For the record, the continued fraction expansion of a real number $x_0 = x$ is found by computing $a_n = \lfloor x_n \rfloor$ and then $x_{n+1} = 1 / (x_n - a_n)$.
For specific examples and intuition, see the other answers to this post. Here I describe a simple procedure for computing the expansion of $\sqrt{N}$ that involves only integer arithmetic.
The motivation is that we can always write $x_n = (r_n + \sqrt{N}) / s_n$ for integers $r_n$ and $s_n$.
We compute the sequences $a_n$, $r_n$ and $s_n$ inductively:
First, compute $a_0 = \lfloor \sqrt{N} \rfloor$. This is just the largest integer $a_0$ with $a_0^2 \leq N$. Set $r_0 = 0$ and $s_0 = 1$. Then for each $n$, define
$$
a_n = \left \lfloor \frac{r_n + a_0}{s_n} \right \rfloor
$$
$$ 
r_{n+1} = a_ns_n - r_n
$$
$$
s_{n+1} = (N - r_{n+1}^2) / s_n. 
$$
Sidenote: the sequence $s_n$ in fact ends up being all integers.