math.factorials: adding double-factorial.
parent
25f1758384
commit
50c8033301
|
@ -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
|
||||
|
|
|
@ -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 ] }
|
||||
|
|
Loading…
Reference in New Issue