レイリーフェージング下のSNRを求めるプログラム書きたかったのに、Rubyよりも電波伝搬の知識が足りなかったので、確立つながりで書いてみた。
階乗が適当すぎる。再帰使うと頭の中こんがらがるし、どっちみちあんまり早くないみたいだから今回はこんなんで良いかなと。誰か良い書き方を教えてください。
# Conbination test # c.rb n m => nCm # c.rb n => nC1 class Integer # 5.combination(2) => 5C2(=5!/(2!3!)) # n.combination(nil | 0) => nC1 def combination(i = 1) i = 1 if i == 0 factorial(self) / ( factorial(i) * factorial(self - i) ) end # factorial(5) => 5!(=5*4*3*2*1) def factorial(n, min = 1) res = 1 n.downto(min){|i| res *= i} res end private :factorial end p ARGV.shift.to_i.combination(ARGV.first.to_i)
結果
% ruby c.rb 10 2 45
おっけー。