Skip to content

Commit 542f2ad

Browse files
committed
Add integer partition.
1 parent 8895d20 commit 542f2ad

4 files changed

Lines changed: 93 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test) (trial division method)
3232
* [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
3333
* [Least Common Multiple](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/least-common-multiple) (LCM)
34+
* [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition)
3435
* **Sets**
3536
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/cartesian-product) - product of multiple sets
3637
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of the set
@@ -98,9 +99,9 @@
9899
* [Longest Increasing subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/longest-increasing-subsequence)
99100
* [Shortest Common Supersequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/shortest-common-supersequence)
100101
* [0/1 Knapsack Problem](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/knapsack-problem)
102+
* [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition)
101103
* Maximum subarray
102104
* Maximum sum path
103-
* Integer Partition
104105
* **Backtracking**
105106
* **Branch & Bound**
106107

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Integer Partition
2+
3+
In number theory and combinatorics, a partition of a positive
4+
integer `n`, also called an **integer partition**, is a way of
5+
writing `n` as a sum of positive integers.
6+
7+
Two sums that differ only in the order of their summands are
8+
considered the same partition. For example, `4` can be partitioned
9+
in five distinct ways:
10+
11+
```
12+
4
13+
3 + 1
14+
2 + 2
15+
2 + 1 + 1
16+
1 + 1 + 1 + 1
17+
```
18+
19+
The order-dependent composition `1 + 3` is the same partition
20+
as `3 + 1`, while the two distinct
21+
compositions `1 + 2 + 1` and `1 + 1 + 2` represent the same
22+
partition `2 + 1 + 1`.
23+
24+
Young diagrams associated to the partitions of the positive
25+
integers `1` through `8`. They are arranged so that images
26+
under the reflection about the main diagonal of the square
27+
are conjugate partitions.
28+
29+
![Integer Partition](https://upload.wikimedia.org/wikipedia/commons/d/d8/Ferrer_partitioning_diagrams.svg)
30+
31+
## References
32+
33+
- [Wikipedia](https://en.wikipedia.org/wiki/Partition_(number_theory))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import integerPartition from '../integerPartition';
2+
3+
describe('integerPartition', () => {
4+
it('should partition the number', () => {
5+
expect(integerPartition(1)).toBe(1);
6+
expect(integerPartition(2)).toBe(2);
7+
expect(integerPartition(3)).toBe(3);
8+
expect(integerPartition(4)).toBe(5);
9+
expect(integerPartition(8)).toBe(22);
10+
});
11+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {Number} number
3+
*/
4+
export default function integerPartition(number) {
5+
// Create partition matrix for solving this task using Dynamic Programming.
6+
const partitionMatrix = Array(number + 1).fill(null).map(() => {
7+
return Array(number + 1).fill(null);
8+
});
9+
10+
// Fill partition matrix with initial values.
11+
12+
// Let's fill the first row that represents how many ways we would have
13+
// to combine the numbers 1, 2, 3, ..., n with number 0. We would have zero
14+
// ways obviously since with zero number we may form only zero.
15+
for (let numberIndex = 1; numberIndex <= number; numberIndex += 1) {
16+
partitionMatrix[0][numberIndex] = 0;
17+
}
18+
19+
// Let's fill the first row. It represents the number of way of how we can form
20+
// number zero out of numbers 0, 1, 2, ... Obviously there is only one way we could
21+
// form number 0 and it is with number 0 itself.
22+
for (let summandIndex = 0; summandIndex <= number; summandIndex += 1) {
23+
partitionMatrix[summandIndex][0] = 1;
24+
}
25+
26+
// Now let's go through other possible options of how we could form number m out of
27+
// summands 0, 1, ..., m using Dynamic Programming approach.
28+
for (let summandIndex = 1; summandIndex <= number; summandIndex += 1) {
29+
for (let numberIndex = 1; numberIndex <= number; numberIndex += 1) {
30+
if (summandIndex > numberIndex) {
31+
// If summand number is bigger then current number itself then just it won't add
32+
// any new ways of forming the number. Thus we may just copy the number from row above.
33+
partitionMatrix[summandIndex][numberIndex] = partitionMatrix[summandIndex - 1][numberIndex];
34+
} else {
35+
// The number of combinations would equal to number of combinations of forming the same
36+
// number but WITHOUT current summand number plus number of combinations of forming the
37+
// previous number but WITH current summand.
38+
const combosWithoutSummand = partitionMatrix[summandIndex - 1][numberIndex];
39+
const combosWithSummand = partitionMatrix[summandIndex][numberIndex - summandIndex];
40+
41+
partitionMatrix[summandIndex][numberIndex] = combosWithoutSummand + combosWithSummand;
42+
}
43+
}
44+
}
45+
46+
return partitionMatrix[number][number];
47+
}

0 commit comments

Comments
 (0)