Advertisement
csmine

DM1 Option info - programme "g n m"

Mar 12th, 2019
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.77 KB | None | 0 0
  1.  
  2. let g3 n =
  3. if n=0 then 0 else
  4. if n<=2 then 1 else
  5. let t = ref [|0;1;1|] in
  6. for i=3 to n do
  7. t := [|!t.(1);!t.(2);!t.(0) + !t.(2)|];
  8. done;
  9. !t.(2);;
  10. g3 22;;
  11. g 20 3;;
  12.  
  13.  
  14. let g n m =
  15. if n=0 then 0 else if n<=(m-1) then 1 else
  16. let t = ref (Array.make m 1) in
  17. !t.(0) <- 0;
  18. for i=m to n do
  19.     let temp = !t.(0) + !t.(m-1) in
  20.     for j=0 to (m-2) do
  21.     !t.(j) <- !t.(j+1);
  22.     done;
  23.     !t.(m-1) <- temp;
  24. done;
  25. !t.(m-1);;
  26.  
  27. g 22 4;;
  28. 43 mod 42;;
  29.  
  30. (* Exercice III. 3) et 4) *)
  31. let g_rapide n m=
  32. if n=0 then 0 else if n<=(m-1) then 1 else
  33. let t = ref (Array.make m 1) in
  34. !t.(0) <- 0;
  35. for i=m to n do
  36.     let (a,b) = ( (i-m) mod m , (i-1) mod m ) in
  37.     let temp = !t.( a ) + !t.( b ) in
  38.     !t.( a ) <- temp;
  39.     done;
  40. !t.( (n-m) mod m);;
  41.  
  42. g 200 3;;
  43. g_rapide (2*(499)-1+1789) 499;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement