Python: Calculate factorial of a non-integral number

时光怂恿深爱的人放手 提交于 2019-12-06 22:39:57

问题


I'm wondering if there's a speedy, Pythonic way to calculate factorials of non-integral numbers (e.g., 3.4)? Of course, the bult-in factorial() function in the Math module is available, but it only works for integrals (I don't care about negative numbers here).


回答1:


You'd want to use math.gamma(x).

The gamma function is an extension of the factorial function to real numbers.

Note that the function is shifted by 1 when compared to the factorial function. So math.factorial(n) is math.gamma(n + 1).




回答2:


In Python 2.7 or 3.2, you can use math.gamma(x + 1). In older versions, you'd need some external library like SciPy.



来源:https://stackoverflow.com/questions/10056797/python-calculate-factorial-of-a-non-integral-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!