math.factorials: adding double-factorial.

db4
John Benediktsson 2013-04-08 21:13:46 -07:00
parent 25f1758384
commit 50c8033301
2 changed files with 13 additions and 1 deletions

View 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

View 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 ] }