http://www.perlmonks.org?node_id=467124


in reply to Re^3: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?
in thread Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?

Nice, but neither @log nor $next are necessary, and the whole thing can be given a functional interface, while transparently memoizing the results:

{ my @ln_fact; BEGIN { $ln_fact[ 0 ] = 0; } sub ln_fact { # returns the natural log of the factorial my $n = shift; die "Invalid arg: $n" if $n < 0 or $n > int $n; $ln_fact[$_] = $ln_fact[$_-1] + log($_) for @ln_fact..$n; return $ln_fact[ $n ]; } }

the lowliest monk

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