let count_primes n =
  let sieve = Array.make (n + 1) true in
  sieve.(0) <- false;
  sieve.(1) <- false;
  for i = 2 to n do
    if sieve.(i) then begin
      let j = ref (i * i) in
      while !j <= n do
        sieve.(!j) <- false;
        j := !j + i
      done
    end
  done;
  let count = ref 0 in
  for i = 2 to n do
    if sieve.(i) then count := !count + 1
  done;
  !count

;;

count_primes 50
