~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/demos/isprime.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 1999, 2000, 2002 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
void
 
25
print_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
 
 
32
int
 
33
main (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
}