let largest_prime_factor n =
  let m = ref n in
  let factor = ref 2 in
  let largest = ref 0 in
  while !m > 1 do
    if !m mod !factor = 0 then begin
      largest := !factor;
      m := !m / !factor
    end else factor := !factor + 1
  done;
  !largest

;;

largest_prime_factor 13195
