]> gitweb.factorcode.org Git - factor.git/commitdiff
Optimize primes-between
authorSamuel Tardieu <sam@rfc1149.net>
Sun, 28 Dec 2008 10:43:13 +0000 (11:43 +0100)
committerSamuel Tardieu <sam@rfc1149.net>
Sun, 28 Dec 2008 10:43:13 +0000 (11:43 +0100)
Rather than having primes-between return a slice of primes-upto,
make primes-upto use primes-between.

Also, those two words cannot be marked as foldable as their
output is mutable.

extra/math/primes/primes.factor

index c8f398863fe1fe46dcf1d4de2f1a18657795f8f2..fa42d7385a54a855aee0ecf064f0af72b2f61dd2 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2007 Samuel Tardieu.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: binary-search combinators kernel lists.lazy math math.functions
-math.miller-rabin math.primes.erato math.ranges sequences ;
+USING: combinators kernel lists.lazy math math.functions
+math.miller-rabin math.order math.primes.erato math.ranges sequences ;
 IN: math.primes
 
 <PRIVATE
@@ -28,15 +28,11 @@ PRIVATE>
 : lprimes-from ( n -- list )
     dup 3 < [ drop lprimes ] [ 1- next-prime [ next-prime ] lfrom-by ] if ;
 
-: primes-upto ( n -- seq )
-    dup 2 < [
-        drop V{ }
-    ] [
-        3 swap 2 <range> [ prime? ] filter 2 prefix
-    ] if ; foldable
-
 : primes-between ( low high -- seq )
-    primes-upto [ 1- next-prime ] dip
-    [ natural-search drop ] [ length ] [ ] tri <slice> ; foldable
+    [ dup 3 max dup even? [ 1 + ] when ] dip
+    2 <range> [ prime? ] filter
+    swap 3 < [ 2 prefix ] when ;
+
+: primes-upto ( n -- seq ) 2 swap primes-between ;
 
 : coprime? ( a b -- ? ) gcd nip 1 = ; foldable