In number theory, a partition of a positive integer n is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered to be the same partition; if order matters then the sum becomes a composition. A summand in a partition is also called a part.
WARNING! Be careful because number of all partitions can be very high even for not great given N.
The partitions of 5 are listed below:
The number of partitions of n is given by the partition function p(n). In number theory, the partition function p(n) represents the number of possible partitions of a natural number n, which is to say the number of distinct (and order independent) ways of representing n as a sum of natural numbers.
Let's generate all possible partitions of 5:
// Create an instance of the partition generator to generate all // possible partitions of 5 Generator<Integer> gen = Factory.createPartitionGenerator(5); // Print the partitions for (ICombinatoricsVector<Integer> p : gen) { System.out.println(p); }
And the result of all 7 integer possible partitions
CombinatoricsVector=([1, 1, 1, 1, 1], size=5) CombinatoricsVector=([2, 1, 1, 1], size=4) CombinatoricsVector=([2, 2, 1], size=3) CombinatoricsVector=([3, 1, 1], size=3) CombinatoricsVector=([3, 2], size=2) CombinatoricsVector=([4, 1], size=2) CombinatoricsVector=([5], size=1)