如何在 python 中进行双阶乘?

发布于 2024-10-12 20:31:54 字数 415 浏览 5 评论 0原文

我已经被这个问题困扰了很长时间。 我已经成功地完成了一次递归阶乘。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

回忆躺在深渊里 2024-10-19 20:31:54
from functools import reduce # only in Python 3

reduce(int.__mul__, range(n, 0, -2))
from functools import reduce # only in Python 3

reduce(int.__mul__, range(n, 0, -2))
心不设防 2024-10-19 20:31:54

这不是与具有不同结束条件和不同参数的递归调用的阶乘相同吗?

def doublefactorial(n):
     if n <= 0:
         return 1
     else:
         return n * doublefactorial(n-2)

如果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?

def doublefactorial(n):
     if n <= 0:
         return 1
     else:
         return n * doublefactorial(n-2)

If n is even, then it will halt when n == 0. If n is odd, then it will halt when n == -1.

情感失落者 2024-10-19 20:31:54

这里的问题是双阶乘是为负实数(-1)定义的! = 1, (-3)!! = -1(即使是负整数(例如-2,-4,...)也应该有+/- inf的解决方案)所以......在负数的所有解决方案中有些东西闻起来很糟糕。如果想为所有实数定义双阶乘,这些解决方案不起作用。解决方案是使用 gamma 函数定义双阶乘。

import scipy.special as sp
from numpy import pi

def dfact(x):
    n = (x + 1.)/2.
    return 2.**n * sp.gamma(n + 0.5)/(pi**(0.5))

有用! :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.

import scipy.special as sp
from numpy import pi

def dfact(x):
    n = (x + 1.)/2.
    return 2.**n * sp.gamma(n + 0.5)/(pi**(0.5))

It works! :D

国粹 2024-10-19 20:31:54

从 Python 3.8 开始,我们可以使用 <来自 mathprod 函数code> 模块,它计算可迭代中所有元素的乘积,在我们的例子中是 range(n, 0, -2)

import math

math.prod(range(n, 0, -2))

请注意,这也处理 的情况n = 0 在这种情况下,结果为 1

Starting Python 3.8, we can use the prod function from the math module which calculates the product of all elements in an iterable, which in our case is range(n, 0, -2):

import math

math.prod(range(n, 0, -2))

Note that this also handles the case n = 0 in which case the result is 1.

遇到 2024-10-19 20:31:54

我的递归解决方案的版本,一行:

dfact = lambda n: (n <= 0) or n * dfact(n-2)

然而,有趣的是,双阶乘可以用“正常”阶乘来表示。对于奇数,

n!! = (2*k)! / (2**k * k!)

其中 k = (n+1)/2。对于偶数参数 n=2k,虽然这与对复杂参数的概括不一致,但表达式更简单,

n!! =(2k)!! = 2*k * k!。

所有这些意味着您可以使用标准数学库中的阶乘函数编写代码,这总是很好:

import math
fact = math.factorial
def dfact(n):
  if n % 2 == 1:
    k = (n+1)/2
    return fact(2*k) / (2**k * fact(k))
  else:
    return 2**k * fact(k)

现在,这段代码对于大 n 显然不是很有效,但它非常有启发性。更重要的是,由于我们现在正在处理标准阶乘,因此在处理非常大的数字时,这是优化的一个非常好的起点。您尝试使用对数或伽马函数来获得大数的近似双阶乘。

My version of the recursive solution, in one line:

dfact = lambda n: (n <= 0) or n * dfact(n-2)

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:

import math
fact = math.factorial
def dfact(n):
  if n % 2 == 1:
    k = (n+1)/2
    return fact(2*k) / (2**k * fact(k))
  else:
    return 2**k * fact(k)

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.

蓝戈者 2024-10-19 20:31:54
def doublefactorial(n):
     if n in (0, 1):
         return 1
     else:
         return n * doublefactorial(n-2)

应该这样做。

def doublefactorial(n):
     if n in (0, 1):
         return 1
     else:
         return n * doublefactorial(n-2)

should do it.

冷情妓 2024-10-19 20:31:54
def double_fact(number):
    if number==0 or number==1:
        return 1
    else:
        return number*double_fact(number-2)

我认为这应该对你有用。

def double_fact(number):
    if number==0 or number==1:
        return 1
    else:
        return number*double_fact(number-2)

I think this should work for you.

孤蝉 2024-10-19 20:31:54

我希望我理解正确,但这行得通吗

 def factorial(n):
 if n == 0 or n == 1:
     return 1
 else:
     return n * factorial(n-2)

I hope I understand it correctly, but will this work

 def factorial(n):
 if n == 0 or n == 1:
     return 1
 else:
     return n * factorial(n-2)
殤城〤 2024-10-19 20:31:54

reduce(lambda x,y: y*x, range(n,1,-2))

这与简单的迭代版本基本相同:

x = n
for y in range(n-2, 1, -2):
    x*=y

显然你也可以递归地执行,但是什么是观点 ?使用递归性实现的这种示例在使用所有递归语言时都很好,但对于命令式语言,它总是使像递归性这样的简单工具看起来比必要的更加复杂,而在处理像树这样的基本递归结构时,递归性可以是一个真正的简化器。

reduce(lambda x,y: y*x, range(n,1,-2))

Which is basically the same as the simple iterative version:

x = n
for y in range(n-2, 1, -2):
    x*=y

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.

毁梦 2024-10-19 20:31:54
def doublefactorial(n):
     if n <= 0:
         return 1
     else:
         return n * doublefactorial(n-2)

应该可以做到这一点。除非我误会了

def doublefactorial(n):
     if n <= 0:
         return 1
     else:
         return n * doublefactorial(n-2)

That should do it. Unless I'm misunderstanding

清风疏影 2024-10-19 20:31:54

我遇到了同样的问题,我是这样决定的:

def double_factorial(N):
if N == 0: return 1
elif N == 1: return 1
elif N == 2: return 2
else:
    return N * double_factorial(N - 2)

I ran into the same problem, I decided like this:

def double_factorial(N):
if N == 0: return 1
elif N == 1: return 1
elif N == 2: return 2
else:
    return N * double_factorial(N - 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文