-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_arithmetics.ml
More file actions
52 lines (44 loc) · 1.22 KB
/
basic_arithmetics.ml
File metadata and controls
52 lines (44 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(** Basic arithmetics with built-in integers *)
open Builtin
(* Greater common divisor and smaller common multiple
implemetations.
*)
(** Greater common (positive) divisor of two non-zero integers.
@param a non-zero integers
@param b non-zero integer
*)
let rec gcd a b =
let x = sign a in
let y = sign b in
let rec pgcd a b n =
if a mod n = 0 && b mod n = 0 then
n * sign x * sign y
else
pgcd a b (n-1)
in
if a*x > b*x then
pgcd a b (b*x)
else
pgcd b a (a*x)
;;
(* Extended Euclidean algorithm. Computing Bezout Coefficients. *)
(** Extended euclidean division of two integers NOT OCAML DEFAULT.
Given non-zero entries a b computes triple (u, v, d) such that
a*u + b*v = d and d is gcd of a and b.
@param a non-zero integer
@param b non-zero integer.
*)
let rec bezout a b =
let r = a in
let u = 1 in
let v = 0 in
let r_prime = b in
let u_prime = 0 in
let v_prime = 1 in
let rec calcul r u v r_prime u_prime v_prime =
if r_prime = 0 then
(u,v,r)
else
calcul r_prime u_prime v_prime (r-((r/r_prime)*r_prime)) (u-((r/r_prime)*u_prime)) (v-((r/r_prime)*v_prime));
in
calcul r u v r_prime u_prime v_prime;;