]> gitweb.factorcode.org Git - factor.git/commitdiff
Short circuit trivial composites
authorSamuel Tardieu <sam@rfc1149.net>
Wed, 24 Jun 2009 13:36:45 +0000 (15:36 +0200)
committerSamuel Tardieu <sam@rfc1149.net>
Wed, 24 Jun 2009 13:53:50 +0000 (15:53 +0200)
basis/math/primes/primes.factor

index 5dc6a513349d1f2d4d19653ae04ab535afb3aa11..7e877a03ce3f9dfcd91fca9734c73ef0adb78260 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2007-2009 Samuel Tardieu.
 ! See http://factorcode.org/license.txt for BSD license.
-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
-vectors ;
+USING: combinators combinators.short-circuit 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 vectors ;
 IN: math.primes
 
 <PRIVATE
@@ -28,12 +28,14 @@ IN: math.primes
     108 max 10000 min <vector> ] keep
     3 < [ [ 2 swap push ] keep ] when ;
 
+: simple? ( n -- ? ) { [ even? ] [ 3 mod 0 = ] [ 5 mod 0 = ] } 1|| ;
+
 PRIVATE>
 
 : prime? ( n -- ? )
     {
         { [ dup 7 < ] [ { 2 3 5 } member? ] }
-        { [ dup even? ] [ 2 = ] }
+        { [ dup simple? ] [ drop f ] }
         [ (prime?) ]
     } cond ; foldable