[R] large factorials

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Apr 22 23:48:39 CEST 2009


if you really really need to have it done from within r, you may want to
use an external facility such as bc, the 'basic calculator' [1,2].  for
example, use the (experimental!) r-bc:

    source('http://r-bc.googlecode.com/svn/trunk/R/bc.R')

(you can also download the zipped package which will install on windows,
where you're likely not to have bc yet; see
http://code.google.com/p/r-bc/downloads/)

    # an intuitive but slow approach implemented mostly in r
    # (alternatively, you may want to have it recursive)
    factorial.r = function(n) {
       result = bc(1)
       while (n > 1) {
          result = result*n
          n = n-1 }
       result }

    # an alternative, faster approach implemented mostly in bc
    factorial.bc = function(n)
       bc(sprintf('define fact(n) { if (n < 2) return 1; return n *
fact(n-1) }; fact(%d)', n))

    library(rbenchmark)
    benchmark(replications=10, columns=c('test', 'elapsed'),
       r=factorial.r(500),
       bc=factorial.bc(500))

    #   test elapsed
    # 2   bc   0.101
    # 1    r  34.181

this gives you factorials for arbitrary input, but note that the result
is not an integer, but an object of class 'bc' backed by a *character
string*:

    result = factorial.bc(10^4)
    is(result)
    # "bc"
    nchar(result)
    # 35660

vQ


[1] http://www.gnu.org/software/bc/manual/html_mono/bc.html
[2] http://www.opengroup.org/onlinepubs/9699919799/utilities/bc.html


Murray Cooper wrote:
> You don't say what the error was, for the R factorial function,
> but it is probably irrelevant for your question.
>
> Factorials get to be big numbers rather quickly and unless you
> are using a program that does arbitrary precission arithmetic
> you will quickly exceed the precission limits, for storing a number.
> If you have Maple, do 170! and count the number of digits in the
> result. You will see what I mean.
>
> There are some tricks when working with large factorials, depending
> on what you are doing with them. I'd first try the log factorial function
> in R I think its called lfactorial. Just do a ?factorial and you'll find
> documentation. If this doesn't work, for you, repost with a clear
> description of what you're trying to do and someone may be able
> to help.
>
> Murray M Cooper, Ph.D.
> Richland Statistics
> 9800 N 24th St
> Richland, MI, USA 49083
> Mail: richstat at earthlink.net
>
> ----- Original Message ----- From: "molinar" <sky2k2 at hotmail.com>
> To: <r-help at r-project.org>
> Sent: Wednesday, April 22, 2009 3:21 PM
> Subject: [R] large factorials
>
>
>>
>> I am working on a project that requires me to do very large factorial
>> evaluations.  On R the built in factorial function and the one I created
>> both are not able to do factorials over 170.  The first gives an
>> error and
>> mine return Inf.
>>
>> Is there a way to have R do these larger calculations (the calculator in
>> accessories can do 10000 factorial and Maple can do even larger)
>> -- 
>> View this message in context:
>> http://www.nabble.com/large-factorials-tp23175816p23175816.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list