Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factorial with R

Tags:

r

factorial

I can't calculate the factorial of 365 by using factorial(365) with the R logial, I think the capacity of this logicial don't allow it. How can I do with an other method?

like image 241
Martialll Avatar asked Mar 09 '18 10:03

Martialll


1 Answers

You can use lgamma(x+1) to get the natural log of factorial.

factorial(365)
# [1] Inf
# Warning message:
# In factorial(365) : value out of range in 'gammafn'
lgamma(366)
# 1792.332
# convince yourself that this works:
x <- 2:10
format(factorial(x), scientific = FALSE) == format(exp(lgamma(x + 1)), scientific = FALSE)
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

floating point math can get you into trouble at times, but lgamma(366) is accurate for ln(factorial(365))

like image 114
De Novo Avatar answered Sep 27 '22 20:09

De Novo