+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 17

Thread: How to calculate the Factorial of Numbers > 170

  1. #1
    Points: 1,077, Level: 17
    Level completed: 77%, Points required for next Level: 23

    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to calculate the Factorial of Numbers > 170




    Hi

    I use factorial() function, but it doesn't work for numbers more than 170. I also use lfactorial() but doesn't work too.

    I put simple codes below :

    Code: 
    
    > factorial(200)
    [1] Inf
    Warning message:
    In factorial(200) : value out of range in 'gammafn'
    
    > exp(lfactorial(200))
    [1] Inf
    
    > gamma(201)
    [1] Inf
    Warning message:
    value out of range in 'gammafn'

  2. #2
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: How to calculate the Factorial of Numbers > 170

    What are you trying to do? Do you need that exact value or are you using it for further calculations? For example: if you're using binomial coefficients these have factorials in the definition but you can simplify the calculations so you don't need to compute the full factorials (otherwise it would be impossible to compute binomial probabilities for large sample sizes).

  3. #3
    R purist
    Points: 20,204, Level: 89
    Level completed: 71%, Points required for next Level: 146
    TheEcologist's Avatar
    Location
    The Netherlands.
    Posts
    1,678
    Thanks
    224
    Thanked 438 Times in 248 Posts

    Re: How to calculate the Factorial of Numbers > 170

    The factorial of x, x! is given by the gamma function +1 ; gamma (x+1).

    Therefore use ?gamma in r.

    factorial of 100;

    gamma(100+1)

    >170 is likely too large for the standard gamma function so use the log gamma function (for example the factorial of 200):

    lgamma(200+1) is 863.232, so the factorial of 200 is e^863.232. Does this help?
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  4. #4
    R purist
    Points: 20,204, Level: 89
    Level completed: 71%, Points required for next Level: 146
    TheEcologist's Avatar
    Location
    The Netherlands.
    Posts
    1,678
    Thanks
    224
    Thanked 438 Times in 248 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Otherwise you can use this (from Martin Maechler´s reply);


    library(Rmpfr)
    gamma(as(1000,"mpfr"))
    #1 'mpfr' number of precision 128 bits
    # 4.023872600770937735437024339230039857186 e 2564
    Last edited by TheEcologist; 04-27-2011 at 09:44 AM. Reason: second evaluation was a little much, removed it
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  5. #5
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: How to calculate the Factorial of Numbers > 170

    I didn't know about
    Code: 
    library(Rmpfr)
    gamma(as(1000,"mpfr"))
    I was considering writing a little bit of code to do arbitrary length integer calculations. Then I thought it had probably been done before so I just stopped and since it wasn't pressing to any of my research I just forgot about it.

    But I still say the question remains as to what the numbers are being used for because a lot of times you can reduce the calculations down a lot if lots of things are going to cancel.

  6. #6
    R purist
    Points: 20,204, Level: 89
    Level completed: 71%, Points required for next Level: 146
    TheEcologist's Avatar
    Location
    The Netherlands.
    Posts
    1,678
    Thanks
    224
    Thanked 438 Times in 248 Posts

    Re: How to calculate the Factorial of Numbers > 170

    In the case of binomial probabilities much will indeed 'cancel out' and such algorithms already exist :Catherine Loader (2000). Fast and Accurate Computation of Binomial Probabilities. And therefore have already been implemented in functions as dbinom - I just automatically assumed DataMiner needs this for something that isn't already implemented... but you are correct.. it would be way better if DataMiner "Describes the goal and not only the step".
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  7. #7
    TS Contributor
    Points: 5,161, Level: 46
    Level completed: 6%, Points required for next Level: 189

    Location
    MD, USA
    Posts
    375
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Isn't there a "Stirling's approximation" to calculate large factorials?

  8. #8
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Yes. And it's quite good for large n in a relative error sense. So depending on what you're doing it could be a decent approximation. But even using the approximation... 170! is too large for R to handle using the base numeric types it has built in so you'd either need an arbitrary length integer or you need to specify what you're doing and figure out a better way to do it than to do the full computations. So it all really depends on if the OP ever shows up again.

  9. #9
    Phineas Packard
    Points: 9,279, Level: 64
    Level completed: 77%, Points required for next Level: 71
    Lazar's Avatar
    Location
    Sydney
    Posts
    901
    Thanks
    147
    Thanked 245 Times in 222 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Code: 
    Stirling<- function (n)((2*pi*n)^.5)*(n/exp(1))^n
    I think this is the Stirling approximation.

  10. #10
    R purist
    Points: 20,204, Level: 89
    Level completed: 71%, Points required for next Level: 146
    TheEcologist's Avatar
    Location
    The Netherlands.
    Posts
    1,678
    Thanks
    224
    Thanked 438 Times in 248 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Dason, I love mpfr. It has proven very useful in the past ! (when correct rounding becomes important)

    For those of you who dont know about it:
    http://en.wikipedia.org/wiki/Arbitra...ion_arithmetic

    and http://www.mpfr.org/
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  11. #11
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Oh I didn't realize it was actually a C library. That might come in even more useful than I thought before...

  12. #12
    Super Moderator
    Points: 9,648, Level: 65
    Level completed: 99%, Points required for next Level: 2
    Dragan's Avatar
    Location
    Illinois, US
    Posts
    1,765
    Thanks
    0
    Thanked 139 Times in 125 Posts

    Re: How to calculate the Factorial of Numbers > 170

    This approximation is very accurate. For n=200 the absolute relative error is less than 2.107x10^(-18)

    n!\simeq n^{n}e^{-n}\sqrt{2\pi }\sqrt{n+\frac{1}{6}+\frac{1}{72n}-\frac{31}{6480n^{2}}-\frac{139}{155520n^{3}}+\frac{9871}{6531840n^{4}}}

  13. #13
    R purist
    Points: 20,204, Level: 89
    Level completed: 71%, Points required for next Level: 146
    TheEcologist's Avatar
    Location
    The Netherlands.
    Posts
    1,678
    Thanks
    224
    Thanked 438 Times in 248 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Quote Originally Posted by Dragan View Post
    This approximation is very accurate. For n=200 the absolute relative error is less than 2.107x10^(-18)

    n!\simeq n^{n}e^{-n}\sqrt{2\pi }\sqrt{n+\frac{1}{6}+\frac{1}{72n}-\frac{31}{6480n^{2}}-\frac{139}{155520n^{3}}+\frac{9871}{6531840n^{4}}}
    Relative is the correct word :-)
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  14. #14
    Beep
    Points: 60,853, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,029
    Thanks
    259
    Thanked 2,130 Times in 1,812 Posts

    Re: How to calculate the Factorial of Numbers > 170

    Exactly. Depending on what you're doing you might be more concerned that the absolute error gets QUITE large.

  15. #15
    TS Contributor
    Points: 5,161, Level: 46
    Level completed: 6%, Points required for next Level: 189

    Location
    MD, USA
    Posts
    375
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Re: How to calculate the Factorial of Numbers > 170


    While we're on the subject, how many significant figures should be carried with stats work? Is it different for p < .05 than for p < .001?

+ Reply to Thread
Page 1 of 2 1 2 LastLast

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats