Python 递归阶乘函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4502429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:02:29  来源:igfitidea点击:

recursive factorial function

pythonrecursionfactorial

提问by user531225

how can I combine these two functions in to one recursive function to have this result:

我怎样才能将这两个函数组合成一个递归函数来得到这个结果:

factorial(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720

these are the codes

这些是代码

def factorial( n ):
   if n <1:   # base case
       return 1
   else:
       return n * factorial( n - 1 )  # recursive call
def fact(n):
       for i in range(1, n+1 ):
               print "%2d! = %d" % ( i, factorial( i ) )

fact(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720

as you see execution of these two gives a correct answer, I just want to make it to one recursive function.

当你看到这两个的执行给出了正确的答案时,我只想把它变成一个递归函数。

采纳答案by pythonFoo

def factorial( n ):
   if n <1:   # base case
       return 1
   else:
       returnNumber = n * factorial( n - 1 )  # recursive call
       print(str(n) + '! = ' + str(returnNumber))
       return returnNumber

回答by Mchl

I've no experience with Python, but something like this?

我没有使用 Python 的经验,但类似这样的事情?

def factorial( n ):
   if n <1:   # base case
       return 1
   else:
       f = n * factorial( n - 1 )  # recursive call
       print "%2d! = %d" % ( n, f )
       return f

回答by Vinay Pandey

try this:

尝试这个:

def factorial( n ):
   if n <1:   # base case
       print "%2d! = %d" % (n, n)
       return 1
   else:
       temp = factorial( n - 1 )
       print "%2d! = %d" % (n, n*temp)
       return n * temp  # recursive call

One thing I noticed is that you are returning '1' for n<1, that means your function will return 1 even for negative numbers. You may want to fix that.

我注意到的一件事是,对于 n<1,您将返回 '1',这意味着即使对于负数,您的函数也会返回 1。你可能想解决这个问题。

回答by Will McCutchen

def factorial(n):
    result = 1 if n <= 1 else n * factorial(n - 1)
    print '%d! = %d' % (n, result)
    return result

回答by D.Shawley

Is this homework by any chance?

这是作业吗?

def traced_factorial(n):
  def factorial(n):
    if n <= 1:
      return 1
    return n * factorial(n - 1)
  for i in range(1, n + 1):
    print '%2d! = %d' %(i, factorial(i))

Give PEP227a read for more details. The short of it is that Python lets you define functions within functions.

PEP227读更多的细节。简而言之,Python 允许您在函数内定义函数。

回答by MattyW

One more

多一个

def fact(x):
    if x == 0:
        return 0
    elif x == 1:
        return 1
    else:
        return x * fact(x-1)

for x in range(0,10):
    print '%d! = %d' %(x, fact(x))

回答by Ilyas

a short one:

一个简短的:

def fac(n):
    if n == 0:
        return 1
    else:
        return n * fac(n-1)
print fac(0)

回答by martynas

2 lines of code:

2行代码:

def fac(n):
    return 1 if (n < 1) else n * fac(n-1)

Test it:

测试一下:

print fac(4)

Result:

结果:

24

回答by T-kin-ter

fac = lambda x: 1 if x == 0 else x * fac(x - 1)

回答by Salah Hamza

I don't really know the factorial of negative numbers, but this will work with all n >= 0:

我真的不知道负数的阶乘,但这适用于所有 n >= 0:

def factorial(n):
if n >= 0:
    if n == 1 or n==0:
        return 1
    else:
        n = n * factorial(n-1)
        return n
else:
    return 'error'