<?
// combinatronics thing v1.0
// this function displays the number of times you select a number from a number
// for example, you can select 2 from 4 a total of 6 times like so:
// 12 23 34 13 24 14
// this caches factorials, you should remove that if you
// only plan to use it a small number of times in your scripts
function fact($number)
{
static $fact_cache;
if ($fact_cache[$number])
return $fact_cache[$number];
else
{
for ($total = 1, $n = 2; $n <= $number; $n++)
$total *= $n;
$fact_cache[$number] = $total;
return $total;
}
}
// the main function
function number_from_number($n, $r)
{
return fact($n) / (fact($r) * fact($n - $r));
}
// * example use - display all possible combinations from 1 to 8 digits * //
for ($n = 1; $n <= 8; $n++)
for ($r = 1; $r < $n; $r++)
echo '<sup>' . $n . '</sup>C<sub>' . $r . '</sub> = ' . number_from_number($n, $r) . '<br>';
?>