2013-04-08 18:03:15 -04:00
|
|
|
! Copyright (C) 2013 John Benediktsson
|
|
|
|
! See http://factorcode.org/license.txt for BSD license
|
|
|
|
|
2013-04-08 18:23:39 -04:00
|
|
|
USING: combinators kernel math math.functions math.ranges
|
|
|
|
memoize sequences ;
|
2013-04-08 18:03:15 -04:00
|
|
|
|
|
|
|
IN: math.factorials
|
|
|
|
|
|
|
|
MEMO: factorial ( n -- n! )
|
|
|
|
dup 1 > [ [1,b] product ] [ drop 1 ] if ;
|
|
|
|
|
2013-04-09 00:13:46 -04:00
|
|
|
ALIAS: n! factorial
|
|
|
|
|
|
|
|
MEMO: double-factorial ( n -- n!! )
|
2013-04-09 00:44:35 -04:00
|
|
|
dup [ even? ] [ 0 < ] bi [
|
|
|
|
[ drop 1/0. ] [
|
|
|
|
2 + -1 swap -2 <range> product recip
|
|
|
|
] if
|
|
|
|
] [
|
|
|
|
2 3 ? swap 2 <range> product
|
|
|
|
] if ;
|
2013-04-09 00:13:46 -04:00
|
|
|
|
|
|
|
ALIAS: n!! double-factorial
|
|
|
|
|
2013-04-08 18:08:53 -04:00
|
|
|
: factorial/ ( n k -- n!/k! )
|
|
|
|
{
|
2013-04-08 18:15:49 -04:00
|
|
|
{ [ dup 1 <= ] [ drop factorial ] }
|
|
|
|
{ [ over 1 <= ] [ nip factorial recip ] }
|
2013-04-08 18:08:53 -04:00
|
|
|
[
|
|
|
|
2dup < [ t ] [ swap f ] if
|
|
|
|
[ (a,b] product ] dip [ recip ] when
|
|
|
|
]
|
|
|
|
} cond ;
|
2013-04-08 18:03:15 -04:00
|
|
|
|
|
|
|
: rising-factorial ( x n -- x(n) )
|
|
|
|
{
|
|
|
|
{ 1 [ ] }
|
|
|
|
{ 0 [ drop 0 ] }
|
|
|
|
[
|
|
|
|
dup 0 < [ neg [ + ] keep t ] [ f ] if
|
|
|
|
[ dupd + [a,b) product ] dip
|
|
|
|
[ recip ] when
|
|
|
|
]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
ALIAS: pochhammer rising-factorial
|
|
|
|
|
|
|
|
: falling-factorial ( x n -- (x)n )
|
|
|
|
{
|
|
|
|
{ 1 [ ] }
|
|
|
|
{ 0 [ drop 0 ] }
|
|
|
|
[
|
|
|
|
dup 0 < [ neg [ + ] keep t ] [ f ] if
|
|
|
|
[ dupd - swap (a,b] product ] dip
|
|
|
|
[ recip ] when
|
|
|
|
]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
: factorial-power ( x n h -- (x)n(h) )
|
|
|
|
{
|
|
|
|
{ 1 [ falling-factorial ] }
|
|
|
|
{ 0 [ ^ ] }
|
|
|
|
[
|
|
|
|
over 0 < [
|
|
|
|
[ [ nip + ] [ swap neg * + ] 3bi ] keep
|
|
|
|
<range> product recip
|
|
|
|
] [
|
|
|
|
neg [ [ dupd 1 - ] [ * ] bi* + ] keep
|
|
|
|
<range> product
|
|
|
|
] if
|
|
|
|
]
|
|
|
|
} case ;
|