new accessors
parent
fbc1076ac2
commit
cde71318ab
|
@ -1,7 +1,7 @@
|
|||
! Copyright (c) 2007 Samuel Tardieu.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: bit-arrays kernel lists.lazy math math.functions math.primes.list
|
||||
math.ranges sequences ;
|
||||
math.ranges sequences accessors ;
|
||||
IN: math.erato
|
||||
|
||||
<PRIVATE
|
||||
|
@ -12,21 +12,21 @@ TUPLE: erato limit bits latest ;
|
|||
2/ 1- ; inline
|
||||
|
||||
: is-prime ( n erato -- bool )
|
||||
>r ind r> erato-bits nth ; inline
|
||||
>r ind r> bits>> nth ; inline
|
||||
|
||||
: indices ( n erato -- range )
|
||||
erato-limit ind over 3 * ind swap rot <range> ;
|
||||
limit>> ind over 3 * ind swap rot <range> ;
|
||||
|
||||
: mark-multiples ( n erato -- )
|
||||
over sq over erato-limit <=
|
||||
[ [ indices ] keep erato-bits [ f -rot set-nth ] curry each ] [ 2drop ] if ;
|
||||
over sq over limit>> <=
|
||||
[ [ indices ] keep bits>> [ f -rot set-nth ] curry each ] [ 2drop ] if ;
|
||||
|
||||
: <erato> ( n -- erato )
|
||||
dup ind 1+ <bit-array> 1 over set-bits erato boa ;
|
||||
|
||||
: next-prime ( erato -- prime/f )
|
||||
[ erato-latest 2 + ] keep [ set-erato-latest ] 2keep
|
||||
2dup erato-limit <=
|
||||
[ 2 + ] change-latest [ latest>> ] keep
|
||||
2dup limit>> <=
|
||||
[
|
||||
2dup is-prime [ dupd mark-multiples ] [ nip next-prime ] if
|
||||
] [
|
||||
|
|
Loading…
Reference in New Issue