Skip to content

Instantly share code, notes, and snippets.

@TheophileWalter
Created March 9, 2017 15:54
Show Gist options
  • Save TheophileWalter/4eb7a65a250037fcec19845a763063aa to your computer and use it in GitHub Desktop.
Save TheophileWalter/4eb7a65a250037fcec19845a763063aa to your computer and use it in GitHub Desktop.
(* Open the big integer module, requires to compilate with "nums.cma" *)
open Big_int;;
(* Get the value as a big_int *)
let n = big_int_of_string Sys.argv.(1) in
(* Set up some big_int that we need use *)
let bi1 = big_int_of_int 1 in
(* The function *)
let rec fact_big_int n =
if le_big_int n bi1 then bi1
else mult_big_int n (fact_big_int (sub_big_int n bi1))
in
(* Compute and print *)
let result = string_of_big_int (fact_big_int n) in
Printf.printf "fact(%s) = %s\n" Sys.argv.(1) result;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment