-USING: kernel math.functions sequences tools.test ;
+USING: kernel math.functions math.ranges 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/0. 1/105 1/0. -1/15 1/0. 1/3 1/0. -1 1/0.
+ 1 1 1 2 3 8 15 48 105 384 945 3840
+ }
+} [ -10 10 [a,b] [ double-factorial ] map ] unit-test
{ 1 } [ 10 10 factorial/ ] unit-test
{ 720 } [ 10 7 factorial/ ] unit-test
ALIAS: n! factorial
MEMO: double-factorial ( n -- n!! )
- dup 1 > [
- dup even? 2 1 ? swap 2 <range> product
- ] [ drop 1 ] if ;
+ dup [ even? ] [ 0 < ] bi [
+ [ drop 1/0. ] [
+ 2 + -1 swap -2 <range> product recip
+ ] if
+ ] [
+ 2 3 ? swap 2 <range> product
+ ] if ;
ALIAS: n!! double-factorial