(* Le texte entre (* *) est du commentaire : Caml l'ignore. *) (****** Question 1 ******) let fact n = if n<0 then 0 else let yo=ref 1 in for i=2 to n do yo:= !yo*i; done; !yo;; fact 10;; (****** Question 2 ******) let somme f n = let s = ref 0 in for i=1 to n do s:= !s+(f i); done; !s;; let produit f n = let p = ref 1 in for i=1 to n do p:= !p*(f i); done; !p;; let fact2 = produit (fun x->x);; fact2 10;; (****** Question 3 ******) let fibo n = if n<2 then 1 else ( let a=ref 1 and b=ref 1 in for i=2 to n do b:= !b + !a; a:= !b - !a; done; !b );; fibo 10;; let approx_nb_or () = let a=ref 1.0 and b=ref 1.0 and r=ref 1.0 and r2=ref 0.0 in while abs_float(!r -. !r2) > 1e-7 do let c= !b in b:= c +. !a; a:=c; r2 := !r; r := !b /. !a; done; !r;; approx_nb_or();; (1. +. sqrt 5.) /. 2. ;; (****** Question 4 ******) let iter f n x0 = let yo=ref x0 in for i=1 to n do yo:= f i !yo done; !yo;; let fact3 n = iter (prefix *) n 1;; fact3 10;; (****** Question 5 & 6 ******) read_line;; print_endline "bonjour";; print_int 45;; (****** Question 7 ******) let blackjack () = let cagnotte = ref 100 and total=ref 0 and mise=ref 0 in while !cagnotte>0 do print_string "Votre cagnotte est de "; print_int !cagnotte; print_newline(); print_string "Votre carte : "; total:= 1+random__int 11; print_int !total; print_newline(); print_endline "Combien misez-vous ?"; mise:=read_int(); while !mise> !cagnotte do print_endline "Tu n'as pas assez de sous! Reessaye :"; mise:=read_int(); done; let continue = ref true in while !continue do let carte = 1+random__int 11 in print_string "Votre carte : "; print_int carte; total:= !total+carte; print_string ". Total : "; print_int !total; if !total>21 then continue:=false else ( print_string ". Souhaitez-vous continuer ? (o/n)"; continue := (read_line() = "o") ); print_newline(); done; if !total>21 then ( print_endline "perdu!"; cagnotte := !cagnotte - !mise ) else if !total>=17 then ( print_endline "gagne!"; cagnotte := !cagnotte + !mise ) else print_endline "petit joueur..."; done;; (****** Question 8 ******) let dichotomie f a b = let x = ref a and y = ref b in while abs_float(!y -. !x)>1e-7 do let c = 0.5*.(!x +. !y) in if (f c) < 0.0 then x:=c else y:=c; done; !x;; let yo x = x *. x +. x -. 1.;; dichotomie yo 0. 2.;; (****** Question 9 ******) let factor n = let m = ref n and d = ref 2 in while !d * !d <= !m do let puissance = ref 0 in while (!m mod !d) = 0 do m := !m / !d; incr puissance; done; if !puissance > 0 then ( print_int !d; if !puissance > 1 then ( print_string "^"; print_int !puissance; ); print_string " "; ); incr d; done; if !m > 1 then print_int !m;; factor 100;; factor 1001;; factor 100000001;; factor 1000000007;; (****** Question 10 ******) type fiche = { nom:string; taille:int; poids:float };; let ma_fiche={nom="moi"; taille=171; poids=62.5; };; ma_fiche.taille;; let imc x = print_string ("Bonjour " ^ x.nom ^ ", votre indice de masse corporelle est de "); print_float (1000. *. x.poids /. float_of_int(x.taille*x.taille)); print_newline();; imc ma_fiche;; (****** Question 11 ******) type complexe = { re:float; im:float; };; let i = { re=0.0; im=1.0; };; let add x y = { re = x.re +. y.re; im = x.im +. y.im; };; let sub x y = { re = x.re -. y.re; im = x.im -. y.im; };; let conj x = { re = x.re; im = 0. -. x.im; };; let mul x y = { re = (x.re*.y.re)-.(x.im*.y.im); im=(x.re*.y.im)+.(x.im*.y.re); };; let div x y = let mod2 = (y.re*.y.re)+.(y.im*.y.im) and tmp=mul x (conj y) in { re=tmp.re/.mod2; im=tmp.im/.mod2; };; mul i i;; conj i;; div (sub i (mul i i)) (add i i);; (*** on a bien (i+1)/2i = (1-i)/2 ***) (****** Question 12 ******) let puiss z n = let prod = ref { re=1.0; im=0.0; } in for i=1 to n do prod := mul !prod z; done; !prod;; puiss i 12;; let exp_rapide z n = let prod = ref { re=1.0; im=0.0; } and mult = ref z and e = ref n in while !e>0 do if !e mod 2 > 0 then prod:= mul !prod !mult; e := !e / 2; mult := mul !mult !mult; done; !prod;; (* ca broute... *) puiss i 1000003;; (* ca broute pas *) exp_rapide i 1000000003;;