Relation between indefinite quadratic forms and continued fractions Let $D$ be a positive square free integer; for simplicity let's take $D$ to be $2$ or $3$ modulo $4$. Then ideal classes in $\mathbb{Z}[\sqrt{D}]$ are in bijection with matrices $\left( \begin{smallmatrix} a & b \\ b & c \end{smallmatrix} \right)$ such that $ac-b^2=-D$, module the action of $SL_2(\mathbb{Z})$. Given such a matrix, consider the element $\theta := \frac{b+\sqrt{D}}{a}$ in $\mathbb{Z}[\sqrt{D}]$. (Since $D$ is not a square, $a$ is not zero.) So matrices of the sort we are interested in are in bijection with numbers $\theta$ of the form $\frac{b+\sqrt{D}}{a}$ such that $a | (b^2-D)$.
When people discuss the continued fraction of $\sqrt{D}$, the set of such real numbers comes up, as it has the property that it is stable under translation by integers, and under inversion. (Which, now that I think about it, is just the $SL_2(\mathbb{Z})$ action. In fact,  the relation between  $\left( \begin{smallmatrix} a & b \\ b & c \end{smallmatrix} \right)$ and $\theta$ is that $(\theta \ 1)$ is isotropic for  $\left( \begin{smallmatrix} a & b \\ b & c \end{smallmatrix} \right)$. So it makes sense that we have related $SL_2(\mathbb{Z})$ actions.)
This makes me think there should be some way to relate the continued fraction behavior of $\sqrt{D}$ to the class group of $\mathbb{Z}[\sqrt{D}]$. This is probably some standard thing that every number theorist knows. Where do I read about it?
 A: The continued fraction is actually related to the principle cycle, which in turn is related to the class group. For example, it is an easy exercise to prove that the period length for $\sqrt{D}$ is at most a logarithmic factor away from the regulator of $\mathbb{Z}[\sqrt{D}]$ (this need not be a maximal order).
The first place I recall that contains a bit on the correspondence with classes, the relation to the regulator, and computational applications is chpater 5.6 and 5.7 in Cohen's "A Course in Computational Algebraic Number Theory".
The second being "Quadratics" by Mollin, chapter 2.
A: Dror is right about the cycles.  
For definite forms($\Delta < 0$), there is only one reduced form per class but for indefinite forms($\Delta > 0$), there is actually a cycle of reduced forms.  Consider for example, the cycle of indefinite reduced forms 
(a, b, c) $\rightarrow$ (-a, b, -c)$\rightarrow$  (c, b, a) $\rightarrow$ (-c, -b, -a).
The cycle of the equivalent reduced indefinite forms corresponds to the convergents of the periodic continued fraction.  The quadratic forms also correspond to the ideal classes of the quadratic field   
Mollin's book on Quadratics that Dror mentioned has worked examples. Also see the paper "Computing in Quadratic Orders" on John Robertson's site 
A: Hi, David. This is roughly Robin's comment, just with longer discussions, and, I am afraid, links to my own answers. That's life. 
I think you would probably enjoy J.H. Conway, "The Sensual Quadratic Form," especially PSL_2(Z) on pages 27-33.
I always like  "The Markoff and Lagrange Spectra" by Thomas W. Cusick and Mary E. Flahive and 
 "Binary Quadratic Forms" by Duncan A. Buell. 
On particular issues I think you are raising, answers by me and by Dror in:
Reasons for switching from simple continued fractions for $\sqrt D$ to reduced forms:
Upper bound of period length of continued fraction representation of very composite number square root 
Numbers (here primes) occurring as "diagonal" coefficients:
Primes as the first coefficient of a reduced indefinite quadratic form 
Other:
Numbers characterized by extremal properties 
In my language, I think you are asking mostly about small numbers occurring as diagonal coefficients, just not necessarily prime. For me, such numbers need also to be primitively represented by the forms in the cycle (for you I guess it is by $ x^2 - D y^2  ,$ using
$a_0 = \lfloor \sqrt D \rfloor$ your quadratic form is equivalent to the reduced form $\langle 1,\; 2 a_0, \; a_0^2 - D \rangle.$  )
phoebus:~/Cplusplus> ./Pell
Input n for Pell
67

0  form   1 16 -3   delta  -5
1  form   -3 14 6   delta  2
2  form   6 10 -7   delta  -1
3  form   -7 4 9   delta  1
4  form   9 14 -2   delta  -7
5  form   -2 14 9   delta  1
6  form   9 4 -7   delta  -1
7  form   -7 10 6   delta  2
8  form   6 14 -3   delta  -5
9  form   -3 16 1   delta  16
10  form   1 16 -3

 disc   268
Automorph, written on right of Gram matrix:
-1106  -17901
-5967  -96578


 Pell automorph
-48842  -399789
-5967  -48842

Pell unit
-48842^2 - 67 * -5967^2 = 1

=========================================
phoebus:~/Cplusplus>

Anyway, let me know if you want to see any more worked examples. 
Or the computer program. It is C++  so the numbers are bounded. Easy enough in other languages, of course.
A: Your question is the subject of Shanks' "infrastructure" of a quadratic field.  See http://en.wikipedia.org/wiki/Infrastructure_(number_theory) .
This has been generalized by Hendrik Lenstra and Rene Schoof (among others).  See the very nice paper by Schoof "Computing Arakelov Class Groups" http://www.msri.org/people/staff/levy/files/Book44/14schoof.pdf
