org.paukov.combinatorics.partition.package.html Maven / Gradle / Ivy
Show all versions of combinatoricslib Show documentation
This package contains the integer partitions generator.
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:
- 1 + 1 + 1 + 1 + 1
- 2 + 1 + 1 + 1
- 2 + 2 + 1
- 3 + 1 + 1
- 3 + 2
- 4 + 1
- 5
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 = CombinatoricsFactory.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)