diviser_pour_regner.ml 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. let puissance x n = let p = ref 1 in
  2. for k = 1 to n do
  3. p := !p * x
  4. done;
  5. !p;;
  6. let rec puissance_rec x n = match n with
  7. | 0 -> 1
  8. | _ -> x * puissance_rec x (n-1);;
  9. let rec exponentiation_rapide x n = match n with
  10. | 0 -> 1
  11. | _ -> let y = exponentiation_rapide x (n/2) in
  12. if n mod 2 = 0 then y * y else x * y * y;;
  13. let exponentiation_rapide_terminale x n =
  14. let rec exponentiation_rapide_aux x n acc = match n with
  15. | 0 -> acc
  16. | _ -> let y = if n mod 2 = 0 then acc else acc * x in
  17. exponentiation_rapide_aux (x * x) (n/2) y
  18. in exponentiation_rapide_aux x n 1;;
  19. let exponentiation_rapide_iter x n =
  20. let p = ref 1 and x = ref x and n = ref n in
  21. while !n > 0 do
  22. if !n mod 2 = 0 then
  23. x := !x * !x
  24. else
  25. p := !p * !x;
  26. n := !n / 2
  27. done;
  28. !p;;
  29. let fibo_iter n =
  30. let u = ref 0 and v = ref 1 and aux = ref 0 in
  31. for k = 1 to !n do
  32. aux := !v;
  33. v := !u + !v;
  34. u := !aux
  35. done;
  36. !u;;
  37. let rec fibo_rec n = match n with
  38. | 0 -> 0
  39. | 1 -> 1
  40. | _ -> fibo_rec (n-1) + fibo_rec (n-2);;
  41. let rec fibo_aux n table =
  42. if table.(n) = -1 then
  43. table.(n) <- fibo_aux (n-1) table + fibo_aux (n-2) table;
  44. table.(n);;
  45. let rec fibo_memo n =
  46. let table = Array.make (n+1) (-1) in
  47. table.(0) <- 0;
  48. table.(1) <- 1;
  49. fibo_aux n table;;
  50. let recherche s x = let n = Array.length s and i = ref 0 in
  51. while !i < n && s.(!i) <> x do
  52. i := !i + 1
  53. done;
  54. !i <> n;;
  55. let rec recherche_rec_aux s x i = match i with
  56. | -1 -> false
  57. | _ -> if s.(i) = x then true else recherche_rec_aux s x (i-1);;
  58. let recherche_rec s x = recherche_rec_aux s x (Array.length s - 1);;
  59. let recherche_dpr_iter s x =
  60. let g = ref 0 and d = ref (Array.length s - 1) in
  61. while !g < !d do
  62. let m = (!g + !d) / 2 in
  63. if (x <= s.(m)) then d := m else g := m + 1
  64. done;
  65. s.(!g) = x;;
  66. let rec recherche_dpr_rec s x =
  67. let rec recherche_dpr_aux s x g d = match g with
  68. | _ when g > d -> false
  69. | _ -> let m = (g + d) / 2 in
  70. if x = s.(m) then true
  71. else if x < s.(m) then recherche_dpr_aux s x g (m-1)
  72. else recherche_dpr_aux s x (m+1) d
  73. in recherche_dpr_aux s x 0 (Array.length s - 1);;
  74. (*Addition de 2 polynomes*)
  75. let rec addition a b = let n = Array.length a in
  76. let c = Array.make n 0 in
  77. for i = 0 to n-1 do
  78. c.(i) <- a.(i) + b.(i)
  79. done;
  80. c;;
  81. let multiplication a b = let n = Array.length a in
  82. let c = Array.make (2*n-1) 0 in
  83. for i = 0 to n-1 do
  84. for j = 0 to n-1 do
  85. c.(i+j) <- c.(i+j) + a.(i) * b.(j)
  86. done
  87. done;
  88. c;;