! Copyright (C) 2007-2009 Samuel Tardieu.
! See http://factorcode.org/license.txt for BSD license.
-USING: combinators combinators.short-circuit fry kernel locals
+USING: combinators combinators.short-circuit kernel
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 ranges
literals random sequences sets vectors ;
IN: math.primes
<PRIVATE
: look-in-bitmap ( n -- ? )
- $[ 8999999 sieve ] marked-unsafe? ; inline
+ integer>fixnum $[ 8,999,999 sieve ] marked-unsafe? ; inline
: (prime?) ( n -- ? )
- dup 8999999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
+ dup 8,999,999 <= [ look-in-bitmap ] [ miller-rabin ] if ;
-: simple? ( n -- ? ) { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
+: simple? ( n -- ? )
+ { [ even? ] [ 3 divisor? ] [ 5 divisor? ] } 1|| ;
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.
+! 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 ;
: nprimes ( n -- seq )
2 swap [ [ next-prime ] keep ] replicate nip ;
-: coprime? ( a b -- ? ) fast-gcd 1 = ; foldable
+: coprime? ( a b -- ? ) simple-gcd 1 = ; foldable
: random-prime ( numbits -- p )
[ ] [ 2^ ] [ random-bits* next-prime ] tri