update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
21
Task/Modular-inverse/C++/modular-inverse.cpp
Normal file
21
Task/Modular-inverse/C++/modular-inverse.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
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) {
|
||||
cout<<mul_inv(42, 2017)<<endl;
|
||||
return 0;
|
||||
}
|
||||
2
Task/Modular-inverse/Racket/modular-inverse-1.rkt
Normal file
2
Task/Modular-inverse/Racket/modular-inverse-1.rkt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(require math)
|
||||
(modular-inverse 42 2017)
|
||||
1
Task/Modular-inverse/Racket/modular-inverse-2.rkt
Normal file
1
Task/Modular-inverse/Racket/modular-inverse-2.rkt
Normal file
|
|
@ -0,0 +1 @@
|
|||
1969
|
||||
20
Task/Modular-inverse/Ruby/modular-inverse.rb
Normal file
20
Task/Modular-inverse/Ruby/modular-inverse.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#based on pseudo code from http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Iterative_method_2 and from translating the python implementation.
|
||||
def extended_gcd(a, b)
|
||||
last_remainder, remainder = a.abs, b.abs
|
||||
x, last_x, y, last_y = 0, 1, 1, 0
|
||||
while remainder != 0
|
||||
last_remainder, (quotient, remainder) = remainder, last_remainder.divmod(remainder)
|
||||
x, last_x = last_x - quotient*x, x
|
||||
y, last_y = last_y - quotient*y, y
|
||||
end
|
||||
|
||||
return last_remainder, last_x * (a < 0 ? -1 : 1)
|
||||
end
|
||||
|
||||
def invmod(e, et)
|
||||
g, x = extended_gcd(e, et)
|
||||
if g != 1
|
||||
raise 'Teh maths are broken!'
|
||||
end
|
||||
x % et
|
||||
end
|
||||
43
Task/Modular-inverse/Seed7/modular-inverse.seed7
Normal file
43
Task/Modular-inverse/Seed7/modular-inverse.seed7
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
const func bigInteger: modInverse (in var bigInteger: a,
|
||||
in var bigInteger: b) is func
|
||||
result
|
||||
var bigInteger: modularInverse is 0_;
|
||||
local
|
||||
var bigInteger: b_bak is 0_;
|
||||
var bigInteger: x is 0_;
|
||||
var bigInteger: y is 1_;
|
||||
var bigInteger: lastx is 1_;
|
||||
var bigInteger: lasty is 0_;
|
||||
var bigInteger: temp is 0_;
|
||||
var bigInteger: quotient is 0_;
|
||||
begin
|
||||
if b < 0_ then
|
||||
raise RANGE_ERROR;
|
||||
end if;
|
||||
if a < 0_ and b <> 0_ then
|
||||
a := a mod b;
|
||||
end if;
|
||||
b_bak := b;
|
||||
while b <> 0_ do
|
||||
temp := b;
|
||||
quotient := a div b;
|
||||
b := a rem b;
|
||||
a := temp;
|
||||
|
||||
temp := x;
|
||||
x := lastx - quotient * x;
|
||||
lastx := temp;
|
||||
|
||||
temp := y;
|
||||
y := lasty - quotient * y;
|
||||
lasty := temp;
|
||||
end while;
|
||||
if a = 1_ then
|
||||
modularInverse := lastx;
|
||||
if modularInverse < 0_ then
|
||||
modularInverse +:= b_bak;
|
||||
end if;
|
||||
else
|
||||
raise RANGE_ERROR;
|
||||
end if;
|
||||
end func;
|
||||
Loading…
Add table
Add a link
Reference in a new issue