: 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* ;
:: nsieve ( n -- pi(n) )
[let | xs [ n init-sieve ] |
xs [| i |
1 = [
0 i n multiples xs set-all
] when
] each-index
xs sum
] ;