Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Modular-inverse/Cowgol/modular-inverse.cowgol
Normal file
46
Task/Modular-inverse/Cowgol/modular-inverse.cowgol
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
sub mulinv(a: int32, b: int32): (t: int32) is
|
||||
if b<0 then b := -b; end if;
|
||||
if a<0 then a := b - (-a % b); end if;
|
||||
t := 0;
|
||||
var nt: int32 := 1;
|
||||
var r := b;
|
||||
var nr := a % b;
|
||||
while nr != 0 loop
|
||||
var q := r / nr;
|
||||
var tmp := nt; nt := t - q*nt; t := tmp;
|
||||
tmp := nr; nr := r - q*nr; r := tmp;
|
||||
end loop;
|
||||
if r>1 then t := -1;
|
||||
elseif t<0 then t := t + b;
|
||||
end if;
|
||||
end sub;
|
||||
|
||||
record Pair is
|
||||
a: int32;
|
||||
b: int32;
|
||||
end record;
|
||||
|
||||
var data: Pair[] := {
|
||||
{42, 2017},
|
||||
{40, 1},
|
||||
{52, -217},
|
||||
{-486, 217},
|
||||
{40, 2018}
|
||||
};
|
||||
|
||||
var i: @indexof data := 0;
|
||||
while i < @sizeof data loop
|
||||
print_i32(data[i].a as uint32);
|
||||
print(", ");
|
||||
print_i32(data[i].b as uint32);
|
||||
print(" -> ");
|
||||
var mi := mulinv(data[i].a, data[i].b);
|
||||
if mi<0
|
||||
then print("no inverse");
|
||||
else print_i32(mi as uint32);
|
||||
end if;
|
||||
print_nl();
|
||||
i := i + 1;
|
||||
end loop;
|
||||
Loading…
Add table
Add a link
Reference in a new issue