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.10.4 lib/ndr_support/integer/calculations.rb
ndr_support-5.10.3 lib/ndr_support/integer/calculations.rb
ndr_support-5.10.2 lib/ndr_support/integer/calculations.rb
ndr_support-5.10.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.10.0 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.7 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.6 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.5 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.4 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.3 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.2 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.9.0 lib/ndr_support/integer/calculations.rb
ndr_support-5.8.4 lib/ndr_support/integer/calculations.rb
ndr_support-5.8.3 lib/ndr_support/integer/calculations.rb
ndr_support-5.8.2 lib/ndr_support/integer/calculations.rb
ndr_support-5.8.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.8.0 lib/ndr_support/integer/calculations.rb
ndr_support-5.7.1 lib/ndr_support/integer/calculations.rb
ndr_support-5.7.0 lib/ndr_support/integer/calculations.rb