In article <bk40gc$ooij8$5 / ID-52924.news.uni-berlin.de>,
Robert Klemme <bob.news / gmx.net> wrote:
: Since recursions are costly and abort the interpreter for large n I
: suggest this implementation:
: 
: def fact(n)
:   return 0 if n<1
: 
:   @facts ||= [0,1]
: 
:   last = @facts.size - 1
: 
:   for i in @facts.size .. n
:     @facts[i] = @facts[last] * i
:     last = i
:   end
: 
:   @facts[n]
: end

You could do that a lot more concisely using inject:

def fact(x)
     @facts[x] ||= (1..x).inject(1) { |a,i| a*i }
end

(Happy side-effect: inject and .. do the Right Thing with respect
to zero and negative values, so you don't need to check for
it--that's already done for you.)

--Dave