1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: combinators combinators.short-circuit kernel locals math
5 math.functions math.ranges memoize sequences ;
9 MEMO: factorial ( n -- n! )
10 dup 1 > [ [1,b] product ] [ drop 1 ] if ;
12 : factorial/ ( n k -- n!/k! )
14 { [ dup 1 < ] [ drop factorial ] }
15 { [ over 1 < ] [ nip factorial recip ] }
17 2dup < [ t ] [ swap f ] if
18 [ (a,b] product ] dip [ recip ] when
22 : rising-factorial ( x n -- x(n) )
27 dup 0 < [ neg [ + ] keep t ] [ f ] if
28 [ dupd + [a,b) product ] dip
33 ALIAS: pochhammer rising-factorial
35 : falling-factorial ( x n -- (x)n )
40 dup 0 < [ neg [ + ] keep t ] [ f ] if
41 [ dupd - swap (a,b] product ] dip
46 : factorial-power ( x n h -- (x)n(h) )
48 { 1 [ falling-factorial ] }
52 [ [ nip + ] [ swap neg * + ] 3bi ] keep
55 neg [ [ dupd 1 - ] [ * ] bi* + ] keep