-USING: kernel math.functions tools.test ;
+USING: kernel math.functions sequences tools.test ;
IN: math.factorials
[ 1 ] [ -1 factorial ] unit-test ! not necessarily correct
[ 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
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 ] }