Explorar o código

Avant-dernière révision

Ghastrod hai 1 ano
pai
achega
00a15938ca

+ 92 - 0
MP/concours/Deuxieme annee/abr.ml

@@ -0,0 +1,92 @@
+type 'a avl = Vide | Noeud of 'a * int * 'a avl * 'a avl;;
+
+type 'a arbre = Vide | Noeud of 'a * 'a arbre * 'a arbre;;
+
+let rec max_arbre a = match a with
+| Vide -> 0
+| Noeud(r,Vide,Vide) -> r
+| Noeud(r,g,d) -> max (max (max_arbre g) (max_arbre d)) r;;
+
+let rec min_arbre a = match a with
+| Vide -> 0
+| Noeud(r,Vide,Vide) -> r
+| Noeud(r,g,d) -> min (min (min_arbre g) (min_arbre d)) r;;
+
+let rec test_abr a = match a with
+| Vide -> true
+| Noeud(r,Vide,Vide) -> true
+| Noeud(r,Vide,d) -> (r <= min_arbre d) && (test_abr d)
+| Noeud(r,g,Vide) -> (r >= max_arbre g) && (test_abr g)
+| Noeud(r,g,d) -> (r >= max_arbre g) && (r <= min_arbre d) && (test_abr g) && (test_abr d);;
+
+(*Parcours en profondeur en mode infixe d'un ABR*)
+let rec parcours_profondeur_infixe a = match a with
+| Vide -> []
+| Noeud(r,g,d) -> (parcours_profondeur_infixe g) @ [r] @ (parcours_profondeur_infixe d);;
+
+let rec recherche_abr a k = match a with
+| Vide -> false
+| Noeud(r,g,d) -> if r = k then true else if r < k then recherche_abr d k else recherche_abr g k;;
+
+let rec etiquette_max a = match a with
+| Vide -> failwith "Erreur"
+| Noeud(r,Vide,Vide) -> r
+| Noeud(r,g,d) -> etiquette_max d;;
+
+let rec etiquette_min a = match a with
+| Vide -> failwith "Erreur"
+| Noeud(r,Vide,Vide) -> r
+| Noeud(r,g,d) -> etiquette_min g;;
+
+let rec etiquette_predecesseur a k = match a with
+| Vide -> failwith "Erreur"
+| Noeud(r,g,d) -> if r = k then 
+  etiquette_max g 
+else if r < k then 
+  etiquette_predecesseur d k 
+else etiquette_predecesseur g k;;
+
+let rec etiquette_successeur a k = match a with
+| Vide -> failwith "Erreur"
+| Noeud(r,g,d) -> if r = k then 
+  etiquette_min d 
+else if r < k then 
+  etiquette_successeur d k 
+else etiquette_successeur g k;;
+
+let rec insertion_feuille a k = match a with
+| Vide -> Noeud(k,Vide,Vide)
+| Noeud(r,g,d) -> if r = k then 
+  a 
+else if r < k then 
+  Noeud(r,g,(insertion_feuille d k)) 
+else Noeud(r,(insertion_feuille g k),d);;
+
+let rec partition a k = match a with
+| Vide -> (Vide,Vide)
+| Noeud(r,g,d) -> if r < k then 
+  let (g1,d1) = partition d k in 
+  (Noeud(r,g,g1),d1) 
+else let (g1,d1) = partition g k in 
+( g1,Noeud(r,d1,d));;
+
+(*Inserer la nouvelle racine k grâce à la fonction partition*)
+let rec insertion_racine a k = match a with
+| Vide -> Noeud(k,Vide,Vide)
+| Noeud(r,g,d) -> let (g1,d1) = partition a k in
+Noeud(k,g1,d1);;
+
+let rec supprime_etiquette_min a = match a with
+| Vide -> failwith "Erreur"
+| Noeud(r,Vide,Vide) -> Vide
+| Noeud(r,g,d) -> Noeud(r,supprime_etiquette_min g,d);;
+
+(*Supprime l'étiquette k dans a utilisant la fonction supprime_etiquette_min*)
+let rec supprime_etiquette a k = match a with
+| Vide -> Vide
+| Noeud(r,g,d) -> if r = k then 
+  if g = Vide then d else if d = Vide then g else Noeud(etiquette_min d,g,supprime_etiquette_min d) 
+else if r < k then 
+  Noeud(r,g,(supprime_etiquette d k)) 
+else Noeud(r,(supprime_etiquette g k),d);;
+

+ 4 - 0
MP/concours/Deuxieme annee/backtracking.ml

@@ -0,0 +1,4 @@
+type nuage = float*float array;;
+let creer_nuage_points (b:int) : float*float list=
+;;
+

+ 33 - 0
MP/concours/Deuxieme annee/graphes.ml

@@ -0,0 +1,33 @@
+type graphe = int list array;;
+let rec parcours_dfs_aux (g:graphe) (listevue:int list) (listeavoir:int list):unit = match listeavoir with
+| [] -> ()
+| t::q -> if (List.mem t listevue) then parcours_dfs_aux g listevue q
+  else
+    begin
+      parcours_dfs_aux g (t::listevue) (g.(t)@q)
+    end;;
+
+let parcours_profondeur (g:graphe) (sommet:int):unit = parcours_dfs_aux g [] [sommet];;
+
+let rec bfs_aux (g:graphe) (listevue:int list) (listeavoir:int list):int list = match listeavoir with
+| [] -> listevue
+| t::q -> if (List.mem t listevue) then bfs_aux g listevue q
+  else
+    begin
+      bfs_aux g (t::listevue) (q@g.(t))
+    end;;
+
+let connexe g = let parcours = bfs_aux g [] [0] in
+  if (List.length parcours = List.length g) then true
+  else false;;
+
+let composantes_connexes g = let n = Array.length g in
+  let cc = Array.make n 0 in
+    for s = 0 to n-1 do
+      if (cc.(s) = 0) then
+        begin
+          let parcours = bfs_aux g [] [s] in
+          List.iter (fun x -> cc.(x) <- s+1) parcours
+        end
+      done;
+    cc;;

+ 37 - 0
MP/concours/Premiere annee/analyse.ml

@@ -0,0 +1,37 @@
+let factorielle1 n = 
+  let p = ref 1 in
+  for i = 1 to n do
+    p := !p * i
+  done;
+  !p
+;;
+
+factorielle1 5;;
+
+
+let maximum t = let max = ref 0 in
+  for i = 0 to Array.length t - 1 do
+    if t.(i) > !max then max := t.(i)
+  done;
+  !max
+;;
+
+let recherche t x = let n = Array.length t and i = ref 0 in
+  while !i < n && t.(!i) <> x do
+    i := !i + 1
+  done;
+  !i < n
+;;
+
+let rec factorielle2 n = if n = 0 then 1 else n * factorielle2 (n - 1);;
+
+let rec pgcd a b = if b = 0 then a else pgcd b (a mod b);;
+
+let min3 a b c = if a <= b && a <= c then a else if b <= a && b <= c then b else c;;
+
+let puissance x n = let p = ref 1 in
+  for k = 1 to n do
+    p := !p * x
+  done;
+  !p
+;;  

+ 0 - 0
MP/concours/arbres.ml → MP/concours/Premiere annee/arbres.ml


+ 104 - 0
MP/concours/Premiere annee/diviser_pour_regner.ml

@@ -0,0 +1,104 @@
+let puissance x n = let p = ref 1 in
+  for k = 1 to n do
+    p := !p * x
+  done;
+!p;;
+
+let rec puissance_rec x n = match n with
+| 0 -> 1
+| _ -> x * puissance_rec x (n-1);;
+
+let rec exponentiation_rapide x n = match n with
+| 0 -> 1
+| _ -> let y = exponentiation_rapide x (n/2) in
+  if n mod 2 = 0 then y * y else x * y * y;;
+
+let exponentiation_rapide_terminale x n =
+  let rec exponentiation_rapide_aux x n acc = match n with
+  | 0 -> acc
+  | _ -> let y = if n mod 2 = 0 then acc else acc * x in
+    exponentiation_rapide_aux (x * x) (n/2) y
+  in exponentiation_rapide_aux x n 1;;
+
+let exponentiation_rapide_iter x n =
+  let p = ref 1 and x = ref x and n = ref n in
+  while !n > 0 do
+    if !n mod 2 = 0 then
+      x := !x * !x
+    else
+      p := !p * !x;
+    n := !n / 2
+  done;
+!p;;
+
+let fibo_iter n =
+  let u = ref 0 and v = ref 1 and aux = ref 0 in
+  for k = 1 to !n do
+    aux := !v;
+    v := !u + !v;
+    u := !aux
+  done;
+!u;;
+
+let rec fibo_rec n = match n with
+| 0 -> 0
+| 1 -> 1
+| _ -> fibo_rec (n-1) + fibo_rec (n-2);;
+
+let rec fibo_aux n table =
+  if table.(n) = -1 then
+    table.(n) <- fibo_aux (n-1) table + fibo_aux (n-2) table;
+  table.(n);;
+
+let rec fibo_memo n =
+  let table = Array.make (n+1) (-1) in
+  table.(0) <- 0;
+  table.(1) <- 1;
+  fibo_aux n table;;
+
+let recherche s x = let n = Array.length s and i = ref 0 in
+  while !i < n && s.(!i) <> x do
+    i := !i + 1
+  done;
+  !i <> n;;
+
+let rec recherche_rec_aux s x i = match i with
+| -1 -> false
+| _ -> if s.(i) = x then true else recherche_rec_aux s x (i-1);;
+
+let recherche_rec s x = recherche_rec_aux s x (Array.length s - 1);;
+
+let recherche_dpr_iter s x =
+  let g = ref 0 and d = ref (Array.length s - 1) in
+    while !g < !d do
+      let m = (!g + !d) / 2 in
+      if (x <= s.(m)) then d := m else g := m + 1
+    done;
+  s.(!g) = x;;
+
+let rec recherche_dpr_rec s x =
+  let rec recherche_dpr_aux s x g d = match g with
+  | _ when g > d -> false
+  | _ -> let m = (g + d) / 2 in
+    if x = s.(m) then true
+    else if x < s.(m) then recherche_dpr_aux s x g (m-1)
+    else recherche_dpr_aux s x (m+1) d
+  in recherche_dpr_aux s x 0 (Array.length s - 1);;
+
+(*Addition de 2 polynomes*)
+
+let rec addition a b = let n = Array.length a in
+ let c = Array.make n 0 in
+  for i = 0 to n-1 do
+    c.(i) <- a.(i) + b.(i)
+  done;
+c;;
+
+let multiplication a b = let n = Array.length a in 
+  let c = Array.make (2*n-1) 0 in
+  for i = 0 to n-1 do
+    for j = 0 to n-1 do
+      c.(i+j) <- c.(i+j) + a.(i) * b.(j)
+    done
+  done;
+c;;

+ 75 - 0
MP/concours/Premiere annee/recursivite.ml

@@ -0,0 +1,75 @@
+let suite_iteratif u0 n f = let u = ref u0 in
+  for i = 1 to n do
+    u := f !u
+  done;
+  !u;;
+
+let rec suite_recursif u0 n f = if n = 0 then u0 else f (suite_recursif u0 (n-1) f);;
+
+let factorielle_iter n = let f = ref 1 in
+  for i = 1 to n do
+    f := !f * i
+  done;
+  !f;;
+
+let rec factorielle_rec n = if n = 0 then 1 else n * factorielle_rec (n-1);;
+
+let puissance_iter x n = let p = ref 1 in
+  for i = 1 to n do
+    p := !p * x
+  done;
+  !p;;
+
+let rec puissance_rec x n = if n = 0 then 1 else x * puissance_rec x (n-1);;
+
+let rec carre n = match n with
+| 0 -> 0
+| _ -> -1 + carre (n-1) + 2 * n;;
+
+let rec somme p q = match p with
+| 0 -> q
+| _ -> somme (p-1) (q+1);;
+
+let rec produit p q = match p with
+| 0 -> 0
+| _ -> somme q (produit (p-1) q);;
+
+let rec min1 t = let n = Array.length t in
+  match n with
+  | 1 -> t.(0)
+  | _ -> let m = min1 (Array.sub t 1 (n-1)) in
+    if t.(0) < m then t.(0) else m;;
+
+(*On veut maintenant l'indice du minimum*)
+let rec min2 t = let n = Array.length t in
+  match n with
+  | 1 -> 0
+  | _ -> let m = min2 (Array.sub t 1 (n-1)) in
+    if t.(0) < t.(m+1) then 0 else m+1;;
+
+let rec produit_egyptien p q = match p with
+| 0 -> 0
+| _ -> if p mod 2 = 0 then produit_egyptien (p/2) (2*q)
+  else q + produit_egyptien (p-1) q;;
+
+let rec division_euclidienne p q = if p < q then (0,p) else
+  let (a,b) = division_euclidienne (p-q) q in
+  (a+1,b);;
+
+let rec pgcd a b = if b = 0 then a else pgcd b (a mod b);;
+
+let rec fibo1 n = match n with
+| 0 -> 0
+| 1 -> 1
+| _ -> fibo1 (n-1) + fibo1 (n-2);;
+
+let rec coefficient_binomial n p = if p = 0 || p = n then 1
+  else coefficient_binomial (n-1) (p-1) + coefficient_binomial (n-1) p;;
+
+(*Hanoi*)
+
+let rec deplace n i j k = 
+  if n = 1 then print_string ("Deplacer le disque de " ^ string_of_int i ^ " vers " ^ string_of_int j ^ "\n")
+  else (deplace (n-1) i k j; deplace 1 i j k; deplace (n-1) k j i);;
+
+let hanoi n = deplace n 1 2 3;;

+ 22 - 0
MP/concours/Premiere annee/structures.ml

@@ -0,0 +1,22 @@
+type coordonnee = float * float;;
+
+type logarithme = Ln of float;;
+
+type compte_en_banque = {nom : string; mutable solde : float};;
+
+let ex1 = {nom = "toto"; solde = 100.0};;
+
+(*Ajouter 20 sur le solde de toto*)
+let ajouter_solde c s = c.solde <- c.solde +. s;;
+
+ajouter_solde ex1 20.0;;
+
+(*Retirer 20 sur le solde de toto*)
+
+type 'a pile = PileVide | Ajout of 'a * 'a pile;;
+
+let pile1 = Ajout(1, Ajout(2, Ajout(3, PileVide)));;
+
+let rec somme_pile p = match p with
+  | PileVide -> 0
+  | Ajout(x, p') -> x + somme_pile p';;

+ 0 - 0
MP/concours/tri.ml → MP/concours/Premiere annee/tri.ml