(* * $Id: sieve.ocaml,v 1.10 2001/06/10 04:12:44 doug Exp $ * http://www.bagley.org/~doug/shootout/ * based on code from Markus Mottl *) let flags = String.make 8193 'f' let rec inner_loop k i = if k < 8193 then begin flags.[k] <- 'f'; inner_loop (k + i) i end let rec middle_loop i cnt = if i < 8193 then if flags.[i] = 't' then begin inner_loop (i + i) i; middle_loop (i + 1) (cnt + 1) end else middle_loop (i + 1) cnt else cnt let _ = let num = try int_of_string Sys.argv.(1) with Invalid_argument _ -> 1 and cnt = ref 0 in for iter = 1 to num do for i = 2 to 8192 do flags.[i] <- 't' done; cnt := middle_loop 2 0; done; Printf.printf "Count: %d\n" !cnt