简体   繁体   English

Python中的此递归函数如何工作?

[英]How does this recursive function in Python work?

I am learning python from the Book: "ThinkPython". 我正在从《 ThinkPython》这本书中学习python。

At page 56 (Chapter 6. Fruitful functions) there is a recursive function that calculates the factorial of any number. 在第56页(第6章,功能丰富的函数)中,有一个递归函数可计算任意数量的阶乘。 It does work, however I don't understand why. 它确实有效,但是我不明白为什么。 This is the code: 这是代码:

def factorial(n):
    if n == 0:
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        return result

Let's say I try with 3, I guess this is what should happen: 假设我尝试3,我猜这应该发生:

  1. enters the factorial function and n=3 输入阶乘函数,n = 3
  2. enters the else statement because n is not 0 输入nose语句,因为n不为0
  3. and here goes back to the beginning to step 1 with n = 2 然后回到n = 2的第一步

so it should do the same until n = 0 and return 1 (it will never get to the last lines). 因此它应该执行相同的操作直到n = 0并返回1 (它永远不会到达最后一行)。

Your function is: 您的功能是:

def factorial(n):
 if n == 0:
  return 1
 else:
  recurse = factorial(n-1)
  result = n * recurse
  return result

Let's go through an execution of this function step-by-step, starting with n = 3. 让我们从n = 3开始逐步执​​行此函数。

Entering 
STATE 1 
n = 3 
recurse = factorial(3-1)

STATE 2
n = 2
recurse = factorial(2-1)

STATE 3
n = 1
recurse = factorial(1-1)

STATE 4
You now have an n = 0, so you return 1 to STATE 3.

STATE 3
n = 1
recurse = 1
result = 1
return result to STATE 2

STATE 2
n = 2
recurse = 1
result = 2 * 1 = 2
return result to STATE 1

STATE 1
n = 3
recurse = 2
result = 3 * 2 = 6

And then 6, the factorial of 3, is returned to the calling function :D 然后6的阶乘3返回到调用函数:D

Recursion really is a tricky thing to confront, it just takes time to get used to it and to the different ways that it can be used. 递归确实是一件棘手的事情,它需要时间来适应它以及使用它的不同方式。

You're correct, as far as you go. 就您而言,您是正确的。 However, you don't continue far enough. 但是,您做得还不够。 When the factorial(0) call returns, where does it return to? 当阶乘(0)调用返回时,它返回到哪里? it returns to the 它返回到

recurse = factorial (n - 1)

line where n = 1 , and then you continue on from there. n = 1 ,然后从那里继续。 So, recurse = 1 , result = 1* 1 = 1 , and returns 1. Again, that goes to the recurse = line in the n=2 case.so, result = 2*1 = 2 , and returns that, to the recurse = line where n = 3, so result = 2 * 3 = 6 , which is what gets returned. 因此, recurse = 1result = 1* 1 = 1 ,并返回1。同样,在n = 2 case.so中,该行进入recurse =行。因此, result = 2*1 = 2 ,然后将其返回给recurse =行,其中n = 3,所以result = 2 * 3 = 6 ,这就是返回的结果。

The factorial of n is defined as n * (n-1) * (n-2) etc until 1 n的阶乘定义为n * (n-1) * (n-2)等,直到1

So for n = 5 -> 5*4*3*2*1 所以对于n = 5 -> 5*4*3*2*1

In the code the factorial is defined as n * whatever the factorial of n-1 is 在代码中,阶乘被定义为n * whatever the factorial of n-1 is的阶乘是n * whatever the factorial of n-1 is

Python will find out this last part by asking itself: what is the factorial of n-1 ? Python会问自己:这是n-1的阶乘是什么? Well it is (n-1) * whatever the factorial of (n-1)-1 is. 好吧,它是(n-1) * whatever the factorial of (n-1)-1

Do you see the loop/ recursion? 您看到循环/递归了吗?

Since for n==0 the factorial will return 1 , python can ask itself over and over again what the recursive part is, until it asks itself, what is the factorial of 0? 由于对于n==0 ,阶乘将返回1 ,因此python可以一遍又一遍地问自己递归部分是什么,直到它问自己, what is the factorial of 0? . This part is hardcoded in the first part. 这一部分在第一部分中进行了硬编码。

Once it enters this last part, it knows the other answers too! 一旦进入最后一部分,它也知道其他答案! For n == 5 : 对于n == 5

  • The factorial is 5 * (factorial of 4) 阶乘为5 *(阶乘为4)
    • The factorial is 4 * (factorial of 3) 阶乘为4 *(阶乘为3)
      • ... ...
        • The factorial of 0 is 1. 0的阶乘为1。

The factorial of 5 = 5*4*3*2*1 5的阶乘= 5 * 4 * 3 * 2 * 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM