84 lines
1.3 KiB
OCaml
84 lines
1.3 KiB
OCaml
(* Exercice 1 *)
|
|
|
|
let dichotomie x t = ();;
|
|
|
|
(* Exercice 2 *)
|
|
|
|
let rec f x n = match n with
|
|
| 0 -> 1
|
|
| _ -> x * (f x (n - 1));;
|
|
|
|
f 10 2;;
|
|
|
|
let puissance x n =
|
|
let res = ref 1 in
|
|
for i=0 to n-1 do
|
|
res := !res * x
|
|
done;
|
|
!res;;
|
|
|
|
puissance 2 2;;
|
|
|
|
(* Exercice 3 *)
|
|
|
|
(* 5 *)
|
|
let rec pgcd a b = match min (abs a) (abs b) with
|
|
| 0 -> max (abs a) (abs b)
|
|
| 1 -> 1
|
|
| _ -> pgcd ((max (abs a) (abs b)) mod (min (abs a) (abs b))) (min (abs a) (abs b));;
|
|
|
|
let rec pgcd a b = match b with
|
|
| 0 -> a
|
|
| -> pgcd b (a mod b);;
|
|
|
|
|
|
(* 7 *)
|
|
let bezout a b =
|
|
let rec aux u0 v0 u1 v1 r0 r1 = match r0 mod r1 with
|
|
| 0 -> r1,u1,v1
|
|
| _ -> aux u1 v1 (u0 - ( (r0 / r1) * u1 ) ) (v0 - ( (r0 / r1) * v1 )) r1 (r0 mod r1) in
|
|
aux 1 0 0 1 a b;;
|
|
|
|
bezout 24 65;;
|
|
bezout 6 6;;
|
|
|
|
pgcd (-1024) 4096;;
|
|
|
|
max (abs 1) (abs 2) ;;
|
|
abs;;
|
|
|
|
(* Exercice 4 *)
|
|
|
|
let next n = match n mod 2 with
|
|
| 0 -> n/2
|
|
| _ -> 3 * n + 1;;
|
|
let
|
|
next 1;;
|
|
|
|
let rec syracuse n = match n with
|
|
| 1 -> [1]
|
|
| _ -> n::syracuse (next n);;
|
|
syracuse 5;;
|
|
|
|
let rec altitude_maximale n = match n with
|
|
| 1 -> 1
|
|
| _ -> max n (altitude_maximale (next n));;
|
|
|
|
syracuse 9;;
|
|
altitude_maximale 9;;
|
|
|
|
let dav n =
|
|
let vmax = ref 0 in
|
|
let v = ref 0 in
|
|
let i = ref n in
|
|
while !i <> 1 do
|
|
i := next !i;
|
|
if !i > n then
|
|
v := !v + 1
|
|
else
|
|
v := 0;
|
|
if !v > !vmax then
|
|
vmax := !v
|
|
done;
|
|
!vmax;;
|
|
dav 9;; |