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

;;

collatz_length 27
