GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Generate mpz_fac_ui data.12Copyright 2002 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of the GNU Lesser General Public License as published by8the Free Software Foundation; either version 2.1 of the License, or (at your9option) any later version.1011The GNU MP Library is distributed in the hope that it will be useful, but12WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public14License for more details.1516You should have received a copy of the GNU Lesser General Public License17along with the GNU MP Library; see the file COPYING.LIB. If not, write to18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,19MA 02110-1301, USA. */2021#include <stdio.h>22#include <stdlib.h>2324#include "dumbmp.c"252627/* sets x=y*(y+2)*(y+4)*....*(y+2*(z-1)) */28void29odd_products (mpz_t x, mpz_t y, int z)30{31mpz_t t;3233mpz_init_set (t, y);34mpz_set_ui (x, 1);35for (; z != 0; z--)36{37mpz_mul (x, x, t);38mpz_add_ui (t, t, 2);39}40mpz_clear (t);41return;42}4344/* returns 0 on success */45int46gen_consts (int numb, int nail, int limb)47{48mpz_t x, y, z, t;49unsigned long a, b, first = 1;5051printf ("/* This file is automatically generated by gen-fac_ui.c */\n\n");52printf ("#if GMP_NUMB_BITS != %d\n", numb);53printf ("Error , error this data is for %d GMP_NUMB_BITS only\n", numb);54printf ("#endif\n");55printf ("#if GMP_LIMB_BITS != %d\n", limb);56printf ("Error , error this data is for %d GMP_LIMB_BITS only\n", limb);57printf ("#endif\n");5859printf60("/* This table is 0!,1!,2!,3!,...,n! where n! has <= GMP_NUMB_BITS bits */\n");61printf62("#define ONE_LIMB_FACTORIAL_TABLE CNST_LIMB(0x1),CNST_LIMB(0x1),CNST_LIMB(0x2),");63mpz_init_set_ui (x, 2);64for (b = 3;; b++)65{66mpz_mul_ui (x, x, b); /* so b!=a */67if (mpz_sizeinbase (x, 2) > numb)68break;69if (first)70{71first = 0;72}73else74{75printf ("),");76}77printf ("CNST_LIMB(0x");78mpz_out_str (stdout, 16, x);79}80printf (")\n");818283mpz_set_ui (x, 1);84mpz_mul_2exp (x, x, limb + 1); /* x=2^(limb+1) */85mpz_init (y);86mpz_set_ui (y, 10000);87mpz_mul (x, x, y); /* x=2^(limb+1)*10^4 */88mpz_set_ui (y, 27182); /* exp(1)*10^4 */89mpz_tdiv_q (x, x, y); /* x=2^(limb+1)/exp(1) */90printf ("\n/* is 2^(GMP_LIMB_BITS+1)/exp(1) */\n");91printf ("#define FAC2OVERE CNST_LIMB(0x");92mpz_out_str (stdout, 16, x);93printf (")\n");949596printf97("\n/* FACMULn is largest odd x such that x*(x+2)*...*(x+2(n-1))<=2^GMP_NUMB_BITS-1 */\n\n");98mpz_init (z);99mpz_init (t);100for (a = 2; a <= 4; a++)101{102mpz_set_ui (x, 1);103mpz_mul_2exp (x, x, numb);104mpz_root (x, x, a);105/* so x is approx sol */106if (mpz_even_p (x))107mpz_sub_ui (x, x, 1);108mpz_set_ui (y, 1);109mpz_mul_2exp (y, y, numb);110mpz_sub_ui (y, y, 1);111/* decrement x until we are <= real sol */112do113{114mpz_sub_ui (x, x, 2);115odd_products (t, x, a);116if (mpz_cmp (t, y) <= 0)117break;118}119while (1);120/* increment x until > real sol */121do122{123mpz_add_ui (x, x, 2);124odd_products (t, x, a);125if (mpz_cmp (t, y) > 0)126break;127}128while (1);129/* dec once to get real sol */130mpz_sub_ui (x, x, 2);131printf ("#define FACMUL%lu CNST_LIMB(0x", a);132mpz_out_str (stdout, 16, x);133printf (")\n");134}135136return 0;137}138139int140main (int argc, char *argv[])141{142int nail_bits, limb_bits, numb_bits;143144if (argc != 3)145{146fprintf (stderr, "Usage: gen-fac_ui limbbits nailbits\n");147exit (1);148}149limb_bits = atoi (argv[1]);150nail_bits = atoi (argv[2]);151numb_bits = limb_bits - nail_bits;152if (limb_bits < 0 || nail_bits < 0 || numb_bits < 0)153{154fprintf (stderr, "Invalid limb/nail bits %d,%d\n", limb_bits,155nail_bits);156exit (1);157}158gen_consts (numb_bits, nail_bits, limb_bits);159return 0;160}161162163