Skip to content

Commit

Permalink
math.factorials: adding subfactorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Dec 8, 2024
1 parent 00b1cff commit dcaf7a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions extra/math/factorials/factorials-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ USING: kernel math.factorials math.functions ranges sequences tools.test ;
{ 3 } [ 6 reverse-factorial ] unit-test
{ 1 } [ 1 reverse-factorial ] unit-test
{ f } [ 18 reverse-factorial ] unit-test

{
{
1
0
1
2
9
44
265
1854
14833
133496
1334961
14684570
176214841
2290792932
32071101049
481066515734
7697064251745
130850092279664
2355301661033953
}
} [ 19 <iota> [ subfactorial ] map ] unit-test
7 changes: 6 additions & 1 deletion extra/math/factorials/factorials.factor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
! See https://factorcode.org/license.txt for BSD license

USING: combinators combinators.short-circuit inverse kernel
math math.functions math.primes ranges sequences ;
math math.constants math.functions math.primes ranges sequences ;

IN: math.factorials

Expand Down Expand Up @@ -123,3 +123,8 @@ PRIVATE>
1 1 [ 2over > ] [ 1 + [ * ] keep ] while [ = ] dip and ;

\ factorial [ reverse-factorial ] define-inverse

: subfactorial ( n -- ? )
[ 1 ] [ factorial 1 + e /i ] if-zero ;

ALIAS: !n subfactorial

0 comments on commit dcaf7a0

Please sign in to comment.