6.1.27 Listing all compositions of an integer into k parts
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
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).
Remark.
Using icomp with too large values of n can easily clutter your working memory
because the number of compositions rises exponentially.
Examples
|
| ⎡
⎢
⎢
⎢
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎥
⎥
⎥
⎦ |
|
| | | | | | | | | | |
|
|
| ⎡
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎣ | 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
|
| ⎤
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎦ |
|
| | | | | | | | | | |
|