1 | /* |
2 | Name: findprime.c |
3 | Purpose: Find probable primes. |
4 | Author: M. J. Fromberger |
5 | |
6 | Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved. |
7 | |
8 | Notes: |
9 | Find the first prime number in sequence starting from the given value. |
10 | Demonstrates the use of mp_int_find_prime(). |
11 | |
12 | Permission is hereby granted, free of charge, to any person obtaining a copy |
13 | of this software and associated documentation files (the "Software"), to deal |
14 | in the Software without restriction, including without limitation the rights |
15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16 | copies of the Software, and to permit persons to whom the Software is |
17 | furnished to do so, subject to the following conditions: |
18 | |
19 | The above copyright notice and this permission notice shall be included in |
20 | all copies or substantial portions of the Software. |
21 | |
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28 | SOFTWARE. |
29 | */ |
30 | |
31 | #include <stdio.h> |
32 | |
33 | #include "iprime.h" |
34 | |
35 | int main(int argc, char *argv[]) { |
36 | char buf[4096]; |
37 | mpz_t seed; |
38 | mp_result res; |
39 | |
40 | if (argc < 2) { |
41 | fprintf(stderr, format: "Usage: %s <start-value>\n" , argv[0]); |
42 | return 1; |
43 | } |
44 | |
45 | mp_int_init(z: &seed); |
46 | if ((res = mp_int_read_string(z: &seed, radix: 10, str: argv[1])) != MP_OK) { |
47 | fprintf(stderr, format: "%s: error reading `%s': %d\n" , argv[0], argv[1], res); |
48 | return 2; |
49 | } |
50 | |
51 | if (mp_int_compare_value(z: &seed, v: 131) <= 0) { |
52 | fprintf(stderr, format: "%s: please enter a start value > 131\n" , argv[0]); |
53 | return 1; |
54 | } |
55 | |
56 | if ((res = mp_int_find_prime(z: &seed)) != MP_TRUE) { |
57 | fprintf(stderr, format: "%s: error finding prime: %d\n" , argv[0], res); |
58 | return 2; |
59 | } |
60 | |
61 | mp_int_to_string(z: &seed, radix: 10, str: buf, limit: sizeof(buf)); |
62 | printf(format: "=> %s\n" , buf); |
63 | |
64 | mp_int_clear(z: &seed); |
65 | |
66 | return 0; |
67 | } |
68 | |