Sum of Divisors for OCaml Golf Competition by clairvy

let l () =
    let p n=
        let f m=
            let rec _f s i m=
                if i == 0 then s else _f (s + if m mod i == 0 then i else 0) (i - 1) m in _f 0 m m
    in
        Printf.printf "%d: %d\n" n (f n)
    in
        let rec r () =
            p (int_of_string (read_line ()));
            r ()
        in
            try r () with End_of_file -> ()
        ;;

l ()

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

download

return to the top page