]> gitweb.factorcode.org Git - factor.git/commitdiff
math.combinatorics: factor out nCk-with-replacement
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 28 Jan 2022 17:35:29 +0000 (09:35 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 28 Jan 2022 17:35:29 +0000 (09:35 -0800)
basis/math/combinatorics/combinatorics.factor

index d4023fa82af0d44dd8cc3542dcffa79a58ca5965..be9cc1b9678f8bfd372cd0f1aee77db8552c13a4 100644 (file)
@@ -184,6 +184,9 @@ PRIVATE>
 
 <PRIVATE
 
+:: nCk-with-replacement ( n k -- nCk )
+    k 1 - n + factorial k factorial / n 1 - factorial / ; inline
+
 :: next-combination-with-replacement ( seq n -- seq )
     seq n 1 - '[ _ = not ] find-last drop :> i
     seq i tail-slice i seq nth 1 + '[ drop _ ] map! drop
@@ -191,8 +194,7 @@ PRIVATE>
 
 :: combinations-with-replacement-quot ( seq k quot -- seq quot' )
     seq length :> n
-    k 1 - n + factorial k factorial / n 1 - factorial / <iota>
-    k 0 <array> seq quot n
+    n k nCk-with-replacement <iota> k 0 <array> seq quot n
     '[ drop _ [ _ nths-unsafe @ ] keep _ next-combination-with-replacement drop ] ; inline
 
 PRIVATE>