in reply to Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?

One more thing you may wish to consider is using algorithms that do not rely on large integers. For example, instead of computing ncr( $n, $r ) ("n choose r") by the formula $n! / ($n! * ($n-$r)!), use the following approach:

sub ncr { my( $n, $r ) = @_; my( $i, $result ); + + $result = 1; for $i ( 1 .. $r ) { $result *= $n - $r + $i; $result /= $i; } return $result; } + + print "ncr( 6, 3 ) = ", ncr( 6, 3), "\n"; # ncr( 6, 3 ) = 20

That is, arrange your multiplications and divisions in such a way that at any given point of time you have an integer not much larger than the output.

  • Comment on Re: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?
  • Select or Download Code