1 /* Exercise mpz_fac_ui. 2 3 Copyright 2000, 2001, 2002 Free Software Foundation, Inc. 4 5 This file is part of the GNU MP Library. 6 7 The GNU MP Library is free software; you can redistribute it and/or modify 8 it under the terms of the GNU Lesser General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or (at your 10 option) any later version. 11 12 The GNU MP Library is distributed in the hope that it will be useful, but 13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15 License for more details. 16 17 You should have received a copy of the GNU Lesser General Public License 18 along with the GNU MP Library. If not, see http://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; 39 unsigned long limit = 1500; 40 mpz_t 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 (f, 1); /* 0! = 1 */ 53 mpz_init (r); 54 55 for (n = 0; n < limit; n++) 56 { 57 mpz_fac_ui (r, n); 58 MPZ_CHECK_FORMAT (r); 59 60 if (mpz_cmp (f, r) != 0) 61 { 62 printf ("mpz_fac_ui(%lu) wrong\n", n); 63 printf (" got "); mpz_out_str (stdout, 10, r); printf("\n"); 64 printf (" want "); mpz_out_str (stdout, 10, f); printf("\n"); 65 abort (); 66 } 67 68 mpz_mul_ui (f, f, n+1); /* (n+1)! = n! * (n+1) */ 69 } 70 71 mpz_clear (f); 72 mpz_clear (r); 73 74 tests_end (); 75 76 exit (0); 77 } 78