Sum of Divisors for OCaml Golf Competition by camlspotter

(* damn, I'm good! *)
let comp n =
  let rec f = function
      | 0 -> 0
      | i when n mod i = 0 -> i + f (i - 1)
      | i -> f (i - 1)
  in
  f n
;;

let _ =
  try 
    while true do
      let n = int_of_string (read_line ()) in
      let r = comp n in
      Printf.printf "%d: %d\n" n r
    done
  with
  | End_of_file -> ()
;;

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page