Created
December 11, 2012 03:48
-
-
Save baya/4255760 to your computer and use it in GitHub Desktop.
计算排列组合
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 计算排列组合 | |
module LotMath | |
extend self | |
# 阶乘 | |
def F(n) | |
return 1 if n == 0 | |
(1..n).inject(:*) | |
end | |
# 排列 | |
def P(n, k) | |
F(n) / F(n - k) | |
end | |
# 组合 | |
def C(n, k) | |
P(n, k) / F(k) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment