github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpz/t-primorial_ui.c (about)

     1  /* Exercise mpz_primorial_ui.
     2  
     3  Copyright 2000-2002, 2012 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-primorial_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  static int isprime (unsigned long int t);
    35  
    36  int
    37  main (int argc, char *argv[])
    38  {
    39    unsigned long  n;
    40    unsigned long  limit = 2222;
    41    mpz_t          f, r;
    42  
    43    tests_start ();
    44  
    45    if (argc > 1 && argv[1][0] == 'x')
    46      limit = ULONG_MAX;
    47    else if (argc > 1)
    48      limit = atoi (argv[1]);
    49  
    50    /* for small limb testing */
    51    limit = MIN (limit, MP_LIMB_T_MAX);
    52  
    53    mpz_init_set_ui (f, 1);  /* 0# = 1 */
    54    mpz_init (r);
    55  
    56    for (n = 0; n < limit; n++)
    57      {
    58        mpz_primorial_ui (r, n);
    59        MPZ_CHECK_FORMAT (r);
    60  
    61        if (mpz_cmp (f, r) != 0)
    62          {
    63            printf ("mpz_primorial_ui(%lu) wrong\n", n);
    64            printf ("  got  "); mpz_out_str (stdout, 10, r); printf("\n");
    65            printf ("  want "); mpz_out_str (stdout, 10, f); printf("\n");
    66            abort ();
    67          }
    68  
    69        if (isprime (n+1))
    70  	mpz_mul_ui (f, f, n+1);  /* p# = (p-1)# * (p) */
    71      }
    72  
    73    mpz_clear (f);
    74    mpz_clear (r);
    75  
    76    tests_end ();
    77  
    78    exit (0);
    79  }
    80  
    81  static int
    82  isprime (unsigned long int t)
    83  {
    84    unsigned long int q, r, d;
    85  
    86    if (t < 3 || (t & 1) == 0)
    87      return t == 2;
    88  
    89    for (d = 3, r = 1; r != 0; d += 2)
    90      {
    91        q = t / d;
    92        r = t - q * d;
    93        if (q < d)
    94  	return 1;
    95      }
    96    return 0;
    97  }