3 * Command line tool for prime search.
7 /* nettle, low-level cryptographics library
9 * Copyright (C) 2007 Niels Möller
11 * The nettle library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or (at your
14 * option) any later version.
16 * The nettle library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 * License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with the nettle library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
42 fprintf(stderr, "Usage: next-prime [OPTIONS] number\n\n"
44 " --help Display this message.\n"
45 " -v, --verbose Display timing information.\n"
46 " --factorial Use factorial of input number.\n"
47 " -s --sieve-limit Number of primes to use for sieving.\n");
51 main(int argc, char **argv)
59 int prime_limit = 200;
64 enum { OPT_FACTORIAL = -100 };
65 static const struct option options[] =
67 /* Name, args, flag, val */
68 { "help", no_argument, NULL, '?' },
69 { "verbose", no_argument, NULL, 'v' },
70 { "factorial", no_argument, NULL, 'f' },
71 { "sieve-limit", required_argument, NULL, 's' },
75 while ( (c = getopt_long(argc, argv, "v?s:", options, NULL)) != -1)
88 prime_limit = atoi(optarg);
115 arg = strtol(argv[0], &end, 0);
118 fprintf(stderr, "Invalid number.\n");
123 else if (mpz_set_str(n, argv[0], 0))
125 fprintf(stderr, "Invalid number.\n");
129 if (mpz_cmp_ui(n, 2) <= 0)
138 nettle_next_prime(p, n, 25, prime_limit, NULL, NULL);
141 mpz_out_str(stdout, 10, p);
151 /* Avoid using gmp_fprintf, to stay compatible with gmp-3.1. */
152 fprintf(stderr, "bit size: %lu, diff: ", (unsigned long) mpz_sizeinbase(p, 2));
153 mpz_out_str(stderr, 10, d);
154 fprintf(stderr, ", total time: %.3g s\n",
155 (double)(end - start) / CLOCKS_PER_SEC);