在 R 编程中计算 nCr 值 - choose()函数
R 语言提供了一个直接函数,可以计算 nCr 值,而无需编写用于计算 nCr 值的整个代码。
Syntax: choose(n, r)
Parameters:
n: Number of elements
r: Number of combinations
Returns: The number of r combinations from a total of n elements, i.e, nCr value.
示例 1:
# R program to calculate nCr value
# Using choose() method
answer1 <- choose(3, 2)
answer2 <- choose(3, 7)
answer3 <- choose(7, 3)
print(answer1)
print(answer2)
print(answer3)
输出:
3
0
35
示例 2:如果我们提供 n 和 r 的值,使得 n < r,则 choose(n, r) 将返回 0。
# R program to calculate nCr value
# Using choose() method
answer1 <- choose(2, 3)
answer2 <- choose(3, 6)
answer3 <- choose(3, 7)
print(answer1)
print(answer2)
print(answer3)
输出:
0
0
0