0% found this document useful (0 votes)
24 views4 pages

Exercices TP1.ml

Algro3

Uploaded by

Kimngan Ho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Exercices TP1.ml

Algro3

Uploaded by

Kimngan Ho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#use "AP2inter.

ml";;
#use "AP2util.ml";;

(*Exercice 1*)

(*partie 1*)
let rec fiboV1 (n : int) : int =
if n < 0
then failwith ("Error fiboV1 : not inside the fonction")
else
if n <= 1
then 1
else fiboV1 (n-1) + fiboV1 (n-2)
;;
fiboV1(4);;

(*tracer la courbe*)
(*open_graph(1000,1000);;

draw_rect (100,100, 550, 300);;


draw_circle (250,250, 150);;

clear_graph();;*)

(*
let f (i : int) : int = i;;
let (i,t) : float array * float array = mycomplexity (40, fiboV1, f, f,1);;
open_graph (800,800);;
mydraw_complexity (i, t, {i_minx = 100 ; i_miny = 100 ; i_maxx = 700 ; i_maxy =
700});;
*)

(*partie 2*)

(* f_n2 = f(n-1)
f_n3 = f(n-2)
*)
let rec fiboV2_aux (n, n1, f_n2, f_n3 : int * int * int * int) : int =
if n1 = n+1
then f_n2
else fiboV2_aux(n, n1 + 1, f_n2+f_n3, f_n2)
;;

let rec fiboV2 (n : int) : int =


if n < 0
then failwith ("Error fiboV2 : not inside the fonction")
else
if n <= 1
then 1
else
fiboV2_aux(n, 2, 1, 1)
;;
fiboV2(4);;

(*tracer la courbe*)
(*
let f_2 (i_2 : int) : int = i_2;;
let (i_2,t_2) : float array * float array = mycomplexity (1000, fiboV2, f_2,
f_2,1);;
open_graph (800,800);;
mydraw_complexity (i_2, t_2, {i_minx = 100 ; i_miny = 100 ; i_maxx = 700 ; i_maxy =
700});;
*)

(*partie 3*)
let fiboV3 (n: int) : int =
let n1 : int ref = ref 1 in
let n2 : int ref = ref 1 in
let m : int ref = ref 0 in
if n < 0
then failwith ("Error fiboV3 : not inside the fonction")
else
if n <= 1
then 1
else
(
for i = 1 to n-1
do
m := !n1 ;
n1 := !n1 + !n2 ;
n2 := !m
done ;
!n1
)
;;
fiboV3(5);;

(*Exercice 2*)
(* Question 1 *)
(* calcul_fac calcule la factorielle des chiffres *)
let calcul_fac (n : int) : int =
let m : int ref = ref 1 in
let c : int ref = ref 0 in

(
while (!c < n)
do
c := !c + 1;
m := !m * !c;
done;
!m
)
;;

(* formule du coefficient binomial *)


let binoV1_aux (n, p : int * int) : int =
calcul_fac(n)/(calcul_fac(p)* (calcul_fac(n-p)))
;;

let binoV1 (n, p : int * int) : int =


if p < 0
then failwith ("Error binoV1 : negative number")
else
if n < p
then failwith ("Error binoV1 : can't be possible because must be n > p")
else binoV1_aux (n,p)
;;
binoV1(5,4);;

(* Question 3 - PAS FINI *)


let rec binoV3_aux (n, p : int * int) : int =
let m : int ref = ref 1 in
let c : int ref = ref 0 in

if !c < n
then

let binoV3 (n, p : int * int) : int =


if p < 0
then failwith ("Error binoV3 : negative number")
else
if n < p
then failwith ("Error binoV3 : can't be possible because must be n > p")
else binoV3_aux(n,p)
;;

(* Exercice 3 *)
let rec search_val (myarr, my_val, imin, imax : int array * int * int * int) : bool
* int =
if imax < imin
then failwith ("Error : valeur pas dans tableau")
else
let imed : int = (imax + imin)/2 in
if myarr.(imed) = my_val
then (true, imed)
else
if my_val > myarr.(imed)
then search_val (myarr, my_val, imed+1, imax)
else search_val (myarr, my_val, imin, imed-1)
;;

(* Exercice 4 *)
(* Question 1 *)
let exchange (arr, i, j : 'a array * int *int) : unit =
let other : int = arr.(i) in
(
arr.(i) <- j ;
arr.(j) <- other ;
)
;;

(* Question 3 *)
let max_at_end (arr, first, last : 'a array * int * int) : bool =
let changement : bool ref = ref false in
for i = first to last-1
do
(
if arr.(i) > arr.(i + 1)
then
(
exchange (arr, i,i+1);
changement := true
)
)
done;
!changement
;;
max_at_end ([|1;3;2;6;5|],0,4);;

(* Question 5 *)
let rec bubble_sort (arr, arr_len : 'a array * int) : 'a array =
if arr_len = 0
then
arr
else
let m : bool = max_at_end(arr, 0, arr_len) in
if m = true
then bubble_sort (arr,arr_len-1)
else arr
;;
bubble_sort([|1;3;2;6;5|],4);;

You might also like