source: trunk/third/gmp/demos/isprime.c @ 18191

Revision 18191, 1.7 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Classify numbers as probable primes, primes or composites.
2   With -q return true if the folowing argument is a (probable) prime.
3
4Copyright 1999, 2000, 2002 Free Software Foundation, Inc.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; either version 2 of the License, or (at your option) any later
9version.
10
11This program is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19#include <stdio.h>
20#include "gmp.h"
21
22char *progname;
23
24void
25print_usage_and_exit ()
26{
27  fprintf (stderr, "usage: %s -q nnn\n", progname);
28  fprintf (stderr, "usage: %s nnn ...\n", progname);
29  exit (-1);
30}
31
32int
33main (int argc, char **argv)
34{
35  mpz_t n;
36  int i;
37
38  progname = argv[0];
39
40  if (argc < 2)
41    print_usage_and_exit ();
42
43  mpz_init (n);
44
45  if (argc == 3 && strcmp (argv[1], "-q") == 0)
46    {
47      if (mpz_set_str (n, argv[2], 0) != 0)
48        print_usage_and_exit ();
49      exit (mpz_probab_prime_p (n, 5) == 0);
50    }
51
52  for (i = 1; i < argc; i++)
53    {
54      int class;
55      if (mpz_set_str (n, argv[i], 0) != 0)
56        print_usage_and_exit ();
57      class = mpz_probab_prime_p (n, 5);
58      mpz_out_str (stdout, 10, n);
59      if (class == 0)
60        puts (" is composite");
61      else if (class == 1)
62        puts (" is a probable prime");
63      else /* class == 2 */
64        puts (" is a prime");
65    }
66  exit (0);
67}
Note: See TracBrowser for help on using the repository browser.