Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Generate all compositions of an integer into the specified number of parts
ResourceFunction["IntegerCompositions"][n,k] gives a list of all compositions of integer n into k parts in canonical order. |
Get every composition of 5 into three parts:
In[1]:= | ![]() |
Out[1]= | ![]() |
The number of compositions of n into k parts is Binomial[n+k-1,n]:
In[2]:= | ![]() |
In[3]:= | ![]() |
Out[3]= | ![]() |
In[4]:= | ![]() |
Out[4]= | ![]() |
In[5]:= | ![]() |
Out[5]= | ![]() |
All compositions returned by IntegerCompositions are distinct:
In[6]:= | ![]() |
Out[6]= | ![]() |
In[7]:= | ![]() |
Out[7]= | ![]() |
Compositions are returned in Sort order:
In[8]:= | ![]() |
Out[8]= | ![]() |
In[9]:= | ![]() |
Out[9]= | ![]() |
Integer compositions are related to integer partitions:
In[10]:= | ![]() |
Out[10]= | ![]() |
In contrast to compositions, partitions do not include 0 and are in non-increasing order:
In[11]:= | ![]() |
Out[11]= | ![]() |
If you allow partitions to include 0, then all permutations thereof give the possible compositions:
In[12]:= | ![]() |
Out[12]= | ![]() |
In[13]:= | ![]() |
Out[13]= | ![]() |
In[14]:= | ![]() |
Out[14]= | ![]() |
While IntegerCompositions allows resulting compositions to include 0, the resource function StrictIntegerCompositions does not:
In[15]:= | ![]() |
Out[15]= | ![]() |
In[16]:= | ![]() |
Out[16]= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License