|
1 /* |
|
2 * lap.c |
|
3 * |
|
4 * Find least annihilating power of a mod m |
|
5 * |
|
6 * This Source Code Form is subject to the terms of the Mozilla Public |
|
7 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
9 |
|
10 #include <stdio.h> |
|
11 #include <stdlib.h> |
|
12 #include <signal.h> |
|
13 |
|
14 #include "mpi.h" |
|
15 |
|
16 void sig_catch(int ign); |
|
17 |
|
18 int g_quit = 0; |
|
19 |
|
20 int main(int argc, char *argv[]) |
|
21 { |
|
22 mp_int a, m, p, k; |
|
23 |
|
24 if(argc < 3) { |
|
25 fprintf(stderr, "Usage: %s <a> <m>\n", argv[0]); |
|
26 return 1; |
|
27 } |
|
28 |
|
29 mp_init(&a); |
|
30 mp_init(&m); |
|
31 mp_init(&p); |
|
32 mp_add_d(&p, 1, &p); |
|
33 |
|
34 mp_read_radix(&a, argv[1], 10); |
|
35 mp_read_radix(&m, argv[2], 10); |
|
36 |
|
37 mp_init_copy(&k, &a); |
|
38 |
|
39 signal(SIGINT, sig_catch); |
|
40 #ifndef __OS2__ |
|
41 signal(SIGHUP, sig_catch); |
|
42 #endif |
|
43 signal(SIGTERM, sig_catch); |
|
44 |
|
45 while(mp_cmp(&p, &m) < 0) { |
|
46 if(g_quit) { |
|
47 int len; |
|
48 char *buf; |
|
49 |
|
50 len = mp_radix_size(&p, 10); |
|
51 buf = malloc(len); |
|
52 mp_toradix(&p, buf, 10); |
|
53 |
|
54 fprintf(stderr, "Terminated at: %s\n", buf); |
|
55 free(buf); |
|
56 return 1; |
|
57 } |
|
58 if(mp_cmp_d(&k, 1) == 0) { |
|
59 int len; |
|
60 char *buf; |
|
61 |
|
62 len = mp_radix_size(&p, 10); |
|
63 buf = malloc(len); |
|
64 mp_toradix(&p, buf, 10); |
|
65 |
|
66 printf("%s\n", buf); |
|
67 |
|
68 free(buf); |
|
69 break; |
|
70 } |
|
71 |
|
72 mp_mulmod(&k, &a, &m, &k); |
|
73 mp_add_d(&p, 1, &p); |
|
74 } |
|
75 |
|
76 if(mp_cmp(&p, &m) >= 0) |
|
77 printf("No annihilating power.\n"); |
|
78 |
|
79 mp_clear(&p); |
|
80 mp_clear(&m); |
|
81 mp_clear(&a); |
|
82 return 0; |
|
83 } |
|
84 |
|
85 void sig_catch(int ign) |
|
86 { |
|
87 g_quit = 1; |
|
88 } |