A: How do I calculate large numbers in n-choose-k?


This question already has an answer here:

  • How would you program Pascal's triangle in R? 2 answers
  • How to work with large numbers in R? 1 answer

For a class assignment, I need to create a function that calculates n Choose k. I did just that, and it works fine with small numbers (e.g. 6 choose 2), but I'm supposed to get it work with 200 choose 50, where it naturally doesn't. The answer is too large and R outputs NaN or Inf, saying:

> q5(200, 50)
[1] "NaN"
Warning message:
In factorial(n) : value out of range in 'gammafn'

I tried using logs and exponents, but it doesn't cut it.

q5 <- function (n, k) {
  answer <- log(exp( factorial(n) / ( (factorial(k)) * (factorial(n - k)) )))

The answer to the actual question is that R cannot show numbers it cannot represent, and some of the terms in your equation are too big to represent. So it fails. However there are approximations to factorial that can be used - they work with logarithms which get big a lot slower.

The most famous one, Sterling's approximation, was not accurate enough, but the Ramanujan's approximation came to the rescue :)

ramanujan <- function(n){
  n*log(n) - n + log(n*(1 + 4*n*(1+2*n)))/6 + log(pi)/2

nchoosek <- function(n,k){

bignchoosek <- function(n,k){
  exp(ramanujan(n) - ramanujan(k) - ramanujan(n-k))

# [1] 15504

# [1] 15504.06

# [1] 4.538584e+47