GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Use mpz_kronecker_ui() to calculate an estimate for the quadratic1class number h(d), for a given negative fundamental discriminant, using2Dirichlet's analytic formula.34Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.56This file is part of the GNU MP Library.78This program is free software; you can redistribute it and/or modify it9under the terms of the GNU General Public License as published by the Free10Software Foundation; either version 2 of the License, or (at your option)11any later version.1213This program is distributed in the hope that it will be useful, but WITHOUT14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for16more details.1718You should have received a copy of the GNU General Public License along with19this program; if not, write to the Free Software Foundation, Inc., 51 Franklin20Street, Fifth Floor, Boston, MA 02110-1301, USA. */212223/* Usage: qcn [-p limit] <discriminant>...2425A fundamental discriminant means one of the form D or 4*D with D26square-free. Each argument is checked to see it's congruent to 0 or 127mod 4 (as all discriminants must be), and that it's negative, but there's28no check on D being square-free.2930This program is a bit of a toy, there are better methods for calculating31the class number and class group structure.3233Reference:3435Daniel Shanks, "Class Number, A Theory of Factorization, and Genera",36Proc. Symp. Pure Math., vol 20, 1970, pages 415-440.3738*/3940#include <math.h>41#include <stdio.h>42#include <stdlib.h>43#include <string.h>4445#include "gmp.h"4647#ifndef M_PI48#define M_PI 3.1415926535897932384649#endif505152/* A simple but slow primality test. */53int54prime_p (unsigned long n)55{56unsigned long i, limit;5758if (n == 2)59return 1;60if (n < 2 || !(n&1))61return 0;6263limit = (unsigned long) floor (sqrt ((double) n));64for (i = 3; i <= limit; i+=2)65if ((n % i) == 0)66return 0;6768return 1;69}707172/* The formula is as follows, with d < 0.7374w * sqrt(-d) inf p75h(d) = ------------ * product --------762 * pi p=2 p - (d/p)777879(d/p) is the Kronecker symbol and the product is over primes p. w is 680when d=-3, 4 when d=-4, or 2 otherwise.8182Calculating the product up to p=infinity would take a long time, so for83the estimate primes up to 132,000 are used. Shanks found this giving an84accuracy of about 1 part in 1000, in normal cases. */8586unsigned long p_limit = 132000;8788double89qcn_estimate (mpz_t d)90{91double h;92unsigned long p;9394/* p=2 */95h = sqrt (-mpz_get_d (d)) / M_PI96* 2.0 / (2.0 - mpz_kronecker_ui (d, 2));9798if (mpz_cmp_si (d, -3) == 0) h *= 3;99else if (mpz_cmp_si (d, -4) == 0) h *= 2;100101for (p = 3; p <= p_limit; p += 2)102if (prime_p (p))103h *= (double) p / (double) (p - mpz_kronecker_ui (d, p));104105return h;106}107108109void110qcn_str (char *num)111{112mpz_t z;113114mpz_init_set_str (z, num, 0);115116if (mpz_sgn (z) >= 0)117{118mpz_out_str (stdout, 0, z);119printf (" is not supported (negatives only)\n");120}121else if (mpz_fdiv_ui (z, 4) != 0 && mpz_fdiv_ui (z, 4) != 1)122{123mpz_out_str (stdout, 0, z);124printf (" is not a discriminant (must == 0 or 1 mod 4)\n");125}126else127{128printf ("h(");129mpz_out_str (stdout, 0, z);130printf (") approx %.1f\n", qcn_estimate (z));131}132mpz_clear (z);133}134135136int137main (int argc, char *argv[])138{139int i;140int saw_number = 0;141142for (i = 1; i < argc; i++)143{144if (strcmp (argv[i], "-p") == 0)145{146i++;147if (i >= argc)148{149fprintf (stderr, "Missing argument to -p\n");150exit (1);151}152p_limit = atoi (argv[i]);153}154else155{156qcn_str (argv[i]);157saw_number = 1;158}159}160161if (! saw_number)162{163/* some default output */164qcn_str ("-85702502803"); /* is 16259 */165qcn_str ("-328878692999"); /* is 1499699 */166qcn_str ("-928185925902146563"); /* is 52739552 */167qcn_str ("-84148631888752647283"); /* is 496652272 */168return 0;169}170171return 0;172}173174175