Finding the class number h(d) of primitive binary quadratic forms of positive discriminant d

Here d > 1, d ≡ 0 or 1 (mod 4), is not a perfect square.
We locate all reduced irrationals of the form (b+√d)/(2|c|), where c is negative and 4c divides d-b2. We use the PQa continued fraction algorithm of Lagrange to break the set into disjoint cycles, retaining one number from each cycle.
Each reduced number then gives rise to a reduced form (a,b,c) of discriminant d, where a=(b2-d)/4c.
We are able to also determine if the Pell equation x2-dy2=-4 has a solution, by using the fact that the equation is soluble iff at least one of the above cycles is odd. If there is no solution, the reduced forms (-a,b,-c) have to be counted as well.

(See G.B. Mathews, Theory of Numbers, 80-81, note and Henri Cohen's A course in computational number theory, page 260, First Edition.)

Enter d (d > 1 and not a perfect square, d ≡ 0 or 1 (mod 4), d < 106 ):

Last modified 31st October 2006
Return to main page