Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue