本文介绍了Python:计算非整数的阶乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有快速的Pythonic方法来计算非整数(例如3.4)的阶乘?当然, Math 模块中的 factorial()函数是可用的,但它只适用于积分(我不在乎这里的负数)。 解决方案
你想要使用 math.gamma(x)。
gamma功能是一个扩展名因子函数与实数的关系。
请注意,与阶乘函数相比,函数移位1。所以 math.factorial(n)是 math.gamma(n + 1)。
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).
解决方案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).
更多推荐
Python:计算非整数的阶乘
发布评论