Python:计算非整数的阶乘 [英] Python: Calculate factorial of a non-integral number

查看:304
本文介绍了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:计算非整数的阶乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆