NAME
Math::Counting - Combinatorial counting operations
SYNOPSIS
use Math::Counting qw( factorial permutation combination );
my $n = 42;
my $r = 27;
printf "Given n=%d and r=%d:\nFact=%d\nPerm=%d\nComb=%d\n",
$n, $r, factorial($n), permutation($n, $r), combination($n, $r);
DESCRIPTION
Compute the numerical factorial, number of permutations and number of combinations using the technique of "tail call elimination" as detailed in Higher Order Perl and based on the algorithms in Mastering Algorithms with Perl.
Note that this code uses floating point, as opposed to "infinite precision," arithmatic.
FUNCTIONS
factorial
$f = factorial($n);
Return the number of arrangements of n.
permutation
$p = permutation($n, $k);
Return the number of arrangements of k elements drawn from a set of n elements. nPn is the same as n!
The term "permutation" has a wide variety of definitions, like this gem: "The number of permutations of a set of n elements is denoted n! and pronounced 'n factorial.'" However, here is a possibly enlightening quote from a reference below:
In combinatorics, the term permutation has a traditional meaning,
which is used to include ordered lists without repetition, but not
exhaustive (so of less than maximum length)...
This package uses the combinatorial sense of the term.
combination
$c = combination($n, $r);
Return the number of ways to choose r elements from a set of n elements.
TO DO
Provide the gamma function for the factorial of non-integer numbers?
Provide a way to use infinite precision arithmetic.
SEE ALSO
The bfac
function of the Math::BigInt
module.
Higher Order Perl by Mark Jason Dominus (http://hop.perl.plover.com/).
Mastering Algorithms with Perl by Orwant, Hietaniemi & Macdonald (http://www.oreilly.com/catalog/maperl/).
And http://encyclopedia.laborlawtalk.com/Permutation actually.
AUTHOR
Not me. I am but a pebble on the beach.
COPYRIGHT AND LICENSE
Copyright 2005 Gene Boggs All Rights Reserved
You may use this package under the same terms as Perl itself.