In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the recursive algorithm in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Recursion is a kind of abstract mathematical logic, which can be simply understood as "the program calls its own algorithm".
Wikipedia's explanation of recursion is:
Recursion (English: Recursion), also translated as recursion, in mathematics and computer science, refers to the method of using the function itself in the definition of a function. The term recursion is also more commonly used to describe the process of repeating things in a self-similar way.
For example, when two mirrors are approximately parallel to each other, the images nested in the mirror appear in the form of infinite recursion. It can also be understood as the process of self-replication.
"delivery" means delivery, and "return" means return. First, a method is passed on layer by layer, and then it is passed to the last layer and then the result is returned.
For example, when I stood in line for nucleic acid testing, there were 100 people in front of me. I wanted to ask the medical staff what time the medical staff would get off work, so I asked the brother in front of me, and he asked the people in front of him, passed it on one by one, and finally passed it on to the medical staff. reply that you will get off work at six o'clock in the afternoon. This sentence was passed back to me at last, and I knew that the medical staff were off duty at six o'clock.
This process is a recursive process, if "message" itself is a method, then the whole message process is to call its own method, and finally get the result.
This is not like a cycle, which is equivalent to wearing headphones for everyone, and then an "intermediary" asks you one by one if you know when the medical staff get off work, and when you ask the medical staff, you get the answer, and the "intermediary" tells me to get off work at six o'clock.
In essence, recursion is to constantly disassemble a big problem, just like peeling onions, and finally disassemble it to the minimum level, which will return the result of solving the problem.
Use Python to give the simplest example of a recursive function to talk about what is a recursive application.
We often see functions call themselves to implement loop operations, such as factorial functions.
The factorial of the integer n is n * (nmur1) * (nmur2) *.
For example, the following 5 lines of Python code can realize the calculation of factorial.
Def fact (n):''n represents the factorial of the required number''if n factorial 1: return n n = n*fact (n Mel 1) return n print (factorial (5))
Output:
one hundred and twenty
Many people may wonder why the computational logic in the fact function calls itself and finally gets the result.
We can deduce it according to mathematical logic:
The factorial of the integer n is: fact (n) = n * (n Mel 1) *.
The factorial of integer nmur1 is: fact (nmur1) = (nmur1) * (nmur2) *.
So it can be inferred that fact (n) = n*fact (nmur1)
Is there a fact method that can be called for each number, and the factorial of the result n is returned when it is finally called to n? 1.
If you look at the image above, the recursive function will be called down one layer at a time, and finally return the result upwards when it comes to Number1.
This is the whole process of recursion, and if we give an accurate definition of recursion, it can be summarized as follows:
1. There is at least one explicit recursive end condition.
2. Give the treatment of recursive termination.
3. Each time you enter a deeper level of recursion, the problem size (amount of computation) should be reduced compared with the last recursion.
Take the above code as an example:
Def factorial (n):''n represents the factorial of the required number''if n termination 1: # 1, clear recursive termination conditions; return n # 2, the method of recursive termination n = n*factorial (n Mel 1) # pass return n # back
In addition to the common factorial cases, there is also the Fibonacci series, which is also a classical use of recursion.
Fibonacci series: 1, 1, 1, 2, 3, 5, 5, 8, 13, 21, 34, 55, 89.
This series starts with the third term, and each term is equal to the sum of the first two terms.
It is defined by the following recursive method: F (0) = 0 ∈ F (1) = 1 ∈ F (n) = F (n-1) + F (n-2)
In Python, we can use recursive functions to implement the Fibonacci sequence:
# 1Magol 1, 2, 3, 5, 8, 13, 21, 34, 55, try to judge which number is the 12th number in the series? Def fab (n):''n is the Fibonacci sequence''if n
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.