From 50c8033301a43c807c060089826c0d9a007c37f7 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 8 Apr 2013 21:13:46 -0700 Subject: [PATCH] math.factorials: adding double-factorial. --- extra/math/factorials/factorials-tests.factor | 5 ++++- extra/math/factorials/factorials.factor | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/extra/math/factorials/factorials-tests.factor b/extra/math/factorials/factorials-tests.factor index 52684a366b..e40b20b9dd 100644 --- a/extra/math/factorials/factorials-tests.factor +++ b/extra/math/factorials/factorials-tests.factor @@ -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 diff --git a/extra/math/factorials/factorials.factor b/extra/math/factorials/factorials.factor index 957a94fd9d..8760ce537f 100644 --- a/extra/math/factorials/factorials.factor +++ b/extra/math/factorials/factorials.factor @@ -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 product + ] [ drop 1 ] if ; + +ALIAS: n!! double-factorial + : factorial/ ( n k -- n!/k! ) { { [ dup 1 <= ] [ drop factorial ] }