Sha256: 77a339e6a16368cbcebfa3afc5d40531b029eb99cc0ad4050b77db1cb603fc1c

Contents?: true

Size: 519 Bytes

Versions: 7

Compression:

Stored size: 519 Bytes

Contents

class Integer

  # Calculate the factorial of an integer.
  #
  #   2.factorial  #=> 2
  #   3.factorial  #=> 6
  #   3.factorial  #=> 24
  #
  #  CREDIT: Malte Milatz

  def factorial
    return 1 if zero?
    f = 1
    2.upto(self) { |n| f *= n }
    f
  end

  alias_method( :fac, :factorial )

  #-- OLD CODE
  #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

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/integer/factorial.rb
facets-2.4.1 lib/facets/integer/factorial.rb
facets-2.4.2 lib/core/facets/integer/factorial.rb
facets-2.4.3 lib/core/facets/integer/factorial.rb
facets-2.4.4 lib/core/facets/integer/factorial.rb
facets-2.4.5 lib/core/facets/integer/factorial.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/integer/factorial.rb