github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/demos/isprime.c (about)

     1  /* Classify numbers as probable primes, primes or composites.
     2     With -q return true if the following argument is a (probable) prime.
     3  
     4  Copyright 1999, 2000, 2002, 2005, 2012 Free Software Foundation, Inc.
     5  
     6  This program is free software; you can redistribute it and/or modify it under
     7  the terms of the GNU General Public License as published by the Free Software
     8  Foundation; either version 3 of the License, or (at your option) any later
     9  version.
    10  
    11  This program is distributed in the hope that it will be useful, but WITHOUT ANY
    12  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
    13  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
    14  
    15  You should have received a copy of the GNU General Public License along with
    16  this program.  If not, see https://www.gnu.org/licenses/.  */
    17  
    18  #include <stdlib.h>
    19  #include <string.h>
    20  #include <stdio.h>
    21  #include "gmp.h"
    22  
    23  char *progname;
    24  
    25  void
    26  print_usage_and_exit ()
    27  {
    28    fprintf (stderr, "usage: %s -q nnn\n", progname);
    29    fprintf (stderr, "usage: %s nnn ...\n", progname);
    30    exit (-1);
    31  }
    32  
    33  int
    34  main (int argc, char **argv)
    35  {
    36    mpz_t n;
    37    int i;
    38  
    39    progname = argv[0];
    40  
    41    if (argc < 2)
    42      print_usage_and_exit ();
    43  
    44    mpz_init (n);
    45  
    46    if (argc == 3 && strcmp (argv[1], "-q") == 0)
    47      {
    48        if (mpz_set_str (n, argv[2], 0) != 0)
    49  	print_usage_and_exit ();
    50        exit (mpz_probab_prime_p (n, 25) == 0);
    51      }
    52  
    53    for (i = 1; i < argc; i++)
    54      {
    55        int class;
    56        if (mpz_set_str (n, argv[i], 0) != 0)
    57  	print_usage_and_exit ();
    58        class = mpz_probab_prime_p (n, 25);
    59        mpz_out_str (stdout, 10, n);
    60        if (class == 0)
    61  	puts (" is composite");
    62        else if (class == 1)
    63  	puts (" is a probable prime");
    64        else /* class == 2 */
    65  	puts (" is a prime");
    66      }
    67    exit (0);
    68  }