Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Modular-inverse/C/modular-inverse-1.c
Normal file
20
Task/Modular-inverse/C/modular-inverse-1.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int mul_inv(int a, int b)
|
||||
{
|
||||
int b0 = b, t, q;
|
||||
int x0 = 0, x1 = 1;
|
||||
if (b == 1) return 1;
|
||||
while (a > 1) {
|
||||
q = a / b;
|
||||
t = b, b = a % b, a = t;
|
||||
t = x0, x0 = x1 - q * x0, x1 = t;
|
||||
}
|
||||
if (x1 < 0) x1 += b0;
|
||||
return x1;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("%d\n", mul_inv(42, 2017));
|
||||
return 0;
|
||||
}
|
||||
25
Task/Modular-inverse/C/modular-inverse-2.c
Normal file
25
Task/Modular-inverse/C/modular-inverse-2.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int mul_inv(int a, int b)
|
||||
{
|
||||
int t, nt, r, nr, q, tmp;
|
||||
if (b < 0) b = -b;
|
||||
if (a < 0) a = b - (-a % b);
|
||||
t = 0; nt = 1; r = b; nr = a % b;
|
||||
while (nr != 0) {
|
||||
q = r/nr;
|
||||
tmp = nt; nt = t - q*nt; t = tmp;
|
||||
tmp = nr; nr = r - q*nr; r = tmp;
|
||||
}
|
||||
if (r > 1) return -1; /* No inverse */
|
||||
if (t < 0) t += b;
|
||||
return t;
|
||||
}
|
||||
int main(void) {
|
||||
printf("%d\n", mul_inv(42, 2017));
|
||||
printf("%d\n", mul_inv(40, 1));
|
||||
printf("%d\n", mul_inv(52, -217)); /* Pari semantics for negative modulus */
|
||||
printf("%d\n", mul_inv(-486, 217));
|
||||
printf("%d\n", mul_inv(40, 2018));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue