I read the rdoc but it's a little verbose for me.. I found this code online to compute nCr :
Rdoc says ..
Can someone give me .. Easier english, please?
Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
Code:
def nCr(n,r)
a, b = r, n-r
a, b = b, a if a < b # a is the larger
numer = (a+1..n).inject(1) { |t,v| t*v } # n!/r!
denom = (2..b).inject(1) { |t,v| t*v } # (n-r)!
numer/denom
end
Rdoc says ..
enum.inject(initial) {| memo, obj | block } => obj
enum.inject {| memo, obj | block } => obj
Combines the elements of enum by applying the block to an accumulator value (memo) and each element in turn. At each step, memo is set to the value returned by the block. The first form lets you supply an initial value for memo. The second form uses the first element of the collection as a the initial value (and skips that element while iterating).
Can someone give me .. Easier english, please?
Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?