Some time ago, a small partner left a message and wanted to know something about Python recursive functions. Recently, Xiaobian has also made up some contents in this respect. To tell you the truth, the cognition process of recursion can really let us explore some interesting contents. Today, let’s explore. < / P > < p > about recursion, there are a lot of descriptive materials on the Internet, which are very professional and detailed. If you are interested, you can search it. However, in terms of our own understanding, it is the way of “calling ourselves”; then, it is easy to understand that recursive functions call functions themselves directly or indirectly in the process of function implementation. < p > < p > as we know, there is a famous mathematician in foreign countries. When he was 9 years old, he used a very short time to calculate the sum of natural numbers from 1 to 100. < p > < p > his method is to sum the sequence of 50 pairs of construction and 101, and get the result: 5050. < p > < p > < p > “the prince of mathematics” is not an empty name. His way of thinking at the age of 9 is really unique. In mathematics, there are 110 achievements named after his name “Gauss”, which is the most outstanding among mathematicians. < / P > < p > let’s not discuss this today. Let’s go back to the main topic. How can we use recursion to solve this problem? As ordinary people, we should think about this question in the following way: < / P > < p > define the scope! Don’t be intimidated by the parameter n, it’s not that terrible, it just represents a number. This limit is the condition for the beginning and end of recursion. < / P > < p > determines the equivalence of functions. The recursive process is actually a process of continuously reducing parameters. The ultimate goal of this step is to determine the relationship between parameter n and parameter n-1. This is how l Peter Deutsch, a “prodigy” who has been writing programs since he was 11, describes recursion as “iterative is human, recursive is God.”. This big cow raised the use of recursion to the height of “God”. As a wonderful way of thinking, recursion is always marvelous at its ability to describe problems and its simplicity in writing code. However, in practice, it is not easy to understand the essence of recursion and achieve flexible application. This is the real reason why Daniel raised recursion to the height of “God”. < / P > < p > the problem is clear, and the implementation is much easier! We will solve the problem of the sum of natural numbers within n, and define a function fn, where n is the maximum number to be solved. Then, there are two ways to solve the problem: < / P > < p > first, the big problem can be decomposed into a series of small problems solved by the same method. However, the solution of big problems needs a series of small problems as the premise; < / P > < p > case 1: peeling onion. I want to get the onion core. Don’t ask me why? The reality is that I can only start with the outermost layer of the onion, layer by layer. You peel onions the same way. < / P > < p > case 2: similar to the problem of Russian dolls. I want to get the innermost and smallest doll, I have to open the outermost doll first. And I use the same method every time I open the doll. < / P > < p > case 3: you line up at the gate of an online popular snack bar. It’s a long line with twists and turns. You can’t count the number of people in front of you. The person in front of you doesn’t know who he is, but he also wants to know. Assuming that all the people in front of you want to know who they are, they start from you and ask one by one. When the first one is asked, he starts from 1 First one by one feedback, know feedback to you, at this time, the people in front of you will know who they are. < / P > < p > these questions are easy to understand. What other classic programming cases are there? As a python beginner, we can also solve these problems by recursion, such as the definition of “tree”, the equal ratio and difference sequence, factorial, Fibonacci sequence and so on. Let’s take a few more complicated examples. < / P > < p > the features and some cases of recursion are explained above, but not every applicable problem of recursive method can be solved. It also has some limitations. < / P > < p > in the computer, function calls are implemented through the stack data structure. Every time a function call is entered, the stack will add a layer of stack frame, and every time the function returns, the stack will decrease one layer of stack frame. Because the size of the stack is not infinite, too many recursive calls will cause stack overflow. < / P > < p > in binary search, if the data to be searched is not in the list provided, when multiple searches reach the limit of the stack, the stack will overflow and an exception will be thrown. Today, I will pay more attention to the following content of < p > python. What other interesting recursive uses are there? Welcome to leave a message below! Fifth personality will be updated, please remember your game account, otherwise you may not be able to play normally