github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpz/t-fac_ui.c (about) 1 /* Exercise mpz_fac_ui and mpz_2fac_ui. 2 3 Copyright 2000-2002, 2012, 2015 Free Software Foundation, Inc. 4 5 This file is part of the GNU MP Library test suite. 6 7 The GNU MP Library test suite is free software; you can redistribute it 8 and/or modify it under the terms of the GNU General Public License as 9 published by the Free Software Foundation; either version 3 of the License, 10 or (at your option) any later version. 11 12 The GNU MP Library test suite is distributed in the hope that it will be 13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 15 Public License for more details. 16 17 You should have received a copy of the GNU General Public License along with 18 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ 19 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include "gmp.h" 23 #include "gmp-impl.h" 24 #include "tests.h" 25 26 27 /* Usage: t-fac_ui [x|num] 28 29 With no arguments testing goes up to the initial value of "limit" below. 30 With a number argument tests are carried that far, or with a literal "x" 31 tests are continued without limit (this being meant only for development 32 purposes). */ 33 34 35 int 36 main (int argc, char *argv[]) 37 { 38 unsigned long n, m; 39 unsigned long limit = 2222; 40 mpz_t df[2], f, r; 41 42 tests_start (); 43 44 if (argc > 1 && argv[1][0] == 'x') 45 limit = ULONG_MAX; 46 else if (argc > 1) 47 limit = atoi (argv[1]); 48 49 /* for small limb testing */ 50 limit = MIN (limit, MP_LIMB_T_MAX); 51 52 mpz_init_set_ui (df[0], 1); /* 0!! = 1 */ 53 mpz_init_set_ui (df[1], 1); /* -1!! = 1 */ 54 mpz_init_set_ui (f, 1); /* 0! = 1 */ 55 mpz_init (r); 56 57 for (n = 0, m = 0; n < limit; n++) 58 { 59 mpz_fac_ui (r, n); 60 MPZ_CHECK_FORMAT (r); 61 62 if (mpz_cmp (f, r) != 0) 63 { 64 printf ("mpz_fac_ui(%lu) wrong\n", n); 65 printf (" got "); mpz_out_str (stdout, 10, r); printf("\n"); 66 printf (" want "); mpz_out_str (stdout, 10, f); printf("\n"); 67 abort (); 68 } 69 70 mpz_2fac_ui (r, n); 71 MPZ_CHECK_FORMAT (r); 72 73 if (mpz_cmp (df[m], r) != 0) 74 { 75 printf ("mpz_2fac_ui(%lu) wrong\n", n); 76 printf (" got "); mpz_out_str (stdout, 10, r); printf("\n"); 77 printf (" want "); mpz_out_str (stdout, 10, df[m]); printf("\n"); 78 abort (); 79 } 80 81 m ^= 1; 82 mpz_mul_ui (df[m], df[m], n+1); /* (n+1)!! = (n-1)!! * (n+1) */ 83 mpz_mul_ui (f, f, n+1); /* (n+1)! = n! * (n+1) */ 84 } 85 86 n = 2097169; /* a prime = 1 mod 4*/ 87 if (n / 2 > MP_LIMB_T_MAX) 88 n = 131041; /* a smaller prime :-) */ 89 mpz_fac_ui (f, n / 2); /* ((n-1)/2)! */ 90 m = mpz_fdiv_ui (f, n); /* ((n-1)/2)! mod n*/ 91 mpz_set_ui (f, m); 92 mpz_mul_ui (f, f, m); /* (((n-1)/2)!)^2 */ 93 m = mpz_fdiv_ui (f, n); /* (((n-1)/2)!)^2 mod n*/ 94 if ( m != n - 1) 95 { 96 printf ("mpz_fac_ui(%lu) wrong\n", n / 2); 97 printf (" al-Haytham's theorem not verified: got %lu, expected %lu.\n", m, n - 1); 98 abort (); 99 } 100 101 mpz_clear (df[0]); 102 mpz_clear (df[1]); 103 mpz_clear (f); 104 mpz_clear (r); 105 106 tests_end (); 107 108 exit (0); 109 }