Last data update: 2014.03.03

R: Factorial 'n!' in Arbitrary Precision
factorialMpfrR Documentation

Factorial 'n!' in Arbitrary Precision

Description

Efficiently compute n! in arbitrary precision, using the MPFR-internal implementation. This is mathematically (but not numerically) the same as Gamma(n+1).

factorialZ (package gmp) should typically be used instead of factorialMpfr() nowadays. Hence, factorialMpfr now is somewhat deprecated.

Usage

factorialMpfr(n, precBits = max(2, ceiling(lgamma(n+1)/log(2))),
              rnd.mode = c("N","D","U","Z","A"))

Arguments

n

non-negative integer (vector).

precBits

desired precision in bits (“binary digits”); the default sets the precision high enough for the result to be exact.

rnd.mode

a 1-letter string specifying how rounding should happen at C-level conversion to MPFR, see mpfr.

Value

a number of (S4) class mpfr.

See Also

factorial and gamma in base R.

factorialZ (package gmp), to replace factorialMpfr, see above.

chooseMpfr() and pochMpfr() (on the same page).

Examples

factorialMpfr(200)

n <- 1000:1010
f1000 <- factorialMpfr(n)
stopifnot(1e-15 > abs(as.numeric(1 - lfactorial(n)/log(f1000))))

## Note that---astonishingly--- measurements show only
## *small* efficiency gain of ~ 10% : over using the previous "technique"
system.time(replicate(8, f1e4 <- factorialMpfr(10000)))
system.time(replicate(8, f.1e4 <- factorial(mpfr(10000,
                            prec=1+lfactorial(10000)/log(2)))))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Rmpfr)
Loading required package: gmp

Attaching package: 'gmp'

The following objects are masked from 'package:base':

    %*%, apply, crossprod, matrix, tcrossprod

C code of R package 'Rmpfr': GMP using 64 bits per limb


Attaching package: 'Rmpfr'

The following objects are masked from 'package:stats':

    dbinom, dnorm, dpois, pnorm

The following objects are masked from 'package:base':

    cbind, pmax, pmin, rbind

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Rmpfr/factorialMpfr.Rd_%03d_medium.png", width=480, height=480)
> ### Name: factorialMpfr
> ### Title: Factorial 'n!'  in Arbitrary Precision
> ### Aliases: factorialMpfr
> ### Keywords: arith
> 
> ### ** Examples
> 
> factorialMpfr(200)
1 'mpfr' number of precision  1246   bits 
[1] 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
> 
> n <- 1000:1010
> f1000 <- factorialMpfr(n)
> stopifnot(1e-15 > abs(as.numeric(1 - lfactorial(n)/log(f1000))))
> 
> ## Note that---astonishingly--- measurements show only
> ## *small* efficiency gain of ~ 10% : over using the previous "technique"
> system.time(replicate(8, f1e4 <- factorialMpfr(10000)))
   user  system elapsed 
  0.292   0.000   0.292 
> system.time(replicate(8, f.1e4 <- factorial(mpfr(10000,
+                             prec=1+lfactorial(10000)/log(2)))))
   user  system elapsed 
  0.304   0.000   0.305 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>