如何在 python 中进行双阶乘?
我已经被这个问题困扰了很长时间。 我已经成功地完成了一次递归阶乘。
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
双阶乘 对于偶数 n,双阶乘是所有小于或等于 n 的偶数正整数的乘积。对于奇数整数 p,双阶乘是所有小于或等于 p 的奇数正整数的乘积。
如果 n 是偶数,则 n!! = n*(n - 2)*(n - 4)*(n - 6)* ... *4*2
如果 p 是奇数,则 p!! = p*(p - 2)*(p - 4)*(p - 6)* ... *3*1
但我不知道做双阶乘。有什么帮助吗?
I've been stucked on this question for a really long time.
I've managed to do a single recursive factorial.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
Double factorial
For an even integer n, the double factorial is the product of all even positive integers less than or equal to n. For an odd integer p, the double factorial is the product of all odd positive integers less than or equal to p.
If n is even, then n!! = n*(n - 2)*(n - 4)*(n - 6)* ... *4*2
If p is odd, then p!! = p*(p - 2)*(p - 4)*(p - 6)* ... *3*1
But I have no idea to do a double factorial. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这不是与具有不同结束条件和不同参数的递归调用的阶乘相同吗?
如果
n
是偶数,那么当n == 0
时它将停止。如果n
是奇数,那么当n == -1
时它将停止。Isn't that just the same as the factorial with a different ending condition and a different parameter to the recursion call?
If
n
is even, then it will halt whenn == 0
. Ifn
is odd, then it will halt whenn == -1
.这里的问题是双阶乘是为负实数(-1)定义的! = 1, (-3)!! = -1(即使是负整数(例如-2,-4,...)也应该有+/- inf的解决方案)所以......在负数的所有解决方案中有些东西闻起来很糟糕。如果想为所有实数定义双阶乘,这些解决方案不起作用。解决方案是使用 gamma 函数定义双阶乘。
有用! :D
The problem here is that the double factorial is defined for negative real numbers (-1)!! = 1, (-3)!! = -1 (even negative integers (such -2, -4, ...) should have solution as +/- inf) so... something is smelling bad in all solutions for negative numbers. If one want to define the double factorial for al reals those solutions don't work. The solution is to define the double factorial using gamma function.
It works! :D
从 Python 3.8 开始,我们可以使用 <来自
mathprod
函数code> 模块,它计算可迭代中所有元素的乘积,在我们的例子中是range(n, 0, -2)
:请注意,这也处理
的情况n = 0
在这种情况下,结果为1
。Starting
Python 3.8
, we can use theprod
function from themath
module which calculates the product of all elements in an iterable, which in our case isrange(n, 0, -2)
:Note that this also handles the case
n = 0
in which case the result is1
.我的递归解决方案的版本,一行:
然而,有趣的是,双阶乘可以用“正常”阶乘来表示。对于奇数,
n!! = (2*k)! / (2**k * k!)
其中 k = (n+1)/2。对于偶数参数 n=2k,虽然这与对复杂参数的概括不一致,但表达式更简单,
n!! =(2k)!! = 2*k * k!。
所有这些意味着您可以使用标准数学库中的阶乘函数编写代码,这总是很好:
现在,这段代码对于大 n 显然不是很有效,但它非常有启发性。更重要的是,由于我们现在正在处理标准阶乘,因此在处理非常大的数字时,这是优化的一个非常好的起点。您尝试使用对数或伽马函数来获得大数的近似双阶乘。
My version of the recursive solution, in one line:
However, it is also interesting to note that the double factorial can be expressed in terms of the "normal" factorial. For odd numbers,
n!! = (2*k)! / (2**k * k!)
where k = (n+1)/2. For even arguments n=2k, although this is not consistent with a generalization to complex arguments, the expression is simpler,
n!! = (2k)!! = 2*k * k!.
All this means that you can write code using the factorial function from the standard math library, which is always nice:
Now, this code is obviously not very efficient for large n, but it is quite instructive. More importantly, since we are dealing with standard factorials now, it is a very good starting point for optimizations when dealing with really large numbers. You try to use logarithms or gamma functions to get approximate double factorials for large numbers.
应该这样做。
should do it.
我认为这应该对你有用。
I think this should work for you.
我希望我理解正确,但这行得通吗
I hope I understand it correctly, but will this work
reduce(lambda x,y: y*x, range(n,1,-2))
这与简单的迭代版本基本相同:
显然你也可以递归地执行,但是什么是观点 ?使用递归性实现的这种示例在使用所有递归语言时都很好,但对于命令式语言,它总是使像递归性这样的简单工具看起来比必要的更加复杂,而在处理像树这样的基本递归结构时,递归性可以是一个真正的简化器。
reduce(lambda x,y: y*x, range(n,1,-2))
Which is basically the same as the simple iterative version:
Obviously you can also do it recursively, but what's the point ? This kind of example implemented using recursivity are fine when using all recursive languages, but with imperative language it's always making simple tools like recursivity looking more complex than necessary, while recursivity can be a real simplifier when dealing with fundamentally recursive structures like trees.
应该可以做到这一点。除非我误会了
That should do it. Unless I'm misunderstanding
我遇到了同样的问题,我是这样决定的:
I ran into the same problem, I decided like this: