Advertisement
Trietch

Untitled

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