Advertisement
Guest User

basic_arithmetics.ml

a guest
Dec 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.86 KB | None | 0 0
  1. (** Basic arithmetics with built-in integers *)
  2.  
  3. open Builtin
  4.  
  5. (* Greater common divisor and smaller common multiple
  6.    implemetations.
  7. *)
  8.  
  9. (** Greater common (positive) divisor of two non-zero integers.
  10.     @param a non-zero integers
  11.     @param b non-zero integer
  12. *)
  13. let rec gcd a b = if b = 0 then a else gcd b (modulo a b);;
  14.  
  15. (* Extended Euclidean algorithm. Computing Bezout Coefficients. *)
  16.  
  17. (** Extended euclidean division of two integers NOT OCAML DEFAULT.
  18.     Given non-zero entries a b computes triple (u, v, d) such that
  19.     a*u + b*v = d and d is gcd of a and b.
  20.     @param a non-zero integer
  21.     @param b non-zero integer.
  22. *)
  23. let rec bezout a b = if b = 0 then 1, 0, a else let (x, y, d) = bezout b (modulo a b) in (y, x - a/b*y, d);;
  24.  
  25. let modinv a m = let (u, _, d) = bezout a m in if d <> 1 then invalid_arg "a and m not coprime" else modulo u m;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement