Problem 5 solved using OCaml