Advertisement
Trietch

Untitled

Nov 9th, 2018
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.95 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 = let rec rreplique l a n = match n with
  22.     | 0 -> []
  23.     | 1 -> l
  24.     | _ -> (rreplique (l@a) a (n-1))
  25.   in (rreplique l l n);;
  26.  
  27. let int_add l m n = let rec reverse = function
  28.     | [] -> []
  29.     | x::l -> (reverse l)@[x] in
  30.   let rec add o p q = match (o,p,q) with
  31.     | ([], [], r) -> (0, [r])
  32.     | (a::[], b::[],r) -> ((a+b+r)/10,[(a+b+r) mod 10])
  33.     | (a1::a2, b1::b2,r) -> let (ret,l) = (add a2 b2 ((a1+b1+r)/10))
  34.         in (ret, ((a1+b1+r) mod 10)::l)
  35.   in let (r,o) = (add (reverse l) (reverse m) n)
  36.   in (r, reverse o) ;;
  37.  
  38.  
  39. let etend_prefixe n (l1,l2) =
  40.   let empty l = match l with
  41.     | [] -> [0]
  42.     | l -> l
  43.   in let nl2 = (empty l2) in
  44.   let rec ep n l = match (l, n) with
  45.     | _, 0 -> []
  46.     | (a::b), n -> [a]@(ep (n-1) b)
  47.     | _, n -> match nl2 with
  48.       | (a::b) -> [a]@(ep (n-1) nl2)
  49.   in let first = l1@(ep n nl2)
  50.   in let rec offset n l = match (l, n) with
  51.       | l, 0 -> l
  52.       | (a::b), _ -> (offset (n-1) b)
  53.       | _, n -> (offset (n-1) nl2)
  54.   in (offset n nl2);;
  55.  
  56. let mkInt l =
  57.   let rec reverse = function
  58.     | [] -> []
  59.     | x::l -> (reverse l)@[x] in
  60.   let pow m n =
  61.     let rec rpow m n = match n with
  62.       | 0 -> 1
  63.       | _ -> (rpow m (n-1))*m
  64.     in if n < 0 then failwith "l'exposant doit etre positif"
  65.     else (rpow m n) in
  66.   let rec rmkint l n =
  67.     match (l,n) with
  68.     | [], _ -> 0
  69.     | (a::b), n -> (a*(pow 10 n))+(rmkint b (n+1))
  70.   in (rmkint (reverse l) 0);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement