Recursive functions and recursion times are limited
Sum up :sum=n+n(n-1)+…+1
Find the factorial :n!=1x2x3…xn
The solution is to modify the number of recursions
How to control the number of recursion
The first one is
The second kind
The third kind of
Recursive functions and recursion times are limitedA function calls itself internally , So this function is a recursive function . Recursion repeats itself , Every recursion , The closer to the final value . When a problem can be solved by many similar small problems , Consider using recursive functions . As recursion deepens , The scale of the problem should be smaller than last time .return The method of the function itself ensures the continuation of recursion , But if there is no clear end condition , Recursion goes on indefinitely . So when the problem is solved , You should tell the function to end recursion , This requires clear end conditions .
Two common examples of recursion : Sum up 、 Find the factorial n!
Sum up :sum=n+n(n-1)+…+1def sum(n): if n > 0: return n+sum(n-1) else: return 0 new_sum = sum(10)print(new_sum)#output55
Find the factorial :n!=1x2x3…xndef factorial(n): if n == 1: return 1 else: return n*factorial(n-1)new_sum = factorial(5)print(new_sum)#output120
When using recursive functions, you should note that the default limit for the number of recursions is 1000, If the recursion times are too many, it will lead to stack overflow
such as
def sum(n): if n > 0: return 1+sum(n-1) else: return 0new_sum = sum(1000)print(new_sum)
Will be submitted to the RecursionError: maximum recursion depth exceeded in comparison Error of
The solution is to modify the number of recursionsimport syssys.setrecursionlimit(1500)# The number of recursion can be modified to 1500def sum(n): if n > 0: return 1+sum(n-1) else: return 0new_sum = sum(1000)print(new_sum)#output1000
When modifying the recursion times, you should pay attention to , Modify the recursion times to 1500, Recursion depth cannot reach 1500, stay 1400 about . The default recursion number is 1000, The depth of recursion is not enough 1000, To 992 about
How to control the number of recursionRecursion is often used , Although it can solve many problems , But its disadvantages are obvious , May not be able to jump out of the loop , This can be avoided by controlling the number of recursions .
use lua Tried several ways ,
The first one isDefine a variable count in the method :
function recursionTest() local times = 0 if times < 10 then times = times + 1 recursionTest() end if times == 0 then print("outer round") endend
Carry it out , There is no limit to it , Because local variables are reset to each time 0.
The second kindlocal recurTimes = 0function recursionTest2() if recurTimes < 10 then recurTimes = recurTimes + 1 recursionTest2() endend
This method can control the number of times , But variables need to be defined outside the method , Before executing the function, you need to set this variable to 0, You need to outsource recursive methods , More complicated .
The third kind offunction recursion(str, t) str = str or "first time " t = t or 0 t = t + 1 print(str, t) if t < 10 then recursion("times:", t) end if t == 1 then print("outer round") endend
Pass in a self increasing variable during recursion , Stop recursion when the threshold is reached , There is no need to pass parameters when executing the outermost layer , The default value is 0, And according to t The value of determines the current recursion level , At the end of recursion , Do other things before the outermost layer is finished , Kill two birds with one stone .
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .