math.combinatorics: faster version of factorial.

db4
John Benediktsson 2012-04-24 10:03:45 -07:00
parent cd9394f8c6
commit f24e40800a
2 changed files with 2 additions and 1 deletions

View File

@ -1,6 +1,7 @@
USING: kernel math math.combinatorics math.combinatorics.private tools.test sequences ; USING: kernel math math.combinatorics math.combinatorics.private tools.test sequences ;
IN: math.combinatorics.tests IN: math.combinatorics.tests
[ 1 ] [ -1 factorial ] unit-test ! required by other math.combinatorics words
[ 1 ] [ 0 factorial ] unit-test [ 1 ] [ 0 factorial ] unit-test
[ 1 ] [ 1 factorial ] unit-test [ 1 ] [ 1 factorial ] unit-test
[ 3628800 ] [ 10 factorial ] unit-test [ 3628800 ] [ 10 factorial ] unit-test

View File

@ -18,7 +18,7 @@ IN: math.combinatorics
PRIVATE> PRIVATE>
: factorial ( n -- n! ) : factorial ( n -- n! )
iota 1 [ 1 + * ] reduce ; dup 1 > [ [1,b] product ] [ drop 1 ] if ;
: nPk ( n k -- nPk ) : nPk ( n k -- nPk )
2dup possible? [ dupd - [a,b) product ] [ 2drop 0 ] if ; 2dup possible? [ dupd - [a,b) product ] [ 2drop 0 ] if ;