Location : Home > Languages > Perl > Package
Title : Math::Counting
Toolbox Logo

名称

 Math::Counting - 階乗・順列の数・組み合わせの数を計算


概要

use Math::Counting qw( factorial permutation combination );
# 次のように書いてもよい。
#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);

説明

 Higher Order Perl で詳述され Mastering Algorithms with Perl にあるアルゴリズムに基づく末尾呼び出し除去("tail call elimination")法を用いた階乗・順列の数・組み合わせの数を計算する。

 このコードは浮動小数点を利用しているので無限の精度をもつ計算とは異なることに注意すること。


関数

factorial

$f = factorial($n);
$f = f($n);

 n の階乗の値を返す。

permutation

$p = permutation($n, $r);
$p = P($n, $r);

 n 個の要素から r 個の要素を抽出する順列の数を返す。nPn は n! と同じである。

combination

$c = combination($n, $r);
$c = C($n, $r);

 n 個の要素から r 個の要素を抽出する組み合わせの数を返す。


To Do

 非整数の階乗であるガンマ関数を提供する?


参考資料


著者

 Not me. I am but a pebble on the beach.


著作権とライセンス

 Copyright 2005 Gene Boggs All Rights Reserved.
 Perl 本体と同等の条件で本パッケージを利用してよい。

Toolbox Logo
Updated : 2006/07/26