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:

1
2
3
4
5
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

References

Implementations

  • 1
    
    # TODO: Implement in Python
    
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    function partition(A, n, p) {
        
        if (p == 0) console.log(`[${A.split('').join(', ')}]`);
        
        else {
            if (n > 1) partition(A, n - 1, p);
            if (n <= p) partition(n + A, n, p - n);
        }
    }
    
    function integerPartition(n) {
    
      // cycle through each cell of matrix
      for (let i = 1; i <= n; i++) {
        for (let j = 1; j <= n; j++) {
          if (i > j)  {
            
            // set cell to cell above it
            D[i][j] = D[i - 1][j];
            
          }
          else {
            
            // grab above cell and add it to previous cell
            const above = D[i - 1][j];
            const left = D[i][j - i];
            D[i][j] = above + left;
            
          }
        }
      }
      return D[n][n];
    }
    
    console.log(`Partitioning: ${integer}`);
    
    partition(A, integer, integer);
    const part = integerPartition(integer);
    
    console.log(part);