Sha256: fd2e2f7642496719334aae178114b7d3582d3e895e702b11dddd18f194f69f53

Contents?: true

Size: 302 Bytes

Versions: 10

Compression:

Stored size: 302 Bytes

Contents

class Integer

  # Calculate the factorial of an integer.
  #
  #   2.factorial  #=> 2
  #   3.factorial  #=> 6
  #   4.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 )

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/integer/factorial.rb
facets-3.1.0 lib/core/facets/integer/factorial.rb
facets-3.0.0 lib/core/facets/integer/factorial.rb
facets-2.9.3 lib/core/facets/integer/factorial.rb
facets-2.9.2 lib/core/facets/integer/factorial.rb
facets-2.9.2 src/core/facets/integer/factorial.rb
facets-2.9.1 lib/core/facets/integer/factorial.rb
facets-2.9.0 lib/core/facets/integer/factorial.rb
facets-2.9.0.pre.2 lib/core/facets/integer/factorial.rb
facets-2.9.0.pre.1 lib/core/facets/integer/factorial.rb