Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

factorialZ

Factorial and Binomial Coefficient as Big Integer


Description

Efficiently compute the factorial n! or a binomial coefficient choose(n, k) as big integer (class bigz).

Usage

factorialZ(n)
chooseZ(n, k)

Arguments

n

non-negative integer (vector), for factorialZ. For chooseZ, may be a bigz big integer, also negative.

k

non-negative integer vector.

Value

a vector of big integers, i.e., of class bigz.

See Also

factorial and gamma in base R;

Examples

factorialZ(0:10)# 1 1 2 6 ... 3628800
factorialZ(0:40)# larger
factorialZ(200)

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

system.time(replicate(8, f1e4 <<- factorialZ(10000)))
nchar(as.character(f1e4))# 35660 ... (too many to even look at ..)

chooseZ(1000, 100:102)# vectorizes
chooseZ(as.bigz(2)^120, 10)
n <- c(50,80,100)
k <- c(20,30,40)
## currently with an undesirable warning: % from methods/src/eval.c  _FIXME_
stopifnot(chooseZ(n,k) == factorialZ(n) / (factorialZ(k)*factorialZ(n-k)))

gmp

Multiple Precision Arithmetic

v0.6-2
GPL (>= 2)
Authors
Antoine Lucas, Immanuel Scholz, Rainer Boehme <rb-gmp@reflex-studio.de>, Sylvain Jasson <Sylvain.Jasson@inrae.fr>, Martin Maechler <maechler@stat.math.ethz.ch>
Initial release
2021-01-07

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.