R Functions: Factorial, Permutation, Combination
## Function: Factorial of a Number
## Input: A non-negative integer
## Ouput: A positive integer
Factorial <- function(N){
if(N==0) return(1)
else return(N*Factorial(N-1))
}
## Function: Permutation
## Input: Positive Integer N and integer k such that
## k >= 0 and k < n.
## Output: A positive integer
Permutation <- function(N,k){
return(Factorial(N)/Factorial(N-k))
}
## Function: Combination
## Input: A positive integer N and an integer k
## such that k >= 0 and k < N.
## Output: A positive integer
Combination <- function(N,k){
return(Permutation(N,k)/Factorial(k))
}
Examples:
Factorial(0)
## [1] 1
Factorial(1)
## [1] 1
Factorial(2)
## [1] 2
Factorial(10)
## [1] 3628800
Permutation(5,0)
## [1] 1
Permutation(5,2)
## [1] 20
Permutation(5,5)
## [1] 120
Permutation(20,10)
## [1] 670442572800
Combination(5,0)
## [1] 1
Combination(5,5)
## [1] 1
Combination(20,10)
## [1] 184756