diff --git a/README.md b/README.md index 3e5d8cd1..67cd6055 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ * [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test) (trial division method) * [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD) * [Least Common Multiple](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/least-common-multiple) (LCM) + * [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition) * **Sets** * [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/cartesian-product) - product of multiple sets * [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of the set @@ -98,9 +99,9 @@ * [Longest Increasing subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/longest-increasing-subsequence) * [Shortest Common Supersequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/shortest-common-supersequence) * [0/1 Knapsack Problem](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/knapsack-problem) + * [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition) * Maximum subarray * Maximum sum path - * Integer Partition * **Backtracking** * **Branch & Bound** diff --git a/src/algorithms/math/integer-partition/README.md b/src/algorithms/math/integer-partition/README.md new file mode 100644 index 00000000..134512d3 --- /dev/null +++ b/src/algorithms/math/integer-partition/README.md @@ -0,0 +1,33 @@ +# Integer Partition + +In number theory and combinatorics, a partition of a positive +integer `n`, also called an **integer partition**, is a way of +writing `n` as a sum of positive integers. + +Two sums that differ only in the order of their summands are +considered the same partition. For example, `4` can be partitioned +in five distinct ways: + +``` +4 +3 + 1 +2 + 2 +2 + 1 + 1 +1 + 1 + 1 + 1 +``` + +The order-dependent composition `1 + 3` is the same partition +as `3 + 1`, while the two distinct +compositions `1 + 2 + 1` and `1 + 1 + 2` represent the same +partition `2 + 1 + 1`. + +Young diagrams associated to the partitions of the positive +integers `1` through `8`. They are arranged so that images +under the reflection about the main diagonal of the square +are conjugate partitions. + +![Integer Partition](https://upload.wikimedia.org/wikipedia/commons/d/d8/Ferrer_partitioning_diagrams.svg) + +## References + +- [Wikipedia](https://en.wikipedia.org/wiki/Partition_(number_theory)) diff --git a/src/algorithms/math/integer-partition/__test__/integerPartition.test.js b/src/algorithms/math/integer-partition/__test__/integerPartition.test.js new file mode 100644 index 00000000..affcbca6 --- /dev/null +++ b/src/algorithms/math/integer-partition/__test__/integerPartition.test.js @@ -0,0 +1,11 @@ +import integerPartition from '../integerPartition'; + +describe('integerPartition', () => { + it('should partition the number', () => { + expect(integerPartition(1)).toBe(1); + expect(integerPartition(2)).toBe(2); + expect(integerPartition(3)).toBe(3); + expect(integerPartition(4)).toBe(5); + expect(integerPartition(8)).toBe(22); + }); +}); diff --git a/src/algorithms/math/integer-partition/integerPartition.js b/src/algorithms/math/integer-partition/integerPartition.js new file mode 100644 index 00000000..b77cc0a3 --- /dev/null +++ b/src/algorithms/math/integer-partition/integerPartition.js @@ -0,0 +1,47 @@ +/** + * @param {Number} number + */ +export default function integerPartition(number) { + // Create partition matrix for solving this task using Dynamic Programming. + const partitionMatrix = Array(number + 1).fill(null).map(() => { + return Array(number + 1).fill(null); + }); + + // Fill partition matrix with initial values. + + // Let's fill the first row that represents how many ways we would have + // to combine the numbers 1, 2, 3, ..., n with number 0. We would have zero + // ways obviously since with zero number we may form only zero. + for (let numberIndex = 1; numberIndex <= number; numberIndex += 1) { + partitionMatrix[0][numberIndex] = 0; + } + + // Let's fill the first row. It represents the number of way of how we can form + // number zero out of numbers 0, 1, 2, ... Obviously there is only one way we could + // form number 0 and it is with number 0 itself. + for (let summandIndex = 0; summandIndex <= number; summandIndex += 1) { + partitionMatrix[summandIndex][0] = 1; + } + + // Now let's go through other possible options of how we could form number m out of + // summands 0, 1, ..., m using Dynamic Programming approach. + for (let summandIndex = 1; summandIndex <= number; summandIndex += 1) { + for (let numberIndex = 1; numberIndex <= number; numberIndex += 1) { + if (summandIndex > numberIndex) { + // If summand number is bigger then current number itself then just it won't add + // any new ways of forming the number. Thus we may just copy the number from row above. + partitionMatrix[summandIndex][numberIndex] = partitionMatrix[summandIndex - 1][numberIndex]; + } else { + // The number of combinations would equal to number of combinations of forming the same + // number but WITHOUT current summand number plus number of combinations of forming the + // previous number but WITH current summand. + const combosWithoutSummand = partitionMatrix[summandIndex - 1][numberIndex]; + const combosWithSummand = partitionMatrix[summandIndex][numberIndex - summandIndex]; + + partitionMatrix[summandIndex][numberIndex] = combosWithoutSummand + combosWithSummand; + } + } + } + + return partitionMatrix[number][number]; +}