Function Repository Resource:

# IntegerCompositions

Generate all compositions of an integer into the specified number of parts

Contributed by: Wolfram Staff (original content by Sriram V. Pemmaraju and Steven S. Skiena)
 ResourceFunction["IntegerCompositions"][n,k] gives a list of all compositions of integer n into k parts in canonical order.

## Details and Options

A composition of n in ResourceFunction["IntegerCompositions"] is taken to be a particular arrangement of non-negative integers whose sum is n.

## Examples

### Basic Examples (1)

Get every composition of 5 into three parts:

 In[1]:=
 Out[1]=

### Properties and Relations (6)

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]=

## Version History

• 1.0.0 – 06 March 2020