My implementation of combination function. I choose to do the multiplication and division at each iteration so that the function will be able to compute combinations for large numbers. Doing all multiplications (either using prod() function or other multiplication technique) before doing division causes the computation operation to quickly reach the max number limit and hence those techniques are not used.
mychoose = function(n,r) {
#Check if integer. If not round to a integer
if (n%%1 != 0) n = round(n)
if (r%%1 != 0) r = round(r)
#Check for negativity and invalid cases
if (r < 0 | n < r) return(0)
#Retrun 1 if n and r are same or r is zero
if ( n == r | r == 0) return(1)
#nCr = nC(n-r). So use a large r value to reduce number of calcuations later
if (r < n/2) r = n - r
c = 1
#Use for loop and do the multiplication and division at each loop so that combination for large numbers can be calculated
#Note:Following loop computes (r+1)*(r+2)...n/(n-r)! which is same as n!/(n-r)!r!
for (i in (r+1):n) c = c*(i/(i-r))
c
}
Simple Tests
mychoose(5,3)
## [1] 10
choose(5,3)
## [1] 10
mychoose(15,4)
## [1] 1365
choose(15,4)
## [1] 1365
Test for Large Numbers
mychoose(2000,229)
## [1] 3.234683e+307
choose(2000,229)
## [1] 3.234683e+307
mychoose(2000,1815)
## [1] 1.821137e+266
choose(2000,1815)
## [1] 1.821137e+266
mychoose(100000,100000)
## [1] 1
choose(100000,100000)
## [1] 1
Negative Test Case
mychoose(5,-1)
## [1] 0
mychoose(-5,1)
## [1] 0
mychoose(-5,-1)
## [1] 0
mychoose(5,7)
## [1] 0