@@ -34,20 +34,17 @@ export default function integerPartition(number) {
3434 // any new ways of forming the number. Thus we may just copy the number from row above.
3535 partitionMatrix [ summandIndex ] [ numberIndex ] = partitionMatrix [ summandIndex - 1 ] [ numberIndex ] ;
3636 } else {
37- // The number of combinations would equal to number of combinations of forming the same
38- // number but WITHOUT current summand number plus number of combinations of forming the
39- // <current number - current summand> number but WITH current summand.
40- // Example: number of ways to form number 4 using summands 1, 2 and 3 is the sum of
41- // {number of ways to form 4 with sums that begin with 1 +
42- // number of ways to form 4 with sums that begin with 2 and include 1 } +
43- // {number of ways to form 4 with sums that begin with 3 and include 2 and 1}
44- // Taking these sums to proceed in descending order of intergers, this gives us:
45- // With 1: 1+1+1+1 -> 1 way
46- // With 2: 2+2, 2+1+1 -> 2 ways
47- // With 3: 3 + (4-3) <= convince yourself that number of ways to form 4 starting
48- // with 3 is == number of ways to form 4-3 where 4-3 == <current number-current summand>
49- // Helper: if there are n ways to get (4-3) then 4 can be represented as 3 + first way,
50- // 3 + second way, and so on until the 3 + nth way. So answer for 4 is: 1 + 2 + 1 = 4 ways
37+ /*
38+ * The number of combinations would equal to number of combinations of forming the same
39+ * number but WITHOUT current summand number PLUS number of combinations of forming the
40+ * <current number - current summand> number but WITH current summand.
41+ *
42+ * Example:
43+ * Number of ways to form 5 using summands {0, 1, 2} would equal the SUM of:
44+ * - number of ways to form 5 using summands {0, 1} (we've excluded summand 2)
45+ * - number of ways to form 3 (because 5 - 2 = 3) using summands {0, 1, 2}
46+ * (we've included summand 2)
47+ */
5148 const combosWithoutSummand = partitionMatrix [ summandIndex - 1 ] [ numberIndex ] ;
5249 const combosWithSummand = partitionMatrix [ summandIndex ] [ numberIndex - summandIndex ] ;
5350
0 commit comments