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