Paste: nsieve with stack
Author: | slava |
Mode: | factor |
Date: | Sat, 27 Sep 2008 14:34:29 |
Plain Text |
: init-sieve ( n -- xs )
[ 1 ] B{ } replicate-as
0 over set-first
0 over set-second ; inline
: set-all ( elt indices seq -- )
swap [ swap set-nth ] with with each ; inline
: multiples ( i n -- range )
over [ 2 * ] [ 1 - ] [ ] tri* <range> ; inline
: sieve-step ( i n xs -- )
[ [ 0 ] 2dip multiples ] dip set-all ; inline
: sieve-loop ( n xs -- )
tuck '[ swap 1 = [ _ _ sieve-step ] [ drop ] if ] each-index ;
: nsieve ( n -- pi(n) )
dup init-sieve [ sieve-loop ] [ sum ] bi ;
New Annotation