Listing all compositions of an integer into k parts: icomp

### 6.5.27 Listing all compositions of an integer into k parts: icomp

A composition of a positive integer n is an ordered set of non-negative integers which sum to n. For example, three compositions of 4 are

4 | = 1 + 3 |  |  |  |  |  |  |  |  |
4 | = 3 + 1 |  |  |  |  |  |  |  |  |
4 | = 1 + 1 + 2 |  |  |  |  |  |  |  |  |

These compositions have two, two and three elements, respectively. The icomp command finds all compositions of an integer with a given number of elements.

- icomp accepts two mandatory arguments and one optional argument:
  - n, a positive integer.
  - k, a positive integer not larger than n.
  - Optionally, either zeros=true or zeros=false.
- icomp(n,k ⟨,zeros=bool⟩) returns the list of all compositions of n into k parts, where a part can be 0. This is equivalent to the optional argument with bool equal to true. With bool equal to false, icomp(n,k,zeros=false) returns the list of all compositions of n into k parts, where each part is nonzero (positive).

Examples.

- Input:
  icomp(4,2)
  Output:

  | ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |  |
  4 | 0
  3 | 1
  2 | 2
  1 | 3
  0 | 4
  |  | ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

- Input:
  icomp(6,3,zeros=false)
  Output:

  | ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |  |
  4 | 1 | 1
  3 | 2 | 1
  2 | 3 | 1
  1 | 4 | 1
  3 | 1 | 2
  2 | 2 | 2
  1 | 3 | 2
  2 | 1 | 3
  1 | 2 | 3
  1 | 1 | 4
  |  | ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦