(* -*- mode: sml -*-
* $Id: sieve.smlnj,v 1.8 2001/08/20 01:11:11 doug Exp $
* http://www.bagley.org/~doug/shootout/
* with help from Dan Wang
*)
structure Test : sig
val main : (string * string list) -> OS.Process.status
end = struct
structure WA = Word8Array
val flags = WA.array (8193, 0w0)
fun init() = let
fun loop i =
if i < 8193 then (WA.update(flags,i,0w1);loop(i+1))
else ()
in loop 2
end
fun do_elts(i,count) =
if i < 8193 then
if WA.sub(flags,i) = 0w1 then let
fun loop k =
if k < 8193 then (WA.update(flags,k,0w0);loop(k+i))
else ()
in loop (i + i) ; do_elts(i + 1,count + 1)
end
else do_elts(i + 1, count)
else count
fun repeat 0 = (init (); do_elts(2,0))
| repeat n = (init (); do_elts(2,0);repeat(n-1))
fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t )
fun atoi s = case Int.fromString s of SOME num => num | NONE => 0
fun main(name, param_list) = let
val arg = hd(param_list @ ["1"]);
val num = atoi arg
val count = repeat num
in printl ["Count: ", Int.toString count];
OS.Process.success
end
end
val _ = SMLofNJ.exportFn("sieve", Test.main);