function GenerateGroup($ingroup, $boys, $girls) { $minboys = 2; $mingirls = 1; $output = 0; if ($boys < 4 || $girls < 1) { $output = 0; } else { $pos_combinations = BoysGirlsPossibleCombinations($ingroup, $minboys, $mingirls, $boys, $girls); PrintArray($pos_combinations); $sum = 0; foreach ($pos_combinations as $combination) { $set_combi = nCr($boys, $combination['boy']) * nCr($girls, $combination['girl']); $sum += $set_combi; //echo '$sum: '.$sum; //echo "nCr($boys,".$combination['boy'].")*nCr($girls,".$combination['girl'].")<br/>"; } $output = $sum; } return $output; }
function nCr($n, $r) { if ($r > $n) { return NaN; } if ($n - $r < $r) { return nCr($n, $n - $r); } $return = 1; for ($i = 0; $i < $r; $i++) { $return *= ($n - $i) / ($i + 1); } return $return; }
function binomialpdf($N, $p, $x) { return nCr($N, $x) * pow($p, $x) * pow(1 - $p, $N - $x); }