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

Version Path
ndr_support-5.3.1 lib/ndr_support/fixnum/calculations.rb
ndr_support-5.3.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-5.2.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-5.1.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-4.2.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-5.0.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-4.1.2 lib/ndr_support/fixnum/calculations.rb
ndr_support-4.1.1 lib/ndr_support/fixnum/calculations.rb
ndr_support-4.0.1 lib/ndr_support/fixnum/calculations.rb
ndr_support-4.0.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-3.3.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-3.2.1 lib/ndr_support/fixnum/calculations.rb
ndr_support-3.2.0 lib/ndr_support/fixnum/calculations.rb
ndr_support-3.1.1 lib/ndr_support/fixnum/calculations.rb