langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
8
Task/Modular-inverse/OCaml/modular-inverse-1.ocaml
Normal file
8
Task/Modular-inverse/OCaml/modular-inverse-1.ocaml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
let mul_inv a = function 1 -> 1 | b ->
|
||||
let rec aux a b x0 x1 =
|
||||
if a <= 1 then x1 else
|
||||
if b = 0 then failwith "mul_inv" else
|
||||
aux b (a mod b) (x1 - (a / b) * x0) x0
|
||||
in
|
||||
let x = aux a b 0 1 in
|
||||
if x < 0 then x + b else x
|
||||
11
Task/Modular-inverse/OCaml/modular-inverse-2.ocaml
Normal file
11
Task/Modular-inverse/OCaml/modular-inverse-2.ocaml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
let rec gcd_ext a = function
|
||||
| 0 -> (1, 0, a)
|
||||
| b ->
|
||||
let s, t, g = gcd_ext b (a mod b) in
|
||||
(t, s - (a / b) * t, g)
|
||||
|
||||
let mod_inv a m =
|
||||
let mk_pos x = if x < 0 then x + m else x in
|
||||
match gcd_ext a m with
|
||||
| i, _, 1 -> mk_pos i
|
||||
| _ -> failwith "mod_inv"
|
||||
11
Task/Modular-inverse/Perl-6/modular-inverse.pl6
Normal file
11
Task/Modular-inverse/Perl-6/modular-inverse.pl6
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
sub inverse($n, :$modulo) {
|
||||
my ($c, $d, $uc, $vc, $ud, $vd) = ($n % $modulo, $modulo, 1, 0, 0, 1);
|
||||
my $q;
|
||||
while $c != 0 {
|
||||
($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;
|
||||
}
|
||||
|
||||
say inverse 42, :modulo(2017)
|
||||
19
Task/Modular-inverse/Run-BASIC/modular-inverse.run
Normal file
19
Task/Modular-inverse/Run-BASIC/modular-inverse.run
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
print multInv(42, 2017)
|
||||
end
|
||||
|
||||
function multInv(a,b)
|
||||
b0 = b
|
||||
multInv = 1
|
||||
if b = 1 then goto [endFun]
|
||||
while a > 1
|
||||
q = a / b
|
||||
t = b
|
||||
b = a mod b
|
||||
a = t
|
||||
t = x0
|
||||
x0 = multInv - q * x0
|
||||
multInv = int(t)
|
||||
wend
|
||||
if multInv < 0 then multInv = multInv + b0
|
||||
[endFun]
|
||||
end function
|
||||
7
Task/Modular-inverse/XPL0/modular-inverse.xpl0
Normal file
7
Task/Modular-inverse/XPL0/modular-inverse.xpl0
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
code IntOut=11, Text=12;
|
||||
int X;
|
||||
def A=42, M=2017;
|
||||
[for X:= 2 to M-1 do
|
||||
if rem(A*X/M) = 1 then [IntOut(0, X); exit];
|
||||
Text(0, "Does not exist");
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue