Sha256: dfb7006099f3a5259035b0657e8ddf0df82b713d1d51208ae237c196cb4263c8

Contents?: true

Size: 379 Bytes

Versions: 3

Compression:

Stored size: 379 Bytes

Contents

class Integer  
  # Calculate the factorial of an integer.
  #
  #   require 'facet/integer/factorial'
  #
  #   2.factorial  #=> 2
  #   3.factorial  #=> 6
  #   3.factorial  #=> 24
  #
  def factorial
    return 1 if self == 0
    #self == 0 ? 1 : ( self * (self-1).factorial )
    f = (1..self.abs).inject { |state, item| state * item }
    return self < 0 ? -f : f
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/integer/factorial.rb
facets-0.7.1 lib/facet/integer/factorial.rb
facets-0.7.2 lib/facet/integer/factorial.rb