Previous Up Next

5.6.28  Listing all compositions of an integer into k parts : icomp

icomp accepts two or three arguments : a positive integer n, a positive integer k not larger than n and optionally zeros=true or zeros=false. The return value is the list of all compostions of n into k parts. Each composition is a list of nonnegative integers which sum up to n. If the option zeros is set to true (which is the default), a part can have zero value. Else, each part has nonzero (positive) value.

For example, 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]]

Previous Up Next