Python:计算非整数的阶乘

编程入门 行业动态 更新时间:2024-10-27 02:21:54
本文介绍了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:计算非整数的阶乘

本文发布于:2023-11-29 19:39:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阶乘   整数   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!