1 ! Copyright (C) 2009 Samuel Tardieu.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bit-arrays kernel math math.functions math.ranges sequences ;
12 : mark-multiples ( i arr -- )
13 [ index> [ sq >index ] keep ] dip
14 [ length 1 - swap <range> f swap ] keep
15 [ set-nth ] curry with each ;
17 : maybe-mark-multiples ( i arr -- )
18 2dup nth [ mark-multiples ] [ 2drop ] if ;
20 : init-sieve ( n -- arr )
21 >index 1 + <bit-array> dup set-bits ;
24 [ init-sieve ] [ sqrt >index [0,b] ] bi
25 over [ maybe-mark-multiples ] curry each ; foldable