]> gitweb.factorcode.org Git - factor.git/commitdiff
math.factorials: adding double-factorial.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Apr 2013 04:13:46 +0000 (21:13 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Apr 2013 04:13:46 +0000 (21:13 -0700)
extra/math/factorials/factorials-tests.factor
extra/math/factorials/factorials.factor

index 52684a366bbc488dad90b6d6e3c1b7b3d73bb8be..e40b20b9ddea42606a8ad0af9abdded195bd23aa 100644 (file)
@@ -1,4 +1,4 @@
-USING: kernel math.functions tools.test ;
+USING: kernel math.functions sequences tools.test ;
 IN: math.factorials
 
 [ 1 ] [ -1 factorial ] unit-test ! not necessarily correct
@@ -6,6 +6,9 @@ IN: math.factorials
 [ 1 ] [ 1 factorial ] unit-test
 [ 3628800 ] [ 10 factorial ] unit-test
 
+{ { 1 1 2 3 8 15 48 105 384 945 } }
+[ 10 iota [ double-factorial ] map ] unit-test
+
 { 1 } [ 10 10 factorial/ ] unit-test
 { 720 } [ 10 7 factorial/ ] unit-test
 { 604800 } [ 10 3 factorial/ ] unit-test
index 957a94fd9db54437bbcc47a0903ea953ac0b7dde..8760ce537fca7dcc9e97143369a0d65cea31bd3e 100644 (file)
@@ -9,6 +9,15 @@ IN: math.factorials
 MEMO: factorial ( n -- n! )
     dup 1 > [ [1,b] product ] [ drop 1 ] if ;
 
+ALIAS: n! factorial
+
+MEMO: double-factorial ( n -- n!! )
+    dup 1 > [
+        dup even? 2 1 ? swap 2 <range> product
+    ] [ drop 1 ] if ;
+
+ALIAS: n!! double-factorial
+
 : factorial/ ( n k -- n!/k! )
     {
         { [ dup 1 <= ] [ drop factorial ] }