[TP1 + cours] Listes / Tableaux
This commit is contained in:
parent
3e47e865b4
commit
d74937dce1
@ -36,7 +36,7 @@ let rec concatener listes = match longueur listes with
|
|||||||
| 1 -> List.hd listes
|
| 1 -> List.hd listes
|
||||||
| 2 -> (List.hd listes)@(List.hd (List.tl listes))
|
| 2 -> (List.hd listes)@(List.hd (List.tl listes))
|
||||||
| _ -> concatener ([(List.hd listes)@(List.hd (List.tl listes))]@(List.tl (List.tl listes)));;
|
| _ -> concatener ([(List.hd listes)@(List.hd (List.tl listes))]@(List.tl (List.tl listes)));;
|
||||||
concatener [[1;2;3];[2;1;3];[4;5;1;3];[1]];;
|
concatener [[1;2;3];[2;1;3];[];[4;5;1;3];[1]];;
|
||||||
concatener [];;
|
concatener [];;
|
||||||
List.concat [];;
|
List.concat [];;
|
||||||
|
|
||||||
@ -44,18 +44,22 @@ List.concat [];;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
let rec recherche_dichomotique a v =
|
let rec recherche_dicho d f a v =
|
||||||
if Array.length v = 0 then false
|
let c = (f - d)/2 in
|
||||||
else if a = v.((Array.length v)/2) then true
|
if c = 0 then false
|
||||||
else if a < v.((Array.length v)/2)
|
else if a = v.(c) then true
|
||||||
then recherche_dichomotique a (Array.sub v 0 ((Array.length v)/2))
|
else if a < v.(c)
|
||||||
else recherche_dichomotique a (Array.sub v ((Array.length v)/2+1) ((Array.length v)/2));;
|
then recherche_dicho d c a v
|
||||||
|
else recherche_dicho c f a v;;
|
||||||
|
|
||||||
|
let recherche_dichotomique a v =
|
||||||
|
recherche_dicho 0 (Array.length v) a v;;
|
||||||
|
|
||||||
|
recherche_dichotomique (-5) [|1;2;3;4;5;6;7;8;9;10;11|];;
|
||||||
|
|
||||||
|
|
||||||
(*Exercice 5*)
|
(*Exercice 5*)
|
||||||
let est_phermier n = (*on prend un n >=2*)
|
let est_premier n = (*on prend un n >=2*)
|
||||||
let t = ref true in
|
let t = ref true in
|
||||||
if n = 2 then ()
|
if n = 2 then ()
|
||||||
else for i = 2 to n/2 do
|
else for i = 2 to n/2 do
|
||||||
@ -63,22 +67,14 @@ let est_phermier n = (*on prend un n >=2*)
|
|||||||
done ;
|
done ;
|
||||||
!t;;
|
!t;;
|
||||||
|
|
||||||
est_phermier 17;;
|
est_premier 17;;
|
||||||
est_phermier 14;;
|
est_premier 14;;
|
||||||
|
|
||||||
|
|
||||||
let est_vrm_phermier n = (* on prend aussi un n >=2*)
|
(*let est_vrm_premier n = (* on prend aussi un n >=2*)
|
||||||
let t = ref true and i = ref 2 in
|
let t = ref true and i = ref 2 in
|
||||||
while !i <=(n/2) && !t do
|
while !i <=(n/2) && !t do
|
||||||
if (n mod !i) = 0 then t := false;
|
if (n mod !i) = 0 then t := false;
|
||||||
i := !i + 1
|
i := !i + 1
|
||||||
done;
|
done;
|
||||||
!t;;
|
!t;;*)
|
||||||
|
|
||||||
|
|
||||||
est_vrm_phermier 17;;
|
|
||||||
est_vrm_phermier 17;;
|
|
||||||
est_vrm_phermier 14;;
|
|
||||||
est_vrm_phermier 14;;
|
|
||||||
est_vrm_phermier 17;;
|
|
||||||
est_vrm_phermier 13;;
|
|
16
spe/cours/cous 2.ml
Normal file
16
spe/cours/cous 2.ml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
let catalan n =
|
||||||
|
let t = Array.make (n+1) 0 in
|
||||||
|
t.(0) <- 1;
|
||||||
|
for i=1 to n do
|
||||||
|
for k=0 to i-1 do
|
||||||
|
t.(i) <- (t.(k)*t.(i-1-k)) + t.(i)
|
||||||
|
done
|
||||||
|
done;
|
||||||
|
t.(n);;
|
||||||
|
|
||||||
|
catalan 5;;
|
||||||
|
|
||||||
|
let rec est_triee l = match l with
|
||||||
|
| [] -> true
|
||||||
|
| [a] -> true
|
||||||
|
| a::(b::t) -> a<=b && est_triee b::t;;
|
Loading…
Reference in New Issue
Block a user