151c586b8Smrg /* Classify numbers as probable primes, primes or composites.
251c586b8Smrg With -q return true if the following argument is a (probable) prime.
351c586b8Smrg
4dab47db4Smrg Copyright 1999, 2000, 2002, 2005, 2012 Free Software Foundation, Inc.
551c586b8Smrg
651c586b8Smrg This program is free software; you can redistribute it and/or modify it under
751c586b8Smrg the terms of the GNU General Public License as published by the Free Software
851c586b8Smrg Foundation; either version 3 of the License, or (at your option) any later
951c586b8Smrg version.
1051c586b8Smrg
1151c586b8Smrg This program is distributed in the hope that it will be useful, but WITHOUT ANY
1251c586b8Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1351c586b8Smrg PARTICULAR PURPOSE. See the GNU General Public License for more details.
1451c586b8Smrg
1551c586b8Smrg You should have received a copy of the GNU General Public License along with
16ce543368Smrg this program. If not, see https://www.gnu.org/licenses/. */
1751c586b8Smrg
1851c586b8Smrg #include <stdlib.h>
1951c586b8Smrg #include <string.h>
2051c586b8Smrg #include <stdio.h>
21*72c7faa4Smrg
2251c586b8Smrg #include "gmp.h"
2351c586b8Smrg
2451c586b8Smrg char *progname;
2551c586b8Smrg
2651c586b8Smrg void
print_usage_and_exit()2751c586b8Smrg print_usage_and_exit ()
2851c586b8Smrg {
2951c586b8Smrg fprintf (stderr, "usage: %s -q nnn\n", progname);
3051c586b8Smrg fprintf (stderr, "usage: %s nnn ...\n", progname);
3151c586b8Smrg exit (-1);
3251c586b8Smrg }
3351c586b8Smrg
3451c586b8Smrg int
main(int argc,char ** argv)3551c586b8Smrg main (int argc, char **argv)
3651c586b8Smrg {
3751c586b8Smrg mpz_t n;
3851c586b8Smrg int i;
3951c586b8Smrg
4051c586b8Smrg progname = argv[0];
4151c586b8Smrg
4251c586b8Smrg if (argc < 2)
4351c586b8Smrg print_usage_and_exit ();
4451c586b8Smrg
4551c586b8Smrg mpz_init (n);
4651c586b8Smrg
4751c586b8Smrg if (argc == 3 && strcmp (argv[1], "-q") == 0)
4851c586b8Smrg {
4951c586b8Smrg if (mpz_set_str (n, argv[2], 0) != 0)
5051c586b8Smrg print_usage_and_exit ();
51dab47db4Smrg exit (mpz_probab_prime_p (n, 25) == 0);
5251c586b8Smrg }
5351c586b8Smrg
5451c586b8Smrg for (i = 1; i < argc; i++)
5551c586b8Smrg {
5651c586b8Smrg int class;
5751c586b8Smrg if (mpz_set_str (n, argv[i], 0) != 0)
5851c586b8Smrg print_usage_and_exit ();
59dab47db4Smrg class = mpz_probab_prime_p (n, 25);
6051c586b8Smrg mpz_out_str (stdout, 10, n);
6151c586b8Smrg if (class == 0)
6251c586b8Smrg puts (" is composite");
6351c586b8Smrg else if (class == 1)
6451c586b8Smrg puts (" is a probable prime");
6551c586b8Smrg else /* class == 2 */
6651c586b8Smrg puts (" is a prime");
6751c586b8Smrg }
6851c586b8Smrg exit (0);
6951c586b8Smrg }
70