diff --git a/extra/math/factorials/factorials-tests.factor b/extra/math/factorials/factorials-tests.factor index 976bba91a0..8a94308e3e 100644 --- a/extra/math/factorials/factorials-tests.factor +++ b/extra/math/factorials/factorials-tests.factor @@ -60,3 +60,8 @@ IN: math.factorials { { 1 1 2 12 288 } } [ 5 iota [ super-factorial ] map ] unit-test { { 1 1 4 108 27648 } } [ 5 iota [ hyper-factorial ] map ] unit-test + +{ { 1 1 1 5 19 101 619 4421 35899 326981 } } +[ 10 iota [ alternating-factorial ] map ] unit-test + +{ { 1 1 2 9 262144 } } [ 5 iota [ exponential-factorial ] map ] unit-test diff --git a/extra/math/factorials/factorials.factor b/extra/math/factorials/factorials.factor index 7964713c78..1f03a28456 100644 --- a/extra/math/factorials/factorials.factor +++ b/extra/math/factorials/factorials.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2013 John Benediktsson ! See http://factorcode.org/license.txt for BSD license -USING: combinators kernel math math.functions math.primes +USING: combinators fry kernel math math.functions math.primes math.ranges memoize sequences ; IN: math.factorials @@ -91,3 +91,13 @@ ALIAS: pochhammer rising-factorial dup 1 > [ [1,b] [ dup ^ ] [ * ] map-reduce ] [ drop 1 ] if ; + +: alternating-factorial ( n -- m ) + dup 1 > [ + [ [1,b] ] keep even? '[ + [ factorial ] [ odd? _ = ] bi [ neg ] when + ] map-sum + ] [ drop 1 ] if ; + +: exponential-factorial ( n -- m ) + dup 1 > [ [1,b] 1 [ swap ^ ] reduce ] [ drop 1 ] if ;