Sha256: 1a35e31b5387fe4348a7e360b1e5585fae9a4d2ba7513e93812a425f82e58577
Contents?: true
Size: 422 Bytes
Versions: 14
Compression:
Stored size: 422 Bytes
Contents
class Fixnum # Gets binomial coefficients: # # 4.choose(2) #=> 6 # def choose(k) fail(ArgumentError, "cannot choose #{k} from #{self}") unless (0..self) === k self.factorial / (k.factorial * (self - k).factorial) end def factorial fail("cannot calculate #{self}.factorial") unless self >= 0 # limited implementation self.zero? ? 1 : (1..self).inject { |product, i| product * i } end end
Version data entries
14 entries across 14 versions & 1 rubygems