(* Exponentiation rapide

   variante du programme 80 page 328 *)

let rec exp_rec r x n =
  if n = 0 then
    r
  else
    exp_rec (if n mod 2 = 0 then r else r * x) (x * x) (n / 2)

let exp x n =
  exp_rec 1 x n

let () =
  assert (exp 2 4 = 16);
  assert (exp 1 15 = 1);
  assert (exp 2 15 = 32768)


This document was generated using caml2html