1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/* Use mpz_kronecker_ui() to calculate an estimate for the quadratic
class number h(d), for a given negative fundamental discriminant, using
Dirichlet's analytic formula.
Copyright 1999-2002 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see https://www.gnu.org/licenses/. */
/* Usage: qcn [-p limit] <discriminant>...
A fundamental discriminant means one of the form D or 4*D with D
square-free. Each argument is checked to see it's congruent to 0 or 1
mod 4 (as all discriminants must be), and that it's negative, but there's
no check on D being square-free.
This program is a bit of a toy, there are better methods for calculating
the class number and class group structure.
Reference:
Daniel Shanks, "Class Number, A Theory of Factorization, and Genera",
Proc. Symp. Pure Math., vol 20, 1970, pages 415-440.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gmp.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* A simple but slow primality test. */
int
prime_p (unsigned long n)
{
unsigned long i, limit;
if (n == 2)
return 1;
if (n < 2 || !(n&1))
return 0;
limit = (unsigned long) floor (sqrt ((double) n));
for (i = 3; i <= limit; i+=2)
if ((n % i) == 0)
return 0;
return 1;
}
/* The formula is as follows, with d < 0.
w * sqrt(-d) inf p
h(d) = ------------ * product --------
2 * pi p=2 p - (d/p)
(d/p) is the Kronecker symbol and the product is over primes p. w is 6
when d=-3, 4 when d=-4, or 2 otherwise.
Calculating the product up to p=infinity would take a long time, so for
the estimate primes up to 132,000 are used. Shanks found this giving an
accuracy of about 1 part in 1000, in normal cases. */
unsigned long p_limit = 132000;
double
qcn_estimate (mpz_t d)
{
double h;
unsigned long p;
/* p=2 */
h = sqrt (-mpz_get_d (d)) / M_PI
* 2.0 / (2.0 - mpz_kronecker_ui (d, 2));
if (mpz_cmp_si (d, -3) == 0) h *= 3;
else if (mpz_cmp_si (d, -4) == 0) h *= 2;
for (p = 3; p <= p_limit; p += 2)
if (prime_p (p))
h *= (double) p / (double) (p - mpz_kronecker_ui (d, p));
return h;
}
void
qcn_str (char *num)
{
mpz_t z;
mpz_init_set_str (z, num, 0);
if (mpz_sgn (z) >= 0)
{
mpz_out_str (stdout, 0, z);
printf (" is not supported (negatives only)\n");
}
else if (mpz_fdiv_ui (z, 4) != 0 && mpz_fdiv_ui (z, 4) != 1)
{
mpz_out_str (stdout, 0, z);
printf (" is not a discriminant (must == 0 or 1 mod 4)\n");
}
else
{
printf ("h(");
mpz_out_str (stdout, 0, z);
printf (") approx %.1f\n", qcn_estimate (z));
}
mpz_clear (z);
}
int
main (int argc, char *argv[])
{
int i;
int saw_number = 0;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-p") == 0)
{
i++;
if (i >= argc)
{
fprintf (stderr, "Missing argument to -p\n");
exit (1);
}
p_limit = atoi (argv[i]);
}
else
{
qcn_str (argv[i]);
saw_number = 1;
}
}
if (! saw_number)
{
/* some default output */
qcn_str ("-85702502803"); /* is 16259 */
qcn_str ("-328878692999"); /* is 1499699 */
qcn_str ("-928185925902146563"); /* is 52739552 */
qcn_str ("-84148631888752647283"); /* is 496652272 */
return 0;
}
return 0;
}
|