Ruby factorial function

前端 未结 19 1990
刺人心
刺人心 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:38

    In math, factorial of n is just the gamma function of n+1
    (see: http://en.wikipedia.org/wiki/Gamma_function)

    Ruby has Math.gamma() so just use Math.gamma(n+1) and cast it back to an integer if desired.

提交回复
热议问题