math.primes: fix "11 >bignum prime?".
parent
4550294227
commit
e3ec051527
|
@ -39,3 +39,5 @@ IN: math.primes.tests
|
||||||
|
|
||||||
[ 5 t { 14 14 14 14 14 } ]
|
[ 5 t { 14 14 14 14 14 } ]
|
||||||
[ 5 15 unique-primes [ length ] [ [ prime? ] all? ] [ [ log2 ] map ] tri ] unit-test
|
[ 5 15 unique-primes [ length ] [ [ prime? ] all? ] [ [ log2 ] map ] tri ] unit-test
|
||||||
|
|
||||||
|
{ t t } [ 11 dup >bignum [ prime? ] bi@ ] unit-test
|
||||||
|
|
|
@ -9,12 +9,13 @@ IN: math.primes
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: look-in-bitmap ( n -- ? )
|
: look-in-bitmap ( n -- ? )
|
||||||
$[ 8999999 sieve ] marked-unsafe? ; inline
|
integer>fixnum $[ 8999999 sieve ] marked-unsafe? ; inline
|
||||||
|
|
||||||
: (prime?) ( n -- ? )
|
: (prime?) ( n -- ? )
|
||||||
dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
|
dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
|
||||||
|
|
||||||
: simple? ( n -- ? ) { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
|
: simple? ( n -- ? )
|
||||||
|
{ [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -37,10 +38,12 @@ PRIVATE>
|
||||||
: <primes-range> ( low high -- range )
|
: <primes-range> ( low high -- range )
|
||||||
[ 3 max dup even? [ 1 + ] when ] dip 2 <range> ;
|
[ 3 max dup even? [ 1 + ] when ] dip 2 <range> ;
|
||||||
|
|
||||||
! In order not to reallocate large vectors, we compute the upper bound
|
! In order not to reallocate large vectors, we compute the upper
|
||||||
! of the number of primes in a given interval. We use a double inequality given
|
! bound of the number of primes in a given interval. We use a
|
||||||
! by Pierre Dusart in http://www.ams.org/mathscinet-getitem?mr=99d:11133
|
! double inequality given by Pierre Dusart in
|
||||||
! for x > 598. Under this limit, we know that there are at most 108 primes.
|
! http://www.ams.org/mathscinet-getitem?mr=99d:11133 for x >
|
||||||
|
! 598. Under this limit, we know that there are at most 108
|
||||||
|
! primes.
|
||||||
: upper-pi ( x -- y )
|
: upper-pi ( x -- y )
|
||||||
dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
|
dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue