I'm tackling this smallest multiple problem on Kattis.
I've solved it in Python and C++, but now I want to do OCaml.
Here's my code:
open Printf;;let rec gcd (a : int64) (b : int64) : int64 = if not (Int64.unsigned_compare b 0L > 0) then a else gcd b (Int64.unsigned_rem a b);;try while true; do let line = read_line () in if String.length line > 0 then let (nums : int64 list) = List.map (fun (s:string) : (int64) -> (Int64.of_string (Char.escaped '0' ^ Char.escaped 'u' ^ s ))) (String.split_on_char '' line) in let rec reduce (li : int64 list) (init : int64) : (int64) = match li with | [] -> init | head :: tail -> reduce tail ( Int64.unsigned_div (Int64.mul head init) (gcd head init) ) in print_string ( Int64.to_string (reduce nums 1L) ^ "\n") else raise End_of_file done;with End_of_file -> ();;
I can get the first case to pass, but the second (and final) case results in a wrong answer. This is weird since my logic is the same in my two earlier submissions of different languages.
My guess is that I'm incorrectly printing unsigned 64 bit integers:
print_string ( Int64.to_string (reduce nums 1L) ^ "\n")
Earlier, I've tried:
printf "%Lu\n" (reduce nums 1L)
But the Printf documentation says
u, n, l, L, or N: convert an integer argument to unsigned decimal. Warning: n, l, L, and N are used for scanf, and should not be used for printf.
and I actually don't think this warning is relevant in my case, but I'm still wary.
Any thoughts? Thanks for reading!