In
number theory and combinatorics, a partition of a positive integer n, also
called an integer partition, is a way of writing n as a sum of positive
integers.
For
example, 4 can be partitioned in five distinct ways:
4
3
+ 1
2
+ 2
2
+ 1 + 1
1
+ 1 + 1 + 1
import org.paukov.combinatorics.Factory; import org.paukov.combinatorics.Generator; import org.paukov.combinatorics.ICombinatoricsVector; public class PartitionEx { public static void main(String args[]) { // Create an instance of the partition generator to generate all // possible partitions of 4 Generator<Integer> gen = Factory.createPartitionGenerator(4); // Print the partitions for (ICombinatoricsVector<Integer> p : gen) { //System.out.println(p); System.out.println(p.getVector()); } } }
Output
[1, 1, 1, 1] [2, 1, 1] [2, 2] [3, 1] [4]
Prevoius Next Home
No comments:
Post a Comment