DiviserRegner.ml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. let fiboiter n = let x = ref 0 and y = ref 1 and aux = ref 0 in
  2. for i = 1 to n do
  3. aux := !y;
  4. y := !y + !x;
  5. x := !aux;
  6. done;
  7. !y
  8. ;;
  9. let rec fiborecu n = match n with
  10. | 0 -> 0
  11. | 1 -> 1
  12. | _ -> fiborecu (n-1) + fiborecu (n-2);;
  13. let rec aux n table =
  14. if table.(n) = -1 then
  15. table.(n) <- (aux (n-1) table) + (aux (n-2) table);
  16. table.(n);;
  17. let fibo2 n = let table = Array.make (n+1) (-1) in
  18. table.(0) <- 0;
  19. table.(1)<- 1;
  20. aux n table
  21. ;;
  22. let m = Array.make_matrix 3 3 3;;
  23. m.(1).(2) <- 5
  24. (* let produitmatriciel a b =
  25. if Array.length a <> Array.length b.(0) then
  26. print_endline ("error");
  27. let result = Array.make_matrix (Array.length a) (Array.length b.(0)) 0 in
  28. for i = 0 to (Array.length a)-1 do
  29. let aux = ref 0 in
  30. for j = 0 to Array.length a.(0)-1 do
  31. done;
  32. done;
  33. result
  34. ;; *)
  35. let produit a b =
  36. let p = Array.length a and ra = Array.length a.(0)
  37. and rb = Array.length b and q = Array.length b.(0) in
  38. if ra <> rb then
  39. failwith "pb de dimension"
  40. else
  41. begin
  42. let c = Array.make_matrix p q 0 in
  43. for i = 0 to p-1 do
  44. for j = 0 to q-1 do
  45. for k = 0 to ra-1 do
  46. c.(i).(j) <- c.(i).(j) + a.(i).(k) * b.(k).(j)
  47. done;
  48. done;
  49. done;
  50. c
  51. end
  52. ;;
  53. let identite n = let c = Array.make_matrix n n 0 in
  54. for i = 0 to (n-1) do
  55. c.(i).(i) <- 1;
  56. done;
  57. c;;
  58. let rec exp_rapide a n = match n with
  59. | 0 -> identite (Array.length a)
  60. | _ -> let aux = exp_rapide a (n/2) in
  61. if n mod 2 = 0 then produit aux aux
  62. else produit (produit aux aux) aux;;
  63. let fibo n =
  64. let a = Array.make_matrix 2 2 1 in
  65. a.(0).(0) <- 0;
  66. (exp_rapide a n).(0).(1);;
  67. let rechercheiter t x= let ln = Array.length t and bo = ref false in
  68. for i = 0 to ln-1 do
  69. if t.(i) = x then bo := true
  70. done;
  71. !bo;;
  72. let rechercheiterprof s x =
  73. let n = Array.length s and i = ref 0 in
  74. while (!i<n) && (x<>s.(!i)) do
  75. i:= !i+1
  76. done;
  77. !i<>n;;
  78. let rec rechercherecu_aux s x i= match i with
  79. | -1 -> false
  80. | _ -> (x = s.(i)) || rechercherecu_aux s x (i-1);;
  81. let rechrecuchap s x = rechercherecu_aux s x ((Array.length s)-1);;
  82. let rechdicoiter s x = let a = ref 0 and b = ref s.(Array.length s-1) in
  83. while !a <> !b do
  84. let c = (!a + !b)/2 in
  85. if s.(c)>= x then
  86. b := c
  87. else
  88. a := c+1
  89. done;
  90. (s.(!a) = x);;
  91. let rec recherchedicorecu s x g d =
  92. if g = d then x = s.(g)
  93. else
  94. begin
  95. let m = (g+d)/2 in
  96. if s.(m)>= x then
  97. recherchedicorecu s x m d
  98. else
  99. recherchedicorecu s x (m+1) d
  100. end
  101. ;;
  102. let recherchedicorecuchap s x = recherchedicorecu s x 0 (Array.length s-1);;
  103. let sommepoly a b = let c = Array.make (max (Array.length a) (Array.length b) ) 0 in
  104. for i = 0 to Array.length c-1 do
  105. c.(i) <- a.(i) + b.(i)
  106. done;
  107. c;;
  108. (* let produitpol a b =
  109. let n = Array.length a in
  110. let c = Array.make (2*n) 0 in
  111. for i = 0 to (n-1) do
  112. (for j = 0 to (n-1) do
  113. c.(i+j) <- c.(i+j) + a.(i) * b.(j)
  114. done : unit);
  115. done;
  116. c
  117. ;; *)
  118. let fusion c1 c2 c3 =
  119. let n = Array.length c1 in
  120. let c = Array.make (2*n) 0 in
  121. for k = 0 to (n/2-1) do
  122. c.(k) <- c1.(k)
  123. done;
  124. for i = (n/2) to (n-1) do
  125. c.(i) <- c1.(i) + c2.(i-n/2)
  126. done;
  127. for j = n to (3*n/2-1) do
  128. c.(j) <- c2.(j-n/2) + c3.(j-n)
  129. done;
  130. for l = (3*n/2) to (2*n-1) do
  131. c.(l) <- c3.(l-n)
  132. done;
  133. c;;
  134. let difference a b =
  135. let n = Array.length a in
  136. let c = Array.make n 0 in
  137. for k = 0 to (n-1) do
  138. c.(k) <- a.(k) - b.(k)
  139. done;
  140. c;;
  141. let rec karatsuba a b =
  142. if (Array.length a = 1) then
  143. [| a.(0)*b.(0); 0 |]
  144. else
  145. begin
  146. let n = Array.length a in
  147. let a1 = Array.sub a 0 (n/2) and
  148. a2 = Array.sub a (n/2) (n/2) and
  149. b1 = Array.sub b 0 (n/2) and
  150. b2 = Array.sub b (n/2) (n/2) in
  151. let c1 = karatsuba a1 b1 and
  152. c3 = karatsuba a2 b2 in
  153. let a12 = sommepoly a1 a2 and
  154. b12 = sommepoly b1 b2 in
  155. let c2 = difference (karatsuba a12 b12) (sommepoly c1 c3) in
  156. fusion c1 c2 c3
  157. end
  158. ;;
  159. karatsuba [|1;2;3|] [|4;5;6|];;