All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.paukov.combinatorics.composition.package.html Maven / Gradle / Ivy

Go to download

Very simple java library to generate permutations, combinations and other combinatorial sequences.

The newest version!




This package contains the integer composition generators.

A composition of an integer n is a way of writing n as the sum of a sequence of (strictly) positive integers. This class generates the composition if a positive integer value.

A composition of an integer n is a way of writing n as the sum of a sequence of (strictly) positive integers. Two sequences that differ in the order of their terms define different compositions of their sum, while they are considered to define the same partition of that number.

The sixteen compositions of 5 are:

  1. 5
  2. 4+1
  3. 3+2
  4. 3+1+1
  5. 2+3
  6. 2+2+1
  7. 2+1+2
  8. 2+1+1+1
  9. 1+4
  10. 1+3+1
  11. 1+2+2
  12. 1+2+1+1
  13. 1+1+3
  14. 1+1+2+1
  15. 1+1+1+2
  16. 1+1+1+1+1.

Compare this with the seven partitions of 5:

  1. 5
  2. 4+1
  3. 3+2
  4. 3+1+1
  5. 2+2+1
  6. 2+1+1+1
  7. 1+1+1+1+1.

Example. Generate compositions all possible integer compositions of 5.

  // Create an instance of the integer composition generator to generate all possible compositions of 5
  Generator<Integer> gen = CombinatoricsFactory.createCompositionGenerator(5);

  // Print the compositions
  for (ICombinatoricsVector<Integer> p : gen) {
     System.out.println(p);
  }

And the result

   CombinatoricsVector=([5], size=1)
   CombinatoricsVector=([1, 4], size=2)
   CombinatoricsVector=([2, 3], size=2)
   CombinatoricsVector=([1, 1, 3], size=3)
   CombinatoricsVector=([3, 2], size=2)
   CombinatoricsVector=([1, 2, 2], size=3)
   CombinatoricsVector=([2, 1, 2], size=3)
   CombinatoricsVector=([1, 1, 1, 2], size=4)
   CombinatoricsVector=([4, 1], size=2)
   CombinatoricsVector=([1, 3, 1], size=3)
   CombinatoricsVector=([2, 2, 1], size=3)
   CombinatoricsVector=([1, 1, 2, 1], size=4)
   CombinatoricsVector=([3, 1, 1], size=3)
   CombinatoricsVector=([1, 2, 1, 1], size=4)
   CombinatoricsVector=([2, 1, 1, 1], size=4)
   CombinatoricsVector=([1, 1, 1, 1, 1], size=5)





© 2015 - 2024 Weber Informatics LLC | Privacy Policy