Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 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;
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
#include<stdio.h>
|
||||
int modularinverse( int a, int b){
|
||||
int c=b/a,x=0;
|
||||
do{
|
||||
if((a<b)||(a==1)) x=1;
|
||||
if((c*a)%b==1) x=c;
|
||||
else c++;
|
||||
} while(x==0);
|
||||
return x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a,b;
|
||||
printf("Unesite brojeve a i b ");
|
||||
scanf("%d%d",&a,&b);
|
||||
printf("Modularni inverz brojeva %d i %d je %d ",a,b,modularinverse(a,b));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue