NAME
Math::Counting - Combinatorial counting operations
SYNOPSIS
## The algorithm-student versions:
use Math::Counting ':long';
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';
printf "Given n=%d and r=%d:\nF=%d\nP=%d\nC=%d\n",
$n, $r, f($n), P($n, $r), C($n, $r);
## The "Right way to do it":
use Math::Counting ':big';
printf "n=%d, r=%d:\nBig F=%d\n Big P=%d\nBig C=%d\n",
$n, $r, bfact($n), bperm($n, $r), bcomb($n, $r);
DESCRIPTION
This package computes the factorial, number of (n,r) permutations and (n,r) combinations using either Math::BigInt and the technique of "tail call elimination" (TCE).
No functions are exported by default.
FUNCTIONS
f, factorial
$f = f($n);
$f = factorial($n);
Return the number of arrangements of n with TCE.
bfact
$f = bfact($n);
Return the value of the "bfact" in Math::BigInt function, which is the "Right Way To Do It."
P, permutation
$p = P($n, $r);
$p = permutation($n, $r);
Return the number of arrangements of r elements drawn from a set of n elements with TCE. (nPn is the same as n!.)
bperm
$p = bperm($n, $r);
Return the Math::BigInt
computation: n!/(n-r)!.
C, choose, combination
$c = C($n, $r);
$c = choose($n, $r);
$c = combination($n, $r);
Return the number of ways to choose r elements from a set of n elements with TCE.
bcomb
$c = bcomb($n, $r);
Return the Math::BigInt
computation: n!/r!(n-r)!.
TO DO
Figure out how to allow the use of different BigInt
variations, like GMP
.
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.
http://en.wikipedia.org/wiki/Factorial
http://en.wikipedia.org/wiki/Permutation
http://en.wikipedia.org/wiki/Combination
AUTHOR
Not me. I am but a pebble on the beach...
COPYRIGHT
Copyright 2007 Gene Boggs All Rights Reserved
You may use this package under the same terms as Perl itself.