Method: Numeric#choose
- Defined in:
- lib/epitools/core_ext/numbers.rb
#choose(r) ⇒ Object Also known as: combinations
Combinations: compute “n choose r” (self.choose®)
This represents number of ways to pick “r” items from a collection of “self” items (where the order of the items doesn’t matter, and items can’t be repeated.)
eg: 49.choose(6) is how many ways can we pick 6 lottery numbers from a set of 49.
Formula: n! / (r! * (n-r)!) == n * n-1 * … * n-r / r * r-1 * … * 2
136 137 138 |
# File 'lib/epitools/core_ext/numbers.rb', line 136 def choose(r) (self-r+1..self).reduce(:*) / (2..r).reduce(:*) end |