Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Fastest way to calculate hypergeometric distribution probabilities (i.e. BIG factorials)?

by kaif (Friar)
on Jun 14, 2005 at 01:34 UTC ( #466341=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://466341]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2021-05-13 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (137 votes). Check out past polls.

    Notices?