How to make combinations of a set of numbers?
This code uses the Math::Combinatorics module (https://metacpan.org/pod/Math::Combinatorics) to generate combinations of size $k from the array @set.
Output:use Math::Combinatorics; @set = (1, 2, 3, 4); $k = 2; $combinat = Math::Combinatorics->new(data => [@set], count => $k); while (@combo = $combinat->next_combination) { print "@combo\n"; }
How to make permutations of a set of numbers?1 2 1 3 1 4 2 3 2 4 3 4
Output:use Algorithm::Permute; my @items = (1, 2, 3); my $permutor = Algorithm::Permute-> new(\@items); while (@perm = $permutor-> next) { print join(", ", @perm), "\n"; }
You can alo create r of n objects permutation generator, where r <= n3, 2, 1 2, 3, 1 2, 1, 3 3, 1, 2 1, 3, 2 1, 2, 3
Output:use Algorithm::Permute; $permutor = Algorithm::Permute->new([1..4], 3); while (@perm = $permutor-> next) { print join(", ", @perm), " | "; }
3, 2, 1 | 2, 3, 1 | 2, 1, 3 | 3, 1, 2 | 1, 3, 2 | 1, 2, 3 | 4, 3, 2 | 3, 4, 2 | 3, 2, 4 | 4, 2, 3 | 2, 4, 3 | 2, 3, 4 | 4, 3, 1 | 3, 4, 1 | 3, 1, 4 | 4, 1, 3 | 1, 4, 3 | 1, 3, 4 | 4, 2, 1 | 2, 4, 1 | 2, 1, 4 | 4, 1, 2 | 1, 4, 2 | 1, 2, 4 |