Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
19
Task/Modular-inverse/Ada/modular-inverse.adb
Normal file
19
Task/Modular-inverse/Ada/modular-inverse.adb
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
with Ada.Text_IO;use Ada.Text_IO;
|
||||
procedure modular_inverse is
|
||||
-- inv_mod calculates the inverse of a mod n. We should have n>0 and, at the end, the contract is a*Result=1 mod n
|
||||
-- If this is false then we raise an exception (don't forget the -gnata option when you compile
|
||||
function inv_mod (a : Integer; n : Positive) return Integer with post=> (a * inv_mod'Result) mod n = 1 is
|
||||
-- To calculate the inverse we do as if we would calculate the GCD with the Euclid extended algorithm
|
||||
-- (but we just keep the coefficient on a)
|
||||
function inverse (a, b, u, v : Integer) return Integer is
|
||||
(if b=0 then u else inverse (b, a mod b, v, u-(v*a)/b));
|
||||
begin
|
||||
return inverse (a, n, 1, 0);
|
||||
end inv_mod;
|
||||
begin
|
||||
-- This will output -48 (which is correct)
|
||||
Put_Line (inv_mod (42,2017)'img);
|
||||
-- The further line will raise an exception since the GCD will not be 1
|
||||
Put_Line (inv_mod (42,77)'img);
|
||||
exception when others => Put_Line ("The inverse doesn't exist.");
|
||||
end modular_inverse;
|
||||
23
Task/Modular-inverse/PowerShell/modular-inverse.ps1
Normal file
23
Task/Modular-inverse/PowerShell/modular-inverse.ps1
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function invmod($a,$n){
|
||||
if ([int]$n -lt 0) {$n = -$n}
|
||||
if ([int]$a -lt 0) {$a = $n - ((-$a) % $n)}
|
||||
|
||||
$t = 0
|
||||
$nt = 1
|
||||
$r = $n
|
||||
$nr = $a % $n
|
||||
while ($nr -ne 0) {
|
||||
$q = [Math]::truncate($r/$nr)
|
||||
$tmp = $nt
|
||||
$nt = $t - $q*$nt
|
||||
$t = $tmp
|
||||
$tmp = $nr
|
||||
$nr = $r - $q*$nr
|
||||
$r = $tmp
|
||||
}
|
||||
if ($r -gt 1) {return -1}
|
||||
if ($t -lt 0) {$t += $n}
|
||||
return $t
|
||||
}
|
||||
|
||||
invmod 42 2017
|
||||
3
Task/Modular-inverse/Uiua/modular-inverse.uiua
Normal file
3
Task/Modular-inverse/Uiua/modular-inverse.uiua
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Definition from https://github.com/Omnikar/uiua-math
|
||||
ModInv ← ˜⨂1˜◿×◡⋅⇡
|
||||
ModInv 42 2017
|
||||
Loading…
Add table
Add a link
Reference in a new issue