Skip to content

Latest commit

 

History

History
16 lines (13 loc) · 292 Bytes

factorial.rb.md

File metadata and controls

16 lines (13 loc) · 292 Bytes

Memoized Factorial in Ruby

I had a dream about this and had to write it down for some reason

class Integer
  @@memo = {0 => 1}

  def factorial
    return @@memo[self] if @@memo.include? self

    value = self * (self - 1).factorial
    @@memo[self] = value
    value
  end
end