NAME
Math::Counting - Combinatorial counting operations
SYNOPSIS
use Math::Counting ':long';
my( $n, $r ) = ( 42, 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);
use Math::Counting ':short';
my( $n, $r ) = ( 42, 27 );
printf "Given n=%d and r=%d:\nFact=%d\nPerm=%d\nComb=%d\n",
$n, $r, f($n), P($n, $r), C($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," arithmetic.
No functions are exported by default. Additionally, there is no :all target anymore, since I realized that no-one would ever use both short and long names together.
FUNCTIONS
factorial
$f = factorial($n);
$f = f($n);
Return the number of arrangements of n.
permutation
$p = permutation($n, $r);
$p = P($n, $r);
Return the number of arrangements of r elements drawn from a set of n elements. nPn is the same as n!.
combination
$c = combination($n, $r);
$c = C($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?
SEE ALSO
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/).
The bfac function of the Math::BigInt
module.
String::OrderedCombination for nPk list generation.
Math::Combinatorics for nPn and nCr list generation.
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.