Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346/* 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-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 3 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, see https://www.gnu.org/licenses/. */202122/* Usage: qcn [-p limit] <discriminant>...2324A fundamental discriminant means one of the form D or 4*D with D25square-free. Each argument is checked to see it's congruent to 0 or 126mod 4 (as all discriminants must be), and that it's negative, but there's27no check on D being square-free.2829This program is a bit of a toy, there are better methods for calculating30the class number and class group structure.3132Reference:3334Daniel Shanks, "Class Number, A Theory of Factorization, and Genera",35Proc. Symp. Pure Math., vol 20, 1970, pages 415-440.3637*/3839#include <math.h>40#include <stdio.h>41#include <stdlib.h>42#include <string.h>4344#include "gmp.h"4546#ifndef M_PI47#define M_PI 3.1415926535897932384648#endif495051/* A simple but slow primality test. */52int53prime_p (unsigned long n)54{55unsigned long i, limit;5657if (n == 2)58return 1;59if (n < 2 || !(n&1))60return 0;6162limit = (unsigned long) floor (sqrt ((double) n));63for (i = 3; i <= limit; i+=2)64if ((n % i) == 0)65return 0;6667return 1;68}697071/* The formula is as follows, with d < 0.7273w * sqrt(-d) inf p74h(d) = ------------ * product --------752 * pi p=2 p - (d/p)767778(d/p) is the Kronecker symbol and the product is over primes p. w is 679when d=-3, 4 when d=-4, or 2 otherwise.8081Calculating the product up to p=infinity would take a long time, so for82the estimate primes up to 132,000 are used. Shanks found this giving an83accuracy of about 1 part in 1000, in normal cases. */8485unsigned long p_limit = 132000;8687double88qcn_estimate (mpz_t d)89{90double h;91unsigned long p;9293/* p=2 */94h = sqrt (-mpz_get_d (d)) / M_PI95* 2.0 / (2.0 - mpz_kronecker_ui (d, 2));9697if (mpz_cmp_si (d, -3) == 0) h *= 3;98else if (mpz_cmp_si (d, -4) == 0) h *= 2;99100for (p = 3; p <= p_limit; p += 2)101if (prime_p (p))102h *= (double) p / (double) (p - mpz_kronecker_ui (d, p));103104return h;105}106107108void109qcn_str (char *num)110{111mpz_t z;112113mpz_init_set_str (z, num, 0);114115if (mpz_sgn (z) >= 0)116{117mpz_out_str (stdout, 0, z);118printf (" is not supported (negatives only)\n");119}120else if (mpz_fdiv_ui (z, 4) != 0 && mpz_fdiv_ui (z, 4) != 1)121{122mpz_out_str (stdout, 0, z);123printf (" is not a discriminant (must == 0 or 1 mod 4)\n");124}125else126{127printf ("h(");128mpz_out_str (stdout, 0, z);129printf (") approx %.1f\n", qcn_estimate (z));130}131mpz_clear (z);132}133134135int136main (int argc, char *argv[])137{138int i;139int saw_number = 0;140141for (i = 1; i < argc; i++)142{143if (strcmp (argv[i], "-p") == 0)144{145i++;146if (i >= argc)147{148fprintf (stderr, "Missing argument to -p\n");149exit (1);150}151p_limit = atoi (argv[i]);152}153else154{155qcn_str (argv[i]);156saw_number = 1;157}158}159160if (! saw_number)161{162/* some default output */163qcn_str ("-85702502803"); /* is 16259 */164qcn_str ("-328878692999"); /* is 1499699 */165qcn_str ("-928185925902146563"); /* is 52739552 */166qcn_str ("-84148631888752647283"); /* is 496652272 */167return 0;168}169170return 0;171}172173174