1
(***********************************************************************)
5
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 2001 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* only by permission. *)
11
(***********************************************************************)
13
(* Erathostene sieve, imperative version.
14
A vector is initialized with integers.
15
Then the vector is sieved. *)
17
let fixed_bound = 5000000;;
21
let v = Array.init max (fun i -> i + 1) in
22
for i = 0 to max - 1 do v.(i) <- i + 1 done;
24
let prime_count = ref 0 in
29
for i = 1 to max - 1 do
30
if Array.unsafe_get v i <> 0 then begin
31
prime_count := !prime_count + 1;
34
if j < max then begin Array.unsafe_set v j 0; sieve (j + prime) end in
39
"There are %d primes less than or equal to %d.\n" !prime_count max;;
43
try int_of_string (Sys.argv.(1))
44
with _ -> fixed_bound in