phpbcmathmethod-combination

Large number multiplication and division


I am trying the below mentioned code(PHP) to find probability. The calculation includes combination calculation of large numbers, using BCmaths function but not getting results. Please suggest, how this can be done.

 function combin($n, $r)
{
  $C = 1;

  for ($i=0;   $i < $n-$r;   $i++)
      {
       $C = bcdiv(bcmul($C, $n-$i), $i+1);
      }
  return $C;
}

$dv = (combin(68, 17))*(combin((7866-68),(177-17)))/combin(7866, 177);
echo $dv;
?>```

Solution

  • Once you start using the bc* functions, you should continue to use them throughout the code. You however, are taking the results of these functions and then using the standard PHP operators on them. You should be able to change your calling code to:

    $dv = bcdiv(combin(7866, 177), bcmul(combin(68, 17), (combin(7866 - 68, 177 - 17))));