diff --git a/Algo/Séminaire/Exercices seminaire.md b/Algo/Séminaire/Exercices seminaire.md index 64985f0..68be4dd 100644 --- a/Algo/Séminaire/Exercices seminaire.md +++ b/Algo/Séminaire/Exercices seminaire.md @@ -408,6 +408,23 @@ let power x n = match n with | -1. -> if n mod 2 = 0 then 1. else -1.) | _ -> (let rec p = function | 0 -> 1. - | n -> x*.p(n-1) in p n) + | n -> (let res. = p x (n/2) in + res *. res *. (if n mod 2 = 1 then x else 1.) + in p x n) +;; + +(*Correction v3*) +let power x n = match n with + | 0 -> (match x with + | 0. -> failwith "power 0^0 impossible" + | _ -> 1.) + | _ -> (match x with + | 1. -> 1. + | 0. -> 0. + | -1. -> if n mod 2 = 0 then 1. else -1.) + | _ -> (let rec p x = function + | 0 -> 1. + | n -> p (x*x) (n/2) *. (if n mod 2 = 1 then x else 1.) + in p x n) ;; ``` \ No newline at end of file