Skip to content

Instantly share code, notes, and snippets.

@khzaw
Last active August 29, 2015 14:11
Show Gist options
  • Save khzaw/671527263e46ead40c02 to your computer and use it in GitHub Desktop.
Save khzaw/671527263e46ead40c02 to your computer and use it in GitHub Desktop.
Recursive exponentiation
let (<>) a b =
let rec timesPrime a b acc = match b with
| 0 -> acc
| _ -> timesPrime a (pred b) (acc+a)
in timesPrime a b 0
;;
let rec pow x = function
| 0 -> 1
| n when n mod 2 = 0 ->
let x = pow x (n/2) in
(x <> x)
| n -> (pow x (n-1) <> x)
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment