Ruby factorial function

前端 未结 19 1991
刺人心
刺人心 2020-12-02 10:31

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!

19条回答
  •  抹茶落季
    2020-12-02 10:41

    Just call this function

    def factorial(n=0)
      (1..n).inject(:*)
    end
    

    examples

    factorial(3)
    factorial(11)
    

提交回复
热议问题