Annotation of OpenXM_contrib/gmp/demos/isprime.c, Revision 1.1
1.1 ! maekawa 1: /* Classify numbers as probable primes, primes or composites.
! 2: With -q return true if the folowing argument is a (probable) prime.
! 3:
! 4: Copyright (C) 1999, 2000 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 2 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, write to the Free Software Foundation, Inc., 59 Temple
! 17: Place - Suite 330, Boston, MA 02111-1307, USA. */
! 18:
! 19: #include <stdio.h>
! 20: #include "gmp.h"
! 21:
! 22: char *progname;
! 23:
! 24: main (int argc, char **argv)
! 25: {
! 26: mpz_t n;
! 27: int i;
! 28:
! 29: progname = argv[0];
! 30:
! 31: if (argc < 2)
! 32: print_usage_and_exit ();
! 33:
! 34: mpz_init (n);
! 35:
! 36: if (argc == 3 && strcmp (argv[1], "-q") == 0)
! 37: {
! 38: if (mpz_set_str (n, argv[2], 0) != 0)
! 39: print_usage_and_exit ();
! 40: exit (mpz_probab_prime_p (n, 5) == 0);
! 41: }
! 42:
! 43: for (i = 1; i < argc; i++)
! 44: {
! 45: int class;
! 46: if (mpz_set_str (n, argv[i], 0) != 0)
! 47: print_usage_and_exit ();
! 48: class = mpz_probab_prime_p (n, 5);
! 49: mpz_out_str (stdout, 10, n);
! 50: if (class == 0)
! 51: puts (" is composite");
! 52: else if (class == 1)
! 53: puts (" is a probable prime");
! 54: else /* class == 2 */
! 55: puts (" is a prime");
! 56: }
! 57: exit (0);
! 58: }
! 59:
! 60: print_usage_and_exit ()
! 61: {
! 62: fprintf (stderr, "usage: %s -q nnn\n", progname);
! 63: fprintf (stderr, "usage: %s nnn ...\n", progname);
! 64: exit (-1);
! 65: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>