Skip to content

Instantly share code, notes, and snippets.

@ziotom78
Created September 28, 2011 07:54
Show Gist options
  • Save ziotom78/1247276 to your computer and use it in GitHub Desktop.
Save ziotom78/1247276 to your computer and use it in GitHub Desktop.
Solution for problem 34 in OCaml
let fast_fact =
let rec fact n = if n > 1 then n * fact (n - 1) else 1
in Array.map fact [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|];;
let digits num =
let rec helper num result =
if num < 10 then num :: result else helper (num / 10) ((num mod 10) :: result)
in helper num [];;
let test_number num =
num == (List.fold_left (+) 0 (List.map (fun x -> fast_fact.(x)) (digits num))) ;;
let (--) i j =
let rec aux n acc =
if n < i then acc else aux (n-1) (n :: acc)
in aux j [] ;;
let _ = print_endline (string_of_int (List.fold_left (+) 0 (List.filter test_number (10--10000000)))) ;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment