Sys.command "ocaml -version";; type entier = { base : int; t : int array } ;; let quatre = { base = 10; t = [| 4 |] } let quarantedeux = { base = 10; t = [| 2; 4 |] } let n2006 = { base = 10; t = [| 6; 0; 0; 2 |] } let n2006 = { base = 10; t = [| 6; 0; 0; 2 |] } let n2006_2 = { base = 10; t = [| -4; 1; 0; 2 |] } let n2006_3 = { base = 10; t = [| 6; 0; 0; -8; 1 |] } let n2006_4 = { base = 10; t = [| -4; 1; 0; -8; 1 |] } let rec puissance x k = match k with | 0 -> 1 | 1 -> x | k when k mod 2 = 0 -> (* x^k = x^(k/2 * 2) = (x^2)^(k/2) *) puissance (x * x) (k / 2) | k -> (* x^k = x^((k-1)/2 * 2 + 1) = x * (x^2)^((k-1)/2) *) x * (puissance (x * x) ((k - 1) / 2)) ;; puissance 10 0;; puissance 10 1;; puissance 10 2;; puissance 10 3;; puissance 10 4;; let valeur {base=b; t=t} = let v = ref 0 in let n = Array.length t in for k = 0 to n - 1 do v := !v + (t.(k) * (puissance b k)) done; !v ;; valeur quatre;; valeur quarantedeux;; valeur n2006;; valeur n2006_2;; valeur n2006_3;; valeur n2006_4;; let compare_entiers (a : entier) (b : entier) : int = let va = valeur a in let vb = valeur b in compare va vb ;; compare_entiers quatre quarantedeux;; compare_entiers quarantedeux quatre;; compare_entiers quatre quatre;; compare_entiers quarantedeux quarantedeux;; compare_entiers n2006 quarantedeux;;