]> gitweb.factorcode.org Git - factor.git/commitdiff
math.factorials: more factorial words.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Apr 2013 19:07:49 +0000 (12:07 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Apr 2013 19:07:49 +0000 (12:07 -0700)
extra/math/factorials/factorials-tests.factor
extra/math/factorials/factorials.factor

index 8a94308e3e1b1d912d8cec54abb49d5e81188941..d708f7974fd7be0a84b6950916fc9ba93097e6d6 100644 (file)
@@ -65,3 +65,9 @@ IN: math.factorials
 [ 10 iota [ alternating-factorial ] map ] unit-test
 
 { { 1 1 2 9 262144 } } [ 5 iota [ exponential-factorial ] map ] unit-test
+
+{ V{ 2 3 5 7 23 719 5039 } }
+[ 10,000 iota [ factorial-prime? ] filter ] unit-test
+
+{ V{ 3 5 7 29 31 211 2309 2311 } }
+[ 10,000 iota [ primorial-prime? ] filter ] unit-test
index 1f03a284566bf134a6c2747cb27b3d39e0ec3c3c..8da12f447ca9f7b692810b853417e90b6d73d68d 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2013 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: combinators fry kernel math math.functions math.primes
-math.ranges memoize sequences ;
+USING: combinators combinators.short-circuit fry kernel math
+math.functions math.primes math.ranges memoize sequences ;
 
 IN: math.factorials
 
@@ -101,3 +101,24 @@ ALIAS: pochhammer rising-factorial
 
 : exponential-factorial ( n -- m )
     dup 1 > [ [1,b] 1 [ swap ^ ] reduce ] [ drop 1 ] if ;
+
+: factorial-prime? ( n -- ? )
+    {
+        [ prime? ]
+        [
+            1 1 [ pick over - 1 <= ] [
+                drop [ 1 + ] [ factorial ] bi
+            ] until nip - abs 1 =
+        ]
+    } 1&& ;
+
+: primorial-prime? ( n -- ? )
+    {
+        [ prime? ]
+        [ 2 > ]
+        [
+            1 1 [ pick over - 1 <= ] [
+                drop [ 1 + ] [ primorial ] bi
+            ] until nip - abs 1 =
+        ]
+    } 1&& ;