Sha256: 52fb770ae975b5ca9d9bf42eacb7011d69113c0a4001bbe28ca4e92be46ccaa9

Contents?: true

Size: 423 Bytes

Versions: 27

Compression:

Stored size: 423 Bytes

Contents

class Integer
  # 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

27 entries across 27 versions & 1 rubygems

Version Path
ndr_support-5.6.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.6.0 lib/ndr_support/integer/calculations.rb
ndr_support-5.5.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.5.0 lib/ndr_support/integer/calculations.rb
ndr_support-5.4.2 lib/ndr_support/integer/calculations.rb
ndr_support-5.4.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.3.2 lib/ndr_support/integer/calculations.rb