Python basic series - recursive function cognition

Table of contents

1. Recursive function

1. Concept

2. Features

3. Personal summary 

2. Practice finding the factorial of any number n

1. Common method

2. Recursive method

 3. Tail recursive method

3. Fibonacci sequence practice

1. Recursive writing


1. Recursive function

1. Concept

        Recursive function: Calling your own function is a recursive function
        . It goes back and forth, and it goes back.

2. Features

When the function is running, it needs memory to open up space. This space is called stack frame space.

Recursion:
(1) The process of going is to continuously open up the stack frame space. When returning, it is to continuously release the stack frame space. The recursive
function is a complete process of continuously opening and releasing the stack frame space
( 2) There are two trigger mechanisms when returning. Either the last layer of function space is fully executed, or it will hit the bottom and rebound (back to the carbine). (3) When writing a recursive function, you must give a jump-
out Conditions, if there are too many layers of recursion, it is not recommended to use, it is easy to memory overflow or blue screen
(4) each layer of recursive call space is an independent individual, an independent copy, resources are not shared, and value transfer can be completed through return

     Officially, the maximum depth of recursion is 1000 layers, specifically according to the machine

def deepfunc():
    deepfunc()
    
报错:
[Previous line repeated 996 more times]

3. Personal summary 

Recursion is a process in which a function calls itself. Once recursion is found, it is necessary to consider the process of returning; the process of recursion is only to find the final value or return, and does not output any results. The result of our operation is mainly the value returned by the process of recursion; It's like stopping the echo in a deep valley.

2. Practice finding the factorial of any number n

1. Common method

def func(n):
	total = 1
	for i in range(1,n+1):
		total *= i
		"""
		total = total * i => 1 * 1
		total = total * i => 1 * 1 * 2
		total = total * i => 1 * 1 * 2 * 3
		total = total * i => 1 * 1 * 2 * 3 * 4 
		total = total * i => 1 * 1 * 2 * 3 * 4 * 5
		"""
	return total
res = func(5) 
print(res)

2. Recursive method

def func(n):
	if n <= 1:
		return 1
	return n * func(n - 1)

res = func(5)
print(res)
# 代码解析:
# 去的过程
n = 5  return 5 * func(5 - 1) => 5*func(4)
n = 4  return 4 * func(4 - 1) => 4*func(3)
n = 3  return 3 * func(3 - 1) => 3*func(2)
n = 2  return 2 * func(2 - 1) => 2*func(1)
n = 1  return 1

# 回的过程
n = 2  return 2*func(1) => 2*1       [func(2)]
n = 3  return 3*func(2) => 3*2*1     [func(3)]
n = 4  return 4*func(3) => 4*3*2*1   [func(4)]
n = 5  return 5*func(4) => 5*4*3*2*1 [func(5)]
res = func(5) <=> 5*4*3*2*1 = 120
"""

# 通俗点
看着这个式子,n=5时,不满足<=1,跳到func(n-1)中,开始计算func(4),依旧不满足,直到n=1,才可以直接return 1;
到达终点后开始反弹,回到2,得出return 2 * 1,回到3,得出3 * func(2)也就是3*2*1,以此类推

 3. Tail recursive method

Recursion is to open up a space for each layer. Tail recursion is a special recursion. It is characterized by opening up directly on the original space. No matter how many times the function is called, it only takes up one space. Benefits: You only need to consider the result of the last layer of space
. There is no need to consider the return process;
Note: Tail recursion needs to put the value into the parameter operation

def jiecheng(n,endval=1):
	if n <= 1:
		return endval	
	return jiecheng(n-1, endval*n)
print(jiecheng(5))
# 代码解析:
# 去的过程
n = 5,endval = 1  
	return jiecheng(5-1,endval*n ) => return jiecheng(4,1*5)
n = 4,endval = 1*5
	return jiecheng(4-1,endval*n ) => return jiecheng(3,1*5*4)
n = 3,endval = 1*5*4
	return jiecheng(3-1,endval*n ) => return jiecheng(2,1*5*4*3)
n = 2,endval = 1*5*4*3
	return jiecheng(2-1,endval*n ) => return jiecheng(1,1*5*4*3*2)
n = 1,endval = 1*5*4*3*2
	if 1 <= 1  条件满足 return endval => return 1*5*4*3*2
	
# 回的过程
n = 2  return 1*5*4*3*2
n = 3  return 1*5*4*3*2
n = 4  return 1*5*4*3*2
n = 5  return 1*5*4*3*2
到此程序全部结束;
"""

为什么尾递归不需要考虑回的过程呢,因为函数目的是return endval,而endval的值已经在计算过程中出来了,即使返回上一层,程序会默认目的已达成,继续返回。
为什么要参数默认写死endval=1呢,因为阶乘就是1*2*。。。这种格式,我们需要给一个初始值,意味着这个1不是固定的,是根据实际情况来,写死也是为了用户只关注n的值即可

3. Fibonacci sequence practice

1. Recursive writing

def feb(n):
	if n ==1 or n == 2:
		return 1
	# 当前值n = 上一个值(n-1) + 上上个值(n-2)
	return feb(n-1) + feb(n-2)

res = feb(5)
print(res)

#代码解析:
斐波那契数列的定义是:第一个数为1,第二个数为1,从第三个数开始,每个数均为前两个数之和。

n = 5 
return feb(4) + feb(3)
       feb(3) + feb(2) + feb(3)
       feb(2) + feb(1) + feb(2) + feb(3)
       feb(2) + feb(1) + feb(2) + feb(2) + feb(1)
       1+1+1+1+1=5

首先,我们将函数调用 feb(5) 传入参数5。
在函数内部,我们首先判断n的值是否等于1或2。由于5不等于1或2,所以我们将跳过这个条件语句。

接下来,我们将执行这行代码 return feb(n-1) + feb(n-2),由于n等于5,因
此时我们需要求出斐波那契数列的第5个数。根据斐波那契数列的定义,第5个数应该是前两个数之和,即第4个数和第3个数之和。

然后,我们需要计算 feb(4) 和 feb(3) 的值。首先,我们将调用 feb(4)。
在 feb(4) 函数内部,我们也需要计算斐波那契数列的第4个数,即第3个数和第2个数之和。因此,我们需要调用 feb(3) 和 feb(2) 来计算这个值。

在 feb(3) 函数内部,我们需要计算斐波那
那契数列的第3个数,即第2个数和第1个数之和。因此,我们需要调用 feb(2) 和 feb(1) 来计算这个值。



在 feb(2) 函数内部,由于 n 的值等于2,因此我们将直接返回1。

在 feb(1) 函数内部,由于 n 的值等于1,因此我们也将直接返回1。

现在,我们已经得到了 feb(2) 和 feb(1) 的值,因此我们可以计算 feb(3) 的值。根据斐波那契数列的定义,第3个数应该是前两个数之和,即1+1=2。因此,feb(3) 的返回值为2。

接下来,我们已经得到了 feb(3) 和 feb(2) 的值,因此我们可以计算 feb(4) 的值。根据斐波那契数列的定义,第4个数应该是前两个数之和,即2+1=3。因此,feb(4) 的返回值为3。

现在,我们已经得到了 feb(4) 和 feb(3) 的值,因此我们可以计算 feb(5) 的值。根据斐波那契数列的定义,第5个数应该是前两个数之和,即3+2=5。因此,feb(5) 的返回值为5。

最后,我们在主函数中将 feb(5) 的返回值打印出来,结果为5。
因此,这段代码的执行过程可以总结如下:

调用 feb(5) 函数来计算斐波那契数列的第5个数。

feb(5) 函数内部调用 feb(4) 和 feb(3) 函数来计算斐波那契数列的第4个数和第3个数。

feb(4) 函数内部调用 feb(3) 和 feb(2) 函数来计算斐波那契数列的第3个数和第2个数。

feb(3) 函数内部调用 feb(2) 和 feb(1) 函数来计算斐波那契数列的第2个数和第1个数。

feb(2) 函数内部直接返回值1。

feb(1) 函数内部直接返回值1。

feb(3) 函数内部得到 feb(2) 和 feb(1) 的返回值后,计算斐波那契数列的第3个数为2。

feb(4) 函数内部得到 feb(3) 和 feb(2) 的返回值后,计算斐波那契数列的第4个数为3。

feb(5) 函数内部得到 feb(4) 和 feb(3) 的返回值后,计算斐波那契数列的第5个数为5。

主函数将 feb(5) 的返回值打印出来,结果为5。

Guess you like

Origin blog.csdn.net/weixin_39855998/article/details/130119487