Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -9,4 +9,6 @@ Or in other words, such that:
It can be shown that such an inverse exists if and only if a and m are [[wp:coprime|coprime]], but we will ignore this for this task.
Either by implementing the algorithm, by using a dedicated library or by using a builtin function in your language, compute the modular inverse of 42 modulo 2017.
Either by implementing the algorithm, by using a dedicated library
or by using a builtin function in your language,
compute the modular inverse of 42 modulo 2017.

View file

@ -0,0 +1,27 @@
# syntax: GAWK -f MODULAR_INVERSE.AWK
# converted from C
BEGIN {
printf("%s\n",mod_inv(42,2017))
exit(0)
}
function mod_inv(a,b, b0,t,q,x0,x1) {
b0 = b
x0 = 0
x1 = 1
if (b == 1) {
return(1)
}
while (a > 1) {
q = int(a / b)
t = b
b = int(a % b)
a = t
t = x0
x0 = x1 - q * x0
x1 = t
}
if (x1 < 0) {
x1 += b0
}
return(x1)
}

View file

@ -0,0 +1,11 @@
package Mod_Inv is
procedure X_GCD(A, B: in Natural; D, X, Y: out Integer);
-- the Extended Euclidean Algorithm
-- finds (D, X, Y) with D = GCD(A, B) = A*X + B*Y
function Inverse(A, M: Integer) return Integer;
-- computes the multiplicative inverse Inv_A of A mod M, using X_GCD
-- raises Constraint_Error if Inv_A does not exist
end Mod_Inv;

View file

@ -1,6 +1,4 @@
with Ada.Text_IO;
procedure Mod_Inv is
package body Mod_Inv is
procedure X_GCD(A, B: in Natural; D, X, Y: out Integer) is
-- the Extended Euclidean Algorithm
@ -35,8 +33,4 @@ procedure Mod_Inv is
end if;
end Inverse;
begin
Ada.Text_IO.Put_Line(Natural'Image(Inverse(42, 2017)));
-- Ada.Text_IO.Put_Line(Natural'Image(Inverse(154, 3311)));
-- The above would raise CONSTRAINT_ERROR : GCD ( 154, 3311 ) = 77 /= 1
end Mod_Inv;

View file

@ -0,0 +1,9 @@
with Ada.Text_IO; with Mod_Inv; use Mod_Inv, Ada.text_IO;
procedure Mod_Inv_Test is
begin
-- Put_Line(Natural'Image(Inverse(154, 3311)));
-- The above would raise CONSTRAINT_ERROR : GCD ( 154, 3311 ) = 77 /= 1
Put_Line(Natural'Image(Inverse(42, 2017)));
end Mod_Inv_Test;

View 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;
}

View 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;
}

View file

@ -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;
}

View file

@ -0,0 +1,23 @@
;;
;; Calculates the GCD of a and b based on the Extended Euclidean Algorithm. The function also returns
;; the Bézout coefficients s and t, such that gcd(a, b) = as + bt.
;;
;; The algorithm is described on page http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Iterative_method_2
;;
(defun egcd (a b)
(do ((r (cons b a) (cons (- (cdr r) (* (car r) q)) (car r))) ; (r+1 r) i.e. the latest is first.
(s (cons 0 1) (cons (- (cdr s) (* (car s) q)) (car s))) ; (s+1 s)
(u (cons 1 0) (cons (- (cdr u) (* (car u) q)) (car u))) ; (t+1 t)
(q nil))
((zerop (car r)) (values (cdr r) (cdr s) (cdr u))) ; exit when r+1 = 0 and return r s t
(setq q (floor (/ (cdr r) (car r)))))) ; inside loop; calculate the q
;;
;; Calculates the inverse module for a = 1 (mod m).
;;
;; Note: The inverse is only defined when a and m are coprimes, i.e. gcd(a, m) = 1.”
;;
(defun invmod (a m)
(multiple-value-bind (r s k) (egcd a m)
(unless (= 1 r) (error "invmod: Values ~a and ~a are not coprimes." a m))
s))

View file

@ -0,0 +1 @@
PowerMod[a,-1,m]

View file

@ -0,0 +1,33 @@
*process source attributes xref or(!);
/*--------------------------------------------------------------------
* 13.07.2015 Walter Pachl
*-------------------------------------------------------------------*/
minv: Proc Options(main);
Dcl (x,y) Bin Fixed(31);
x=42;
y=2017;
Put Edit('modular inverse of',x,' by ',y,' ---> ',modinv(x,y))
(Skip,3(a,f(4)));
modinv: Proc(a,b) Returns(Bin Fixed(31));
Dcl (a,b,ob,ox,d,t) Bin Fixed(31);
ob=b;
ox=0;
d=1;
If b=1 Then;
Else Do;
Do While(a>1);
q=a/b;
r=mod(a,b);
a=b;
b=r;
t=ox;
ox=d-q*ox;
d=t;
End;
End;
If d<0 Then
d=d+ob;
Return(d);
End;
End;

View file

@ -0,0 +1,9 @@
use bigint; say 42->bmodinv(2017);
# or
use Math::ModInt qw/mod/; say mod(42, 2017)->inverse->residue;
# or
use Math::Pari qw/PARI lift/; say lift PARI "Mod(1/42,2017)";
# or
use Math::GMP qw/:constant/; say 42->bmodinv(2017);
# or
use ntheory qw/invmod/; say invmod(42, 2017);

View file

@ -0,0 +1,15 @@
sub invmod {
my($a,$n) = @_;
my($t,$nt,$r,$nr) = (0, 1, $n, $a % $n);
while ($nr != 0) {
# Use this instead of int($r/$nr) to get exact unsigned integer answers
my $quot = int( ($r - ($r % $nr)) / $nr );
($nt,$t) = ($t-$quot*$nt,$nt);
($nr,$r) = ($r-$quot*$nr,$nr);
}
return if $r > 1;
$t += $n if $t < 0;
$t;
}
say invmod(42,2017);

View file

@ -1,2 +0,0 @@
use Math::ModInt qw(mod);
print mod(42, 2017)->inverse

View file

@ -0,0 +1,17 @@
(de modinv (A B)
(let (B0 B X0 0 X1 1 Q 0 T1 0)
(while (< 1 A)
(setq
Q (/ A B)
T1 B
B (% A B)
A T1
T1 X0
X0 (- X1 (* Q X0))
X1 T1 ) )
(if (lt0 X1) (+ X1 B0) X1) ) )
(println
(modinv 42 2017) )
(bye)