March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -2,7 +2,7 @@ From [http://en.wikipedia.org/wiki/Modular_multiplicative_inverse Wikipedia]:
|
|||
|
||||
:In [[wp:modular arithmetic|modular arithmetic]], the '''modular multiplicative inverse''' of an [[integer]] ''a'' [[wp:modular arithmetic|modulo]] ''m'' is an integer ''x'' such that
|
||||
|
||||
::<math>a^{-1} \equiv x \pmod{m}.</math>
|
||||
::<math>a\,x \equiv 1 \pmod{m}.</math>
|
||||
|
||||
Or in other words, such that:
|
||||
:<math>\exists k \in\mathbf{Z},\qquad a\, x = 1 + k\,m</math>
|
||||
|
|
|
|||
19
Task/Modular-inverse/AutoHotkey/modular-inverse.ahk
Normal file
19
Task/Modular-inverse/AutoHotkey/modular-inverse.ahk
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
MsgBox, % ModInv(42, 2017)
|
||||
|
||||
ModInv(a, b) {
|
||||
if (b = 1)
|
||||
return 1
|
||||
b0 := b, x0 := 0, x1 :=1
|
||||
while (a > 1) {
|
||||
q := a // b
|
||||
, t := b
|
||||
, b := Mod(a, b)
|
||||
, a := t
|
||||
, t := x0
|
||||
, x0 := x1 - q * x0
|
||||
, x1 := t
|
||||
}
|
||||
if (x1 < 0)
|
||||
x1 += b0
|
||||
return x1
|
||||
}
|
||||
|
|
@ -1,20 +1,18 @@
|
|||
#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;
|
||||
#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(void) {
|
||||
printf("%d\n", mul_inv(42, 2017));
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
1
Task/Modular-inverse/Maple/modular-inverse.maple
Normal file
1
Task/Modular-inverse/Maple/modular-inverse.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
1/42 mod 2017;
|
||||
|
|
@ -5,7 +5,7 @@ sub inverse($n, :$modulo) {
|
|||
($q, $c, $d) = ($d div $c, $d % $c, $c);
|
||||
($uc, $vc, $ud, $vd) = ($ud - $q*$uc, $vd - $q*$vc, $uc, $vc);
|
||||
}
|
||||
return $ud < 0 ?? $ud + $modulo !! $ud;
|
||||
return $ud % $modulo;
|
||||
}
|
||||
|
||||
say inverse 42, :modulo(2017)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue