本文轉(zhuǎn)載自:https://www.cnblogs.com/hider/p/10019220.html
總結(jié)
組合數(shù):choose(n,k) —— 從n個(gè)中選出k個(gè)
階乘:factorial(k) —— k!
排列數(shù):choose(n,k) * factorial(k)
冪:^
余數(shù):%%
整數(shù)商:%/%
列出所有組合數(shù)矩陣:combn(x,n)
t(combn(x,n)) 轉(zhuǎn)置
二、例子
choose(5,3) # 10
factorial(5) # 120
choose(5,3)*factorial(3) # 60
2^10 # 1024
10 %% 3 # 1
10 %/% 3 # 3
x <- 1:5
n <-3
combn(x,n)
/*
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 1 1 2 2 2 3
[2,] 2 2 2 3 3 4 3 3 4 4
[3,] 3 4 5 4 5 5 4 5 5 5
*/
x <- 1:5
n <-3
combn(x,n)
/*
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 1 1 2 2 2 3
[2,] 2 2 2 3 3 4 3 3 4 4
[3,] 3 4 5 4 5 5 4 5 5 5
*/