let collatz_len n =
  let m = ref n in
  let c = ref 0 in
  while !m > 1 do
    if !m mod 2 = 0 then m := !m / 2
    else m := 3 * !m + 1;
    c := !c + 1
  done;
  !c

let euler14 limit =
  let best = ref 0 in
  let best_n = ref 0 in
  for n = 2 to limit do
    let l = collatz_len n in
    if l > !best then begin
      best := l;
      best_n := n
    end
  done;
  !best_n

;;

euler14 100
