(* exercice 1 *) let a = 2 ;; let f x = a * x;; let a = 3 in f 1 ;; let a = 3;; f 1;; let a = let a = 3 and b = 2 in let a = a + b and b = a - b in a - b ;; let b = 2 in a - b * b ;; (* exerxcice 2*) let a = sqrt(7.) in (1. +. a +. a *. a *. a)/.(1. +. exp(a));; let (a,b)=cos(1.),log(5.) in (log a +. sin b)/.(a +. b);; (* exercice 3 *) let funct1 f = (f 0. +. f 1.)/.2.;; let funct2 (f,x) = let a = f x in a *. a;; (*ou, sans respecter la signature*) let funct2 f x = let a = f x in a *. a;; (*ou en donnant le type de x*) let funct2 (f ,(x : float)) = (f x ) *. (f x);; let funct2 (f ,(x : float)) = let a = f x in a *. a;; let funct3 f = (function (x:float) -> let a = f x in a *. a);; let funct4 f = (function x -> f (x +. 1.));; let f x = 2. *. x;; funct1 f ;; funct2 (f,3.);; funct3 f;; funct3 f 3.;; funct4 f ;; funct4 f 3.;; (* exercice 4*) let delta u = (function n -> u (n+1) - u n );; (* exercice 5*) let parfait k = let somdiv n = let s = ref 1 in (*somme des diviseurs*) for i = 2 to n-1 do if n mod i = 0 then s := !s +i; done; !s in for n = 2 to k do if somdiv n = n then begin print_int n; print_newline(); end; done;; parfait 10000;; (*une version plus rapide *) let parfait2 k = let somdiv n = let s = ref 1 and i = ref 2 in while !i * !i < n do (*on s'arrête avant la racine carré*) if n mod !i = 0 then s := !s + !i + n / !i; i := !i + 1; done; if !i * !i = n then s := !s + !i; (*cas d'un carré*) !s in for n = 2 to k do if somdiv n = n then begin print_int n; print_newline(); end; done;; parfait2 10000;; (* Exercice 6*) let recherche e t = let n = Array.length t in let i = ref 0 and trouve = ref false in while (!i < n) && (e >= t.(!i)) && not !trouve do if e = t.(!i) then trouve := true ; i := !i + 1 done; if !trouve then begin print_string "trouvé en "; print_int (!i-1) end else print_string "pas là";; recherche 5 [|1;3;4;5;8;9|];; recherche 7 [|1;3;4;5;8;9|];; (*autre méthode on utilise une recherche par dichotomie*) let recherche e t = let n = Array.length t in let rec aux g d = let m = (g+d)/2 in if m = d then if t.(m) = e then begin print_string " il est en "; print_int (m+1); print_string " ième position"; end else print_string " il n'est pas là " else if t.(m) >= e then aux g (m) else aux (m+1) d; in aux 0 (n-1);; (* Exercice 7 : algorithme d'Horner *) (*on écrit P = a0 + x (a1 + x (a2 +x ...*) let valeur a p = let n = Array.length p -1 in let v = ref (a * p.(n)) in for i = 1 to n-1 do v := a * (p.(n-i) + !v) done; !v + p.(0);; valeur 2 [|1;-1;1|];; (*version récursive*) let valeur a p = let n = Array.length p -1 in let rec aux i res = match i with | 0 -> p.(0) + res | i -> aux (i-1) (a * (p.(i) + res)) in aux n 0;; (* Exercice 8 : *) (*version iterative*) let log2 n = let puissance = ref 1 and res = ref 0 in while !puissance <= n do puissance:= !puissance*2; res := !res +1; done; (!res-1);; (*version récursive*) let log2rec n = let rec aux n res = match n with | 0 -> res -1 | n -> aux (n/2) (res+1) in aux n 0;; (* ces deux fonctions ne sont bien entendu pas définie en 0*) (*Exercice 9 : *) (*première version : on fait des boucles sur les chiffres*) let amstrong () = let cube n = n*n*n in (* commence par les nombres à 4 chiffres*) for i = 1 to 9 do (*chiffre de 10**3*) for j = 0 to 9 do (*chiffre de 10**2*) for k = 0 to 9 do (* chiffre de 10*) for l = 0 to 9 do let n = i*(cube 10)+j*10*10+k*10+l in (*on calcule le nombre*) if (cube i)+(cube j)+(cube k)+(cube l) = n then begin (*on le compare *) print_int n; print_newline(); end; done done done done; (* on passe aux nombres à 3 chiffres*) for i = 1 to 9 do (*chiffre de 10**2*) for j = 0 to 9 do (*chiffre de 10*) for k = 0 to 9 do let n = i*10*10+j*10+k in if (cube i)+(cube j)+(cube k) = n then begin print_int n; print_newline(); end; done done done;; amstrong();; (* plus court mais pas en complexité*) let amstrong2 () = let cube n = n*n*n in for i = 0 to 9 do (*chiffre de 10**3*) for j = 0 to 9 do (*chiffre de 10**2*) for k = 0 to 9 do (* chiffre de 10*) for l = 0 to 9 do let n = i*(cube 10)+j*10*10+k*10+l in if (i <>0) or (j<>0) then (*il ne faut pas que i et j soient nuls en même temps*) if (cube i)+(cube j)+(cube k)+(cube l) = n then begin print_int n; print_newline(); end; done done done done;; (*version par parcours des nombres avec calcul de la somme des cubes récursive*) let amstrong3 () = let cube n = n*n*n in let rec aux n res = match n with | 0 -> res | n -> aux (n /10) (res+(cube (n mod 10)) ) in for n = 100 to 9999 do if (aux n 0)=n then begin print_int n; print_newline(); end; done;; amstrong3();; let amstrong4() = let cube n = n*n*n in let rec somme n res = match n with (*somme des cubes de chiffres*) | 0 -> res | n -> somme (n /10) (res+(cube (n mod 10)) ) in for i = 0 to 9 do for j = i to 9 do for k = j to 9 do for l = k to 9 do let n = (cube i)+(cube j)+(cube k)+(cube l) in if n = somme n 0 && n>=100 then begin (*on ne prend que des nombres à 3 ou 4 chiffres*) print_int n; print_newline(); end; done done done done;; amstrong4();;