Skip to content

Instantly share code, notes, and snippets.

@baya
Created December 11, 2012 03:48
Show Gist options
  • Save baya/4255760 to your computer and use it in GitHub Desktop.
Save baya/4255760 to your computer and use it in GitHub Desktop.
计算排列组合
# 计算排列组合
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