Jump to content

Exponentiation operator: Difference between revisions

m
m (→‎{{header|OCaml}}: indentation)
Line 652:
It is possible to create a generic exponential. For this, one must know the
multiplication function, and the unit value. Here, the usual fast algorithm is
used.:
 
<lang ocaml>let pow one mul a n = let rec g p x = function 0 -> x | i ->
let rec g p x = function
g (mul p p) (if i mod 2 = 1 then mul p x else x) (i/2) in g a one n;;
| 0 -> x
| i ->
g (mul p p) (if i mod 2 = 1 then mul p x else x) (i/2) in g a one n;;
in
g a one n
;;
 
pow 1 ( * ) 2 16;; (* 65536 *)
Line 674 ⟶ 680:
*)</lang>
 
See also [[Matrix-exponentiation_operatorexponentiation operator#OCaml]] for a matrix usage.
 
=={{header|Perl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.