let euler40 () =
  let buf = Buffer.create 4096 in
  let len = ref 0 in
  let i = ref 1 in
  while !len < 1500 do
    let s = string_of_int !i in
    Buffer.add_string buf s;
    len := !len + String.length s;
    i := !i + 1
  done;
  let s = Buffer.contents buf in
  let prod = ref 1 in
  let positions = [1; 10; 100; 1000] in
  List.iter (fun p ->
    let c = s.[p - 1] in
    prod := !prod * (Char.code c - Char.code '0')
  ) positions;
  !prod

;;

euler40 ()
