递归阶乘函数
如何将这两个函数组合成一个递归函数以获得以下结果:
factorial(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
这是我的阶乘函数的当前代码:
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
如您所见,执行这两个函数给了我正确的答案,但我只是想将这两个函数简化为单个递归函数。
How can I combine these two functions into one recursive function to have this result:
factorial(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
This is the current code for my factorial function:
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))
and the output that this code produces is the following:
fact(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
As you see, the execution of these two functions gives me correct answers, but I just wanted to simplify the two functions to a single recursive function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我们可以将这两个函数组合成这个单个递归函数:
We can combine the two functions to this single recursive function:
2行代码:
测试一下:
结果:
2 lines of code:
Test it:
Result:
简短的:
a short one:
试试这个:
我注意到的一件事是,对于 n<1,您将返回“1”,这意味着即使对于负数,您的函数也会返回 1。您可能想解决这个问题。
try this:
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.
我没有使用Python的经验,但是像这样的东西?
I've no experience with Python, but something like this?
这是作业吗?
请阅读 PEP227 了解更多详细信息。缺点是 Python 允许您在函数内定义函数。
Is this homework by any chance?
Give PEP227 a read for more details. The short of it is that Python lets you define functions within functions.
再来一张
One more
并首次使用递归和 while 循环计算阶乘。
虽然 TrebledJ 在关于使用
if
的注释中写的选项更好。因为while
循环比if
执行更多操作(SETUP_LOOP、POP_BLOCK
)。该功能较慢。timeit -n 10000 -r 10
while
每个循环 836 µs ± 11.8 µsif
每个循环 787 µs ± 7.22 µsAnd for the first time calculate the factorial using recursive and the while loop.
Although the option that TrebledJ wrote in the comments about using
if
is better. Becausewhile
loop performs more operations (SETUP_LOOP, POP_BLOCK
) thanif
. The function is slower.timeit -n 10000 -r 10
while
836 µs ± 11.8 µs per loopif
787 µs ± 7.22 µs per loop可以使用这4行代码...
Can use these 4 lines of code...
我真的不知道负数的阶乘,但这适用于所有 n >= 0:
I don't really know the factorial of negative numbers, but this will work with all n >= 0:
在 Python 3.8 中,您可以尝试阶乘函数本身。
例如:
In Python 3.8 you can try factorial function itself.
For example:
递归函数中总是存在某种循环和一些停止循环的停止代码:
如您所见,满足
if
条件会导致实际结束“循环”的代码,这是递归函数中最重要的部分。相反,条件的 else 部分会导致再次调用 recursivefactorial 函数,这实际上是一种循环。There is always some kind of a loop in recursive functions and some stoping codes which stop the loop:
As you can see the fulfilling the
if
condition leads to the code that actually ends the "loop" and this is the most important part of a recursive function. In contrast, theelse
part of the condition leads to callingrecursivefactorial
function once again which is effectively a kind of loop.再来一张 =)
One more =)