nPk()
Syntax:
string nPk ( n , k )
Arguments:
| string | n | a positive integer of arbitrary digit length or zero; must be passed as a string
for values beyond the capacity of numeric types
| | string | k | a positive integer of arbitrary digit length or zero less than or equal to n;
must be passed as a string for values beyond the capacity of numeric types
|
Description:
This function will calculate nPk, the number of permutations of k elements that can be taken from a (typically larger) set of n
elements. Uses the math functions of the BC math library extension: arguments must be passed as
strings, for values beyond the capacity of numeric types, and the function always
returns a string, but it can handle virtually any size string.
Source:
<?php
/* quantifies permutations of size k taken from set n */
function nPk($n,$k){
/* cast args as string types */
$n = (string) $n;
$k = (string) $k;
/* calculate */
$result = factorial( $n, bcsub($n,$k) );
return $result; // string
}
/* required function: factorial */
function factorial($n,$k='1'){
/* cast args as string types */
$n = (string) $n;
$k = (string) $k;
/* multiply iteratively */
$result = '1';
$k = bcadd($k,'1');
for($xth=$k;bccomp($xth,$n)<1;$xth=bcadd($xth,'1')){
$result = bcmul($result,$xth);
}
return $result; // string
}
/*
* Copyright 2001 David Altherr;
* altherda@email.uc.edu
* www.davidaltherr.net
*
* Free for use under MIT open source public license
*/
?>
|
|
|