A further thought. If these calculations are in anyway time-critical.

In almost every formula I've ever encountered that uses huge factorials -- usually probability calculations of one form or another -- there are usually (at least) two factorial (or factorial derived) terms that eventually are divided one by the other so bringing the silly numbers back into the realms of rationality.

So you tend to have formulae something like:

r = n! / (n - m)!

Ostensibly, the calculation goes like this:

n = 1e9 m = 4 r = ( 1 * 2 * 3 ... 999999999 * 1000000000 ) / ( 5 * 6 * 7 ... 999999999 * 1000000000 )

Which using arbitrary precision can take long time (and a large amount of memory) to calculate.

But with cursory inspection it is easy to see that the result is just 1/ ( 1* 2 * 3 * 4 ) == 1/24 == 0.041666666666666666666666666666667 .

If your application requires the calculation of large numbers of these types of formulae, and timeliness is of any consideration, it makes sense to avoid the huge numbers by deferring the actual math until you've performed some "cancelling out".

In most cases, the avoidance of arbitrary precision more than compensates for the extra effort and care required.


In reply to Re^2: Operations with Extremely Large Numbers by BrowserUk
in thread Operations with Extremely Large Numbers by jjw017

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.