-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg6.c
48 lines (36 loc) · 1.02 KB
/
prg6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
// Function to compute the multiplicative inverse of a modulo m
void multiplicative_inverse(const mpz_t a, const mpz_t m) {
mpz_t gcd;
mpz_init(gcd);
// Compute the greatest common divisor of a and m
mpz_gcd(gcd, a, m);
// Check if gcd(a, m) equals 1 (i.e., a and m are coprime)
if (mpz_cmp_ui(gcd, 1) == 0) {
printf("Y");
// Compute the multiplicative inverse of a modulo m
mpz_t inv;
mpz_init(inv);
mpz_invert(inv, a, m);
// Print the multiplicative inverse
gmp_printf(" %Zd", inv);
// Clear memory
mpz_clear(inv);
} else {
printf("N");
}
mpz_clear(gcd);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
return 1;
}
mpz_t a, m;
mpz_init_set_str(a, argv[1], 10);
mpz_init_set_str(m, argv[2], 10);
multiplicative_inverse(a, m);
mpz_clears(a, m, NULL);
return 0;
}