# partitions -- list the partitions of an integer

## Synopsis

• Usage:
partitions(n)
partitions(n, k)
• Inputs:
• n, an integer, a nonnegative integer
• k, an integer, a nonnegative integer, the maximum size of each part
• Outputs:
• a list, of all partitions of n

## Description

 i1 : partitions(4) o1 = {Partition{4}, Partition{3, 1}, Partition{2, 2}, Partition{2, 1, 1}, ------------------------------------------------------------------------ Partition{1, 1, 1, 1}} o1 : List i2 : partitions(4, 2) o2 = {Partition{2, 2}, Partition{2, 1, 1}, Partition{1, 1, 1, 1}} o2 : List

Each partition is a basic list of type Partition.

 i3 : p = new Partition from {2,2,1} o3 = Partition{2, 2, 1} o3 : Partition i4 : member(p, partitions(5,2)) o4 = true i5 : member(p, partitions(5,1)) o5 = false i6 : conjugate(p) o6 = Partition{3, 2} o6 : Partition

For ordered lists of exactly k nonnegative integers that sum to n, use compositions instead. In the following example, we create Partition objects from the output of compositions to find all the partitions of 10, of length exactly 4, with no part greater than 5.

 i7 : A = unique apply(compositions(4, 10), comp -> rsort comp); i8 : B = select(A, a -> all(a, i -> 0 new Partition from b) o9 = {Partition{5, 3, 1, 1}, Partition{4, 4, 1, 1}, Partition{5, 2, 2, 1}, ------------------------------------------------------------------------ Partition{4, 3, 2, 1}, Partition{3, 3, 3, 1}, Partition{4, 2, 2, 2}, ------------------------------------------------------------------------ Partition{3, 3, 2, 2}} o9 : List

If partitions n is called on a negative integer n, an empty list is returned. If a negative integer is given for k, it will cause an error.