]> gitweb.factorcode.org Git - factor.git/commitdiff
Get rid of vector reallocation by preallocating it
authorSamuel Tardieu <sam@rfc1149.net>
Wed, 24 Jun 2009 13:27:58 +0000 (15:27 +0200)
committerSamuel Tardieu <sam@rfc1149.net>
Wed, 24 Jun 2009 13:53:46 +0000 (15:53 +0200)
basis/math/primes/primes.factor

index ea8c60508d4a69e0c983a8643f9bc22f8bf185aa..5dc6a513349d1f2d4d19653ae04ab535afb3aa11 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2007-2009 Samuel Tardieu.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: combinators kernel math math.bitwise math.functions
+USING: combinators fry kernel math math.bitwise math.functions
 math.order math.primes.erato math.primes.erato.private
-math.primes.miller-rabin math.ranges literals random sequences sets ;
+math.primes.miller-rabin math.ranges literals random sequences sets
+vectors ;
 IN: math.primes
 
 <PRIVATE
@@ -12,6 +13,21 @@ IN: math.primes
 : (prime?) ( n -- ? )
     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 swap push ] keep ] when ;
+
 PRIVATE>
 
 : prime? ( n -- ? )
@@ -29,9 +45,9 @@ PRIVATE>
     ] if ; foldable
 
 : primes-between ( low high -- seq )
-    [ dup 3 max dup even? [ 1 + ] when ] dip
-    2 <range> [ prime? ] filter
-    swap 3 < [ 2 prefix ] when ;
+    [ [ 3 max dup even? [ 1 + ] when ] dip 2 <range> ]
+    [ <primes-vector> ] 2bi
+    [ '[ [ prime? ] _ push-if ] each ] keep clone ;
 
 : primes-upto ( n -- seq ) 2 swap primes-between ;