Location : Home > Languages > Perl > Package Title : Math::Counting |
![]() |
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 個の要素を抽出する組み合わせの数を返す。
非整数の階乗であるガンマ関数を提供する?
Not me. I am but a pebble on the beach.
Copyright 2005 Gene Boggs All Rights Reserved.
Perl 本体と同等の条件で本パッケージを利用してよい。
![]() |
Updated : 2006/07/26 |