Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

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