Alex Rivera | Logout

Ruby factorial function

Asked 2010-03-12T17:20:49.090
101

I'm going crazy: Where is the Ruby function for factorial? No, I don't need tutorial implementations, I just want the function from the library. It's not in Math!

I'm starting to doubt, is it a standard library function?

Edit
Report

2 Answers

122

Like this is better

(1..n).inject(:*) || 1
answered 2012-09-13T21:57:18.480
0
class Integer
  def factorial
    return self < 0 ? false : self==0 ? 1 : self.downto(1).inject(:*)
    #Not sure what other libraries say, but my understanding is that factorial of 
    #anything less than 0 does not exist.
  end
end
answered 2013-05-12T23:57:26.900

Your Answer