Previous Up Next

7.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

     
  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.

Remark.

Using icomp with too large values of n can easily clutter your working memory because the number of compositions rises exponentially.

Examples

icomp(4,2)
     






    40
31
22
13
04






          
icomp(5,3,zeros=false)
     







311
221
131
212
122
113







          

Previous Up Next