1 /* Use mpz_kronecker_ui() to calculate an estimate for the quadratic
2 class number h(d), for a given negative fundamental discriminant, using
3 Dirichlet's analytic formula.
5 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your option)
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 You should have received a copy of the GNU General Public License along with
20 this program. If not, see http://www.gnu.org/licenses/. */
23 /* Usage: qcn [-p limit] <discriminant>...
25 A fundamental discriminant means one of the form D or 4*D with D
26 square-free. Each argument is checked to see it's congruent to 0 or 1
27 mod 4 (as all discriminants must be), and that it's negative, but there's
28 no check on D being square-free.
30 This program is a bit of a toy, there are better methods for calculating
31 the class number and class group structure.
35 Daniel Shanks, "Class Number, A Theory of Factorization, and Genera",
36 Proc. Symp. Pure Math., vol 20, 1970, pages 415-440.
48 #define M_PI 3.14159265358979323846
52 /* A simple but slow primality test. */
54 prime_p (unsigned long n)
56 unsigned long i, limit;
63 limit = (unsigned long) floor (sqrt ((double) n));
64 for (i = 3; i <= limit; i+=2)
72 /* The formula is as follows, with d < 0.
75 h(d) = ------------ * product --------
79 (d/p) is the Kronecker symbol and the product is over primes p. w is 6
80 when d=-3, 4 when d=-4, or 2 otherwise.
82 Calculating the product up to p=infinity would take a long time, so for
83 the estimate primes up to 132,000 are used. Shanks found this giving an
84 accuracy of about 1 part in 1000, in normal cases. */
86 unsigned long p_limit = 132000;
89 qcn_estimate (mpz_t d)
95 h = sqrt (-mpz_get_d (d)) / M_PI
96 * 2.0 / (2.0 - mpz_kronecker_ui (d, 2));
98 if (mpz_cmp_si (d, -3) == 0) h *= 3;
99 else if (mpz_cmp_si (d, -4) == 0) h *= 2;
101 for (p = 3; p <= p_limit; p += 2)
103 h *= (double) p / (double) (p - mpz_kronecker_ui (d, p));
114 mpz_init_set_str (z, num, 0);
116 if (mpz_sgn (z) >= 0)
118 mpz_out_str (stdout, 0, z);
119 printf (" is not supported (negatives only)\n");
121 else if (mpz_fdiv_ui (z, 4) != 0 && mpz_fdiv_ui (z, 4) != 1)
123 mpz_out_str (stdout, 0, z);
124 printf (" is not a discriminant (must == 0 or 1 mod 4)\n");
129 mpz_out_str (stdout, 0, z);
130 printf (") approx %.1f\n", qcn_estimate (z));
137 main (int argc, char *argv[])
142 for (i = 1; i < argc; i++)
144 if (strcmp (argv[i], "-p") == 0)
149 fprintf (stderr, "Missing argument to -p\n");
152 p_limit = atoi (argv[i]);
163 /* some default output */
164 qcn_str ("-85702502803"); /* is 16259 */
165 qcn_str ("-328878692999"); /* is 1499699 */
166 qcn_str ("-928185925902146563"); /* is 52739552 */
167 qcn_str ("-84148631888752647283"); /* is 496652272 */