Is There a Package or Technique Availabe for Calculating Large Factorials in R

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

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

Calculation of the factorial of a large number in R

Use factorialZ from the gmp package:

> gmp::factorialZ(171)
Big Integer ('bigz') :
[1] 1241018070217667823424840524103103992616605577501693185388951803611996075221691752992751978120487585576464959501670387052809889858690710767331242032218484364310473577889968548278290754541561964852153468318044293239598173696899657235903947616152278558180061176365108428800000000000000000000000000000000000000000

the largest n that you can use in recursion factorial in R

The bit-span of computer numeric values seems much more likely to be the problem than the "computer memory" by which I am assuming you meant the address space of RAM. The largest value for an integer that can be represented without approximation in R (and any software package using the usual IEEE standard) is 2^53 - 1. There are other packages (some of them available as R packages) that can support arbitrary precision numerics. The ?Recall functions is a more stable methods of doing recursion, although it is not optimized in R. The integer.max is set at the precison obtainable with 2 bytes:

 2^32-1
[1] 4294967295

And the more recently introduced "long integers" max out at the limits of the mantissa of floating point "doubles".

> 2^53-1
[1] 9.007199e+15
> print( 2^53-1, digits=18)
[1] 9007199254740991

So as soon as you get a value of factorial(n) that is larger than that limit you only get an approximation and when you exceed the limits of the exponentiation for a "double" numeric, you get "Inf". My version of factorial seems to have the same breaking point as yours:

> fact <- function(n)
+ if(n==0) { 1} else{ (n) *Recall(n-1)}
> fact (170)
[1] 7.257416e+306
> fact (171)
[1] Inf

Here's another way of thinking about calculating factorial:

> exp( sum(log(1:15)))
[1] 1.307674e+12
> factorial(15)
[1] 1.307674e+12

The "Stirling's approximation" is often used to speed up calculations of factorial. For more accurate values you can install either the gmp or the Rmpfr packages.

Calculating factorials with numbers bigger than ints and longs in java?

You can use BigInteger in java, it has as much numbers as you want

    BigInteger fact= BigInteger.ONE;
int factorialNo = 10;

for (int i = 2; i <= factorialNo; i++){
fact = fact.multiply(new BigInteger(String.valueOf(i)));
}

System.out.println("The factorial of " + factorialNo +
" (or " + factorialNo + "!) is: " + fact);
final int digits = fact.toString().length();

BigInteger number = new BigInteger(fact.toString());
BigInteger reminder;
BigInteger sum = BigInteger.ZERO;
BigInteger ten = new BigInteger(String.valueOf(10));

while(number.compareTo(BigInteger.ONE)>=0)
{
reminder=number.mod(ten);
sum=sum.add(reminder);
number=number.divide(ten);
}

System.out.println("The total sum of all the " + digits
+ " idividual digits from the answer of the factorial of "
+ factorialNo + " is: " + sum

EDIT: the code is improved to be compatible with author's code

Generating all distinct permutations of a list in R

combinat::permn will do that work:

> library(combinat)
> permn(letters[1:3])
[[1]]
[1] "a" "b" "c"

[[2]]
[1] "a" "c" "b"

[[3]]
[1] "c" "a" "b"

[[4]]
[1] "c" "b" "a"

[[5]]
[1] "b" "c" "a"

[[6]]
[1] "b" "a" "c"

Note that calculation is huge if the element is large.

Is there a method that calculates a factorial in Java?

I don't think it would be useful to have a library function for factorial. There is a good deal of research into efficient factorial implementations. Here is a handful of implementations.



Related Topics



Leave a reply



Submit