NAME

Math::Counting - Combinatorial counting operations

SYNOPSIS

use Math::Counting qw( factorial permutation combination );
# Also:
#use Math::Counting qw( f P C );
#use Math::Counting qw( :all );

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," arithmetic.

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.

Algorithm::Combinatorics

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.