(* TP PF sujet n°1 *) (* -------------------------------------------------------- *) (* III *) (* (a) *) let rec puissance x n = if n=0 then 1. else x*.(puissance x (n-1));; let rec pgcd a b = if b=0 then a else pgcd b (a mod b);; let rec fib n = if n=0 then 0 else if n=1 then 1 else (fib (n-1))+(fib (n-2));; let rec existe_diviseur n m = (* test l'existence d'un diviseur de n plus petit que m (pour m>=1) *) (m>1) & ((n mod m = 0) or (existe_diviseur n (m-1)));; let premier n = (* détermine si n est un nombre premier (pour n>1) *) not (existe_diviseur n (n-1));; (* (d) *) let rec nb_add_fib n = if n=0 then 0 else if n=1 then 0 else nb_add_fib (n-1) + nb_add_fib (n-2) + 1;; (* on a : nb_add_fib n = (fib n) - 1 *) (* (e) *) let rec fib2 n = if n=0 then (0,1) else let (a,b) = fib2 (n-1) in (b,a+b);; let rec nb_add_fib2 n = if n=0 then 0 else nb_add_fib2 (n-1) + 1;; (* on a : nb_add_fib2 n = n *) (* -------------------------------------------------------- *) (* IV *) (* (a) *) function (x,y) -> if x=0 then y else 0.;; (0,(function x -> if x=0. then x else 0.));; (0,0.,0.);; (0,(0.,0.));; function x -> function y -> if x=0 then y else 0.;; function x -> (x 0)+.0.;; ((function x -> if x=0 then 0. else 0.), (function x -> if x=0 then 0. else 0.));; (* (b) *) let integrale (a,b) f = (* calcul de l'intégrale de f sur l'intervalle (a,b) *) let h = (b-.a)/.6. in h*.(f(a)+.4.*.f((a+.b)/.2.)+.f(b));; let derivee f = (* dérivée d'une fonction dérivable *) function x0 -> let h = 0.0001 in (f(x0+.h)-.f(x0))/.h;; let compose f g = (* composée de 2 fonctions *) function x -> f(g(x));; let u(x) = 3.*.x*.x+.1.;; let v(x) = sin(x);; let g = u in let f = v in let x = 2. in (derivee (compose g f))(x);; (* - : float = -2.2706035638764277 *) let g = u in let f = v in let x = 2. in ((derivee f)(x))*.((derivee g)(f(x)));; (* - : float = -2.27078038638068591 *) let f = v in let a = 0. in let x = 0.5 in (derivee (function x -> (integrale (a,x) f)))(x);; (* - : float = 0.479501407252003187 *) let f = v in let x = 0.5 in f(x);; (* - : float = 0.479425538604203 *) (* (d) *) let rec sigma u n = if n=0 then u(0) else u(n)+(sigma u (n-1));; (* (e) *) sigma (function n -> n) 100;; sigma (function n -> if n mod 2 = 0 then n else -n) 100;; let inv100 x = let rec sigma u n = if n=0 then u(0) else u(n)+.(sigma u (n-1)) in sigma (function n -> (puissance (1.-.x) n)) 100;; (* inv100 x = 1./.x pour 0