Advertisement
Trietch

Untitled

Nov 11th, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.82 KB | None | 0 0
  1. let nom = "MARREC" and prenom = "Tristan";;
  2.  
  3.  
  4. let pow n exp =
  5.   let rec rec_pow n exp = match exp with
  6.     | 0 -> 1
  7.     | _ -> (rec_pow n (exp-1))*n
  8.   in if exp < 0 then failwith "l'exposant doit etre positif"
  9.   else (rec_pow n exp);;
  10.  
  11.  
  12. let pgcd m n =
  13.   let rec rpgcd m n =
  14.     if m = 0 && n <> 0 then n
  15.     else if n = 0 && m <> 0 then m
  16.     else if m >= n && n > 0 then (rpgcd n (m mod n))
  17.     else (rpgcd n m)
  18.   in if n = 0 && m = 0 then failwith "truc"
  19.   else (rpgcd m n);;
  20.  
  21.  
  22. let ppcm m n = (m*n)/(pgcd m n);;
  23.  
  24.  
  25. let replique list n =
  26.   let rec rreplique list list2 n = match n with
  27.     | 0 -> []
  28.     | 1 -> list
  29.     | _ -> (rreplique (list@list2) list2 (n-1))
  30.   in (rreplique list list n);;
  31.  
  32.  
  33. let int_add list1 list2 n =
  34.   let rec reverse = function
  35.     | [] -> []
  36.     | x::list1 -> (reverse list1)@[x] in
  37.   let rec add add_list1 add_list2 q = match (add_list1, add_list2, q) with
  38.     | ([], [], r) -> (0, [r])
  39.     | (a::[], b::[], r) -> ((a+b+r)/10,[(a+b+r) mod 10])
  40.     | (a1::a2, b1::b2, r) -> let (ret,list1) = (add a2 b2 ((a1+b1+r)/10))
  41.         in (ret, ((a1+b1+r) mod 10)::list1)
  42.     | _::_, [], _ | [], _::_, _ -> failwith "ne devrais jamais arriver"
  43.   in let (r,add_list1) = (add (reverse list1) (reverse list2) n)
  44.   in match (list1, list2, n) with
  45.   | [], [], n -> (n, []);
  46.   | _, _, _ -> (r, reverse add_list1);;
  47.  
  48.  
  49. let rec etend_prefixe n (list1, list2)= match (n, list1, list2) with
  50.   | 0, _, _ -> (list1, list2)
  51.   | n, _, [] -> (etend_prefixe (n-1) (list1@[0], [0]))
  52.   | n, _, deb::fin -> (etend_prefixe (n-1) (list1@[deb], fin@[deb]));;
  53.  
  54.  
  55. let etend_pc lp lc (list1,list2) =
  56.   let zero l = match l with
  57.     | [] -> [0]
  58.     | _ -> l in
  59.   let rec size l = match l with
  60.     | [] -> 0
  61.     | _::fin -> (size fin)+1 in
  62.   let (a,b) = etend_prefixe (lp-(size list1)) (list1,(zero list2)) in
  63.   let (c,_) = etend_prefixe (lc-(size (zero list2))) (b,b) in
  64.   (a,c);;
  65.  
  66.  
  67. let mkInt list =
  68.   let rec reverse = function
  69.     | [] -> []
  70.     | deb::fin -> (reverse fin)@[deb] in
  71.   let rec rec_mkint list n =
  72.     match (list,n) with
  73.     | [], _ -> 0
  74.     | (a::b), n -> (a*(pow 10 n))+(rec_mkint b (n+1))
  75.   in (rec_mkint (reverse list) 0);;
  76.  
  77.  
  78. let rec mkQuot a b =
  79.   let abs n = if n < 0 then n*(-1) else n in
  80.   let p = (pgcd (abs a) (abs b)) in
  81.   if b < 0 then (a/(p*(-1))), (b/(p*(-1)))
  82.   else (a/p),(b/p);;
  83.  
  84.  
  85. let addQuot a b = match (a,b) with
  86.   | (a,b),(c,d) -> (mkQuot (a*d+b*c) (b*d));;
  87.  
  88.  
  89. let rec couper list n = let rec first list n = match list with
  90.     | [] -> []
  91.     | (a::b) -> match a with
  92.       | (c,d) -> if c = n
  93.           then []
  94.           else [a]@(first b n) in
  95.   let rec second list n = match list with
  96.     | [] -> []
  97.     | (a::b) -> match a with
  98.       | (c,d) -> if c = n
  99.           then a::b
  100.           else (second b n) in
  101.   (first list n),(second list n);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement