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

index 976bba91a0626d9c6ea59ddc720fff25142fb618..8a94308e3e1b1d912d8cec54abb49d5e81188941 100644 (file)
@@ -60,3 +60,8 @@ IN: math.factorials
 { { 1 1 2 12 288 } } [ 5 iota [ super-factorial ] map ] unit-test
 
 { { 1 1 4 108 27648 } } [ 5 iota [ hyper-factorial ] map ] unit-test
+
+{ { 1 1 1 5 19 101 619 4421 35899 326981 } }
+[ 10 iota [ alternating-factorial ] map ] unit-test
+
+{ { 1 1 2 9 262144 } } [ 5 iota [ exponential-factorial ] map ] unit-test
index 7964713c783f97c513565c68dc07c45859cf1759..1f03a284566bf134a6c2747cb27b3d39e0ec3c3c 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2013 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: combinators kernel math math.functions math.primes
+USING: combinators fry kernel math math.functions math.primes
 math.ranges memoize sequences ;
 
 IN: math.factorials
@@ -91,3 +91,13 @@ ALIAS: pochhammer rising-factorial
     dup 1 > [
         [1,b] [ dup ^ ] [ * ] map-reduce
     ] [ drop 1 ] if ;
+
+: alternating-factorial ( n -- m )
+    dup 1 > [
+        [ [1,b] ] keep even? '[
+            [ factorial ] [ odd? _ = ] bi [ neg ] when
+        ] map-sum
+    ] [ drop 1 ] if ;
+
+: exponential-factorial ( n -- m )
+    dup 1 > [ [1,b] 1 [ swap ^ ] reduce ] [ drop 1 ] if ;