Sha256: 5889513de42d5a78f68b354e6b09c4f1d83f55dd579eddf0a71e558604e010f0

Contents?: true

Size: 518 Bytes

Versions: 10

Compression:

Stored size: 518 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

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/integer/factorial.rb
facets-2.8.3 lib/core/facets/integer/factorial.rb
facets-2.8.2 lib/core/facets/integer/factorial.rb
facets-2.8.1 lib/core/facets/integer/factorial.rb
facets-2.8.0 lib/core/facets/integer/factorial.rb
facets-2.7.0 lib/core/facets/integer/factorial.rb
facets-2.6.0 lib/core/facets/integer/factorial.rb
facets-2.5.0 lib/core/facets/integer/factorial.rb
facets-2.5.1 lib/core/facets/integer/factorial.rb
facets-2.5.2 lib/core/facets/integer/factorial.rb