math.primes: little bit more cleanup.
parent
ab50fc818f
commit
40b699cc1e
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2007-2009 Samuel Tardieu.
|
! Copyright (C) 2007-2009 Samuel Tardieu.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: combinators combinators.short-circuit fry kernel math
|
USING: combinators combinators.short-circuit fry kernel locals
|
||||||
math.bitwise math.functions math.order math.primes.erato
|
math math.bitwise math.functions math.order math.primes.erato
|
||||||
math.primes.erato.private math.primes.miller-rabin math.ranges
|
math.primes.erato.private math.primes.miller-rabin math.ranges
|
||||||
literals random sequences sets vectors ;
|
literals random sequences sets vectors ;
|
||||||
IN: math.primes
|
IN: math.primes
|
||||||
|
@ -14,21 +14,6 @@ IN: math.primes
|
||||||
: (prime?) ( n -- ? )
|
: (prime?) ( n -- ? )
|
||||||
dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
|
dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
|
||||||
|
|
||||||
! In order not to reallocate large vectors, we compute the upper bound
|
|
||||||
! of the number of primes in a given interval. We use a double inequality given
|
|
||||||
! by Pierre Dusart in 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 )
|
|
||||||
dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
|
|
||||||
|
|
||||||
: lower-pi ( x -- y )
|
|
||||||
dup log [ / ] [ 0.992 swap / 1 + ] bi * floor ;
|
|
||||||
|
|
||||||
: <primes-vector> ( low high -- vector )
|
|
||||||
swap [ [ upper-pi ] [ lower-pi ] bi* - >integer
|
|
||||||
108 max 10000 min <vector> ] keep
|
|
||||||
3 < [ 2 suffix! ] when ;
|
|
||||||
|
|
||||||
: simple? ( n -- ? ) { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
|
: simple? ( n -- ? ) { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
@ -49,10 +34,27 @@ PRIVATE>
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
|
: <primes-range> ( low high -- range )
|
||||||
|
[ 3 max dup even? [ 1 + ] when ] dip 2 <range> ;
|
||||||
|
|
||||||
|
! In order not to reallocate large vectors, we compute the upper bound
|
||||||
|
! of the number of primes in a given interval. We use a double inequality given
|
||||||
|
! by Pierre Dusart in 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 )
|
||||||
|
dup log [ / ] [ 1.2762 swap / 1 + ] bi * ceiling ;
|
||||||
|
|
||||||
|
: lower-pi ( x -- y )
|
||||||
|
dup log [ / ] [ 0.992 swap / 1 + ] bi * floor ;
|
||||||
|
|
||||||
|
:: <primes-vector> ( low high -- vector )
|
||||||
|
high upper-pi low lower-pi - >integer
|
||||||
|
108 10000 clamp <vector>
|
||||||
|
low 3 < [ 2 suffix! ] when ;
|
||||||
|
|
||||||
: (primes-between) ( low high -- seq )
|
: (primes-between) ( low high -- seq )
|
||||||
[ [ 3 max dup even? [ 1 + ] when ] dip 2 <range> ]
|
[ <primes-range> ] [ <primes-vector> ] 2bi
|
||||||
[ <primes-vector> ] 2bi
|
[ '[ [ prime? ] _ push-if ] each ] keep ;
|
||||||
[ '[ [ prime? ] _ push-if ] each ] keep clone ;
|
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -65,9 +67,11 @@ PRIVATE>
|
||||||
[ (primes-between) ]
|
[ (primes-between) ]
|
||||||
} cond ;
|
} cond ;
|
||||||
|
|
||||||
: primes-upto ( n -- seq ) 2 swap primes-between ;
|
: primes-upto ( n -- seq )
|
||||||
|
2 swap primes-between ;
|
||||||
|
|
||||||
: nprimes ( n -- seq ) 2 swap [ [ next-prime ] keep ] replicate nip ;
|
: nprimes ( n -- seq )
|
||||||
|
2 swap [ [ next-prime ] keep ] replicate nip ;
|
||||||
|
|
||||||
: coprime? ( a b -- ? ) fast-gcd 1 = ; foldable
|
: coprime? ( a b -- ? ) fast-gcd 1 = ; foldable
|
||||||
|
|
||||||
|
@ -80,18 +84,10 @@ PRIVATE>
|
||||||
|
|
||||||
ERROR: no-relative-prime n ;
|
ERROR: no-relative-prime n ;
|
||||||
|
|
||||||
<PRIVATE
|
|
||||||
|
|
||||||
: (find-relative-prime) ( n guess -- p )
|
|
||||||
over 1 <= [ over no-relative-prime ] when
|
|
||||||
dup 1 <= [ drop 3 ] when
|
|
||||||
[ 2dup coprime? ] [ 2 + ] until nip ;
|
|
||||||
|
|
||||||
PRIVATE>
|
|
||||||
|
|
||||||
: find-relative-prime* ( n guess -- p )
|
: find-relative-prime* ( n guess -- p )
|
||||||
#! find a prime relative to n with initial guess
|
[ dup 1 <= [ no-relative-prime ] when ]
|
||||||
>odd (find-relative-prime) ;
|
[ >odd dup 1 <= [ drop 3 ] when ] bi*
|
||||||
|
[ 2dup coprime? ] [ 2 + ] until nip ;
|
||||||
|
|
||||||
: find-relative-prime ( n -- p )
|
: find-relative-prime ( n -- p )
|
||||||
dup random find-relative-prime* ;
|
dup random find-relative-prime* ;
|
||||||
|
|
Loading…
Reference in New Issue