Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a package or technique availabe for calculating large factorials in R?

Tags:

r

factorial

If I calculate factorial(100) then I get an answer of [1] 9.332622e+157 but when I try to calculate a larger factorial, say factorial(1000) I get an answer of [1] Inf

Is there a way to use arbitrary precision when calculating factorials such that I can calculate say factorial(1000000)?

like image 732
chollida Avatar asked Dec 20 '22 00:12

chollida


1 Answers

For arbitrary precision you can use either gmp or Rmpfr. For specifically factorial gmp offers factorialZ and Rmpfr has factorialMpfr. So you can run something like below

> Rmpfr::factorialMpfr(200)
1 'mpfr' number of precision  1246   bits 
[1] 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
> gmp::factorialZ(200)
Big Integer ('bigz') :
[1] 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000

HTH

like image 130
Frash Avatar answered Dec 28 '22 11:12

Frash