2
Math::Combinatorics - Perform combinations and permutations on lists
5
Available as an object oriented API.
7
use Math::Combinatorics;
10
my $combinat = Math::Combinatorics->new(count => 2,
14
print "combinations of 2 from: ".join(" ",@n)."\n";
15
print "------------------------".("--" x scalar(@n))."\n";
16
while(my @combo = $combinat->next_combination){
17
print join(' ', @combo)."\n";
22
print "combinations of 2 from: ".join(" ",@n)."\n";
23
print "------------------------".("--" x scalar(@n))."\n";
24
while(my @permu = $combinat->next_permutation){
25
print join(' ', @permu)."\n";
30
Or available via exported functions 'permute', 'combine', and
33
use Math::Combinatorics;
36
print "combinations of 2 from: ".join(" ",@n)."\n";
37
print "------------------------".("--" x scalar(@n))."\n";
38
print join("\n", map { join " ", @$_ } combine(2,@n)),"\n";
40
print "permutations of 3 from: ".join(" ",@n)."\n";
41
print "------------------------".("--" x scalar(@n))."\n";
42
print join("\n", map { join " ", @$_ } permute(@n)),"\n";
46
combinations of 2 from: a b c
47
------------------------------
52
combinations of 2 from: a b c
53
------------------------------
61
Output from both types of calls is the same, but the object-oriented
62
approach consumes much less memory for large sets.
65
Combinatorics is the branch of mathematics studying the enumeration,
66
combination, and permutation of sets of elements and the mathematical
67
relations that characterize their properties. As a jumping off point,
70
http://mathworld.wolfram.com/Combinatorics.html
72
This module provides a pure-perl implementation of nCk, nPk, and n!
73
(combination, permutation, and factorial, respectively). Functional and
74
object-oriented usages allow problems such as the following to be
77
nCk "Fun questions to ask the pizza parlor wait staff: how many possible
78
combinations of 2 toppings can I get on my pizza?".
80
nPk "Master Mind Game: ways to arrange pieces of different colors in a
81
certain number of positions, without repetition of a color".
83
Object-oriented usage additionally allows solving these problems by
84
calling the new() entry elsewhere in this document with a frequency
87
nPRk "morse signals: diferent signals of 3 positions using the 2 two
90
nCRk "ways to extract 3 balls at once of a bag with black and white
93
nPRk "different words obtained permuting the letters of the word
97
Allen Day <allenday@ucla.edu>, with algorithmic contributions from
98
Christopher Eltschka and Tye.
101
Thanks to everyone for helping to make this a better module.
103
For adding new features: Carlos Rica, David Coppit
105
For bug reports: Ying Yang, Joerg Beyer, Marc Logghe
107
LICENSE AND COPYRIGHT
109
Copyright (c) 2004 Allen Day. All rights reserved. This program is
110
free software; you can redistribute it and/or modify it under the same
111
terms as Perl itself.