Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
6
Task/Modular-arithmetic/Jq/modular-arithmetic-1.jq
Normal file
6
Task/Modular-arithmetic/Jq/modular-arithmetic-1.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def assert($e; $msg): if $e then . else "assertion violation @ \($msg)" | error end;
|
||||
|
||||
def is_integer: type=="number" and floor == .;
|
||||
|
||||
# To take advantage of gojq's arbitrary-precision integer arithmetic:
|
||||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
|
||||
38
Task/Modular-arithmetic/Jq/modular-arithmetic-2.jq
Normal file
38
Task/Modular-arithmetic/Jq/modular-arithmetic-2.jq
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# "ModularArithmetic" objects are represented by JSON objects of the form: {value, mod}.
|
||||
# The function modint::assert/0 checks the input is of this form with integer values.
|
||||
|
||||
def is_modint: type=="object" and has("value") and has("mod");
|
||||
|
||||
def modint::assert:
|
||||
assert(type=="object"; "object expected")
|
||||
| assert(has("value"); "object should have a value")
|
||||
| assert(has("mod"); "object should have a mod")
|
||||
| assert(.value | is_integer; "value should be an integer")
|
||||
| assert(.mod | is_integer; "mod should be an integer");
|
||||
|
||||
def modint::make($value; $mod):
|
||||
assert($value|is_integer; "value should be an integer")
|
||||
| assert($mod|is_integer; "mod should be an integer")
|
||||
| { value: ($value % $mod), mod: $mod};
|
||||
|
||||
def modint::add($A; $B):
|
||||
if ($B|type) == "object"
|
||||
then assert($A.mod == $B.mod ; "modint::add")
|
||||
| modint::make( $A.value + $B.value; $A.mod )
|
||||
else modint::make( $A.value + $B; $A.mod )
|
||||
end;
|
||||
|
||||
def modint::mul($A; $B):
|
||||
if ($B|type) == "object"
|
||||
then assert($A.mod == $B.mod ; "mul")
|
||||
| modint::make( $A.value * $B.value; $A.mod )
|
||||
else modint::make( $A.value * $B; $A.mod )
|
||||
end;
|
||||
|
||||
def modint::pow($A; $pow):
|
||||
assert($pow | is_integer; "pow")
|
||||
| reduce range(0; $pow) as $i ( modint::make(1; $A.mod);
|
||||
modint::mul( .; $A) );
|
||||
|
||||
# pretty print
|
||||
def modint::pp: "«\(.value) % \(.mod)»";
|
||||
26
Task/Modular-arithmetic/Jq/modular-arithmetic-3.jq
Normal file
26
Task/Modular-arithmetic/Jq/modular-arithmetic-3.jq
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
def ring::add($A; $B):
|
||||
if $A|is_modint then modint::add($A; $B)
|
||||
elif $A|is_integer then $A + $B
|
||||
else "ring::add" | error
|
||||
end;
|
||||
|
||||
def ring::mul($A; $B):
|
||||
if $A|is_modint then modint::mul($A; $B)
|
||||
elif $A|is_integer then $A * $B
|
||||
else "ring::mul" | error
|
||||
end;
|
||||
|
||||
def ring::pow($A; $B):
|
||||
if $A|is_modint then modint::pow($A; $B)
|
||||
elif $A|is_integer then $A|power($B)
|
||||
else "ring::pow" | error
|
||||
end;
|
||||
|
||||
def ring::pp:
|
||||
if is_modint then modint::pp
|
||||
elif is_integer then .
|
||||
else "ring::pp" | error
|
||||
end;
|
||||
|
||||
def ring::f($x):
|
||||
ring::add( ring::add( ring::pow($x; 100); $x); 1);
|
||||
5
Task/Modular-arithmetic/Jq/modular-arithmetic-4.jq
Normal file
5
Task/Modular-arithmetic/Jq/modular-arithmetic-4.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def main:
|
||||
(ring::f(1) | "f(\(1)) => \(.)"),
|
||||
(modint::make(10;13)
|
||||
| ring::f(.) as $out
|
||||
| "f(\(ring::pp)) => \($out|ring::pp)");
|
||||
Loading…
Add table
Add a link
Reference in a new issue