Home | Permutation Generators | Combination Generators | Set/subset Generators | Cartesian Product Generators
JNumberTools provides following 15 combination generation APIs. All mth generators are BigInteger compatible. That is, it can generate combinations at very large index rapidly. For example combination at index 10100
Currently Available Algorithms
-
Multiset Combinations
- Multiset combination in lex order
- Every mth multiset combination
- Multiset combination random choice
- Multiset combination random sample
- Multiset combination from sequence
-
Repetitive Combinations
- Repetitive combination in lex order
- Every Mth repetitive combination
- Repetitive combination random choice
- Repetitive combination random sample
- Repetitive combination from sequence
Selection of r distinct items out of n elements. In mathematics, this is also known as n-Choose-r. Generates all combinations in lex order.
//all possible combination of 3 numbers in range [0,5) in lex order
JNumberTools.combinations()
.unique(5,3)
.lexOrder().stream().toList();
JNumberTools.combinations()
.unique(3,"A","B","C","D","E")
.lexOrder().stream().toList();
Same as n-Choose-r but generates every mth combination in lex order starting from given index. This concept is important because the count of combinations can grow astronomically large. For example, to generate, say, the next 1 billionth combination of 34-Choose-17, we need to wait for days to generate the desired billionth combination if we generate all combinations sequentially and then select the billionth combination.
//5th, 7th , 9th.. combination of 3 numbers in range [0,5) in lex order
JNumberTools.combinations()
.unique(5,3)
.lexOrderMth(5,2).stream().toList();
JNumberTools.combinations()
.unique(3,"A","B","C","D","E")
.lexOrderMth(5,2).stream().toList();
generates 'n' random combinations where duplicates are allowed
//generate any 5 random combinations of 10 items [0,10) from 20 items [0,20) with possibility of duplicates
JNumberTools.combinations()
.unique(20,10)
.choice(5) //5 random combinations
.stream().forEach(System.out::println);
//generate any 5 random combinations of 5 items from ["A","B","C","D","E","F","G"] with possibility of duplicates
JNumberTools.combinations()
.unique(5,"A","B","C","D","E","F","G")
.choice(5)
.stream().forEach(System.out::println);
generates 'n' random combinations without any duplicates
//generate any 5 random combinations of 10 items [0,10) from 20 items [0,20) with no duplicates
JNumberTools.combinations()
.unique(20,10)
.sample(5) //5 random combinations
.stream().forEach(System.out::println);
//generate any 5 random combinations of 5 items from ["A","B","C","D","E","F","G"] with no duplicates
JNumberTools.combinations()
.unique(5,"A","B","C","D","E","F","G")
.sample(5)
.stream().forEach(System.out::println);
Generates all combinations at index specified by custom sequence
//generates all mth combinations specified by Iterable<Number>
var iterable = List.of(10,20, 1_000_000_000L, new BigInteger("1000000000000000000000"));
JNumberTools.combinations()
.unique(200,100)
.fromSequence(iterable) //generates 10-th, 20-th, billion-th and sextillion-th permutation
.stream().toList();
Home | Permutation Generators | Combination Generators | Set/subset Generators | Cartesian Product Generators