Day19 recursive function

recursive function


Inside the function, you can call other functions. If a function calls itself within itself, this function is a recursive function.

For example, we compute the factorial n! = 1 x 2 x 3 x ... x n, a function fact(n)expressed, it can be seen:

fact(n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! x n = fact(n-1) x n

So, fact(n)it may be expressed as n x fact(n-1), n = 1, only require special handling.

So, fact(n)write recursive way is to use:

def fact(n):
    if n==1: return 1 return n * fact(n - 1) 

The above is a recursive function. You can try:

>>> fact(1)
1
>>> fact(5) 120 >>> fact(100) 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 

If we calculate fact(5), we can see the definition of a function is calculated as follows:

===> fact(5)
===> 5 * fact(4)
===> 5 * (4 * fact(3))
===> 5 * (4 * (3 * fact(2)))
===> 5 * (4 * (3 * (2 * fact(1))))
===> 5 * (4 * (3 * (2 * 1)))
===> 5 * (4 * (3 * 2))
===> 5 * (4 * 6)
===> 5 * 24
===> 120

Advantage is to define a simple recursive function, clear logic. In theory, all recursive functions can be written in a circular fashion, but not as good as recursive loop logic is clear.

Recursive function needs to be taken to avoid stack overflow. In the computer, the function call is achieved through the stack (Stack) This data structure, each time a function call into the stack a stack frame is increased by one, each time the function returns, the stack will be reduced one stack frame. As the stack size is not unlimited, so the number of recursive calls too, can cause a stack overflow. You can try fact(1000):

>>> fact(1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in fact ... File "<stdin>", line 4, in fact RuntimeError: maximum recursion depth exceeded in comparison 

Recursive call stack overflow is solved by tail-recursion optimization, tail recursion and in fact the effect of the cycle is the same, so the cycle as a special tail recursive function is also possible.

Tail recursion means that, when the function returns, calls itself itself, and, return statement can not contain expressions. In this way, the compiler or interpreter you can do to optimize tail recursion, the recursive call to itself, no matter how many times, only occupy a stack frame, stack overflow situation does not occur.

The above fact(n)function due to the return n * fact(n - 1)introduction of the multiplication expression, so it is not the tail-recursive. To change the tail-recursive manner, it requires a bit more code is mainly the product of each step should passed to a recursive function:

def fact(n):
    return fact_iter(n, 1) def fact_iter(num, product): if num == 1: return product return fact_iter(num - 1, num * product) 

It can be seen return fact_iter(num - 1, num * product)only return a recursive function itself, num - 1and num * productwill be evaluated before the function call does not affect the function call.

fact(5)Corresponding fact_iter(5, 1)call as follows:

===> fact_iter(5, 1)
===> fact_iter(4, 5)
===> fact_iter(3, 20) ===> fact_iter(2, 60) ===> fact_iter(1, 120) ===> 120 

When the tail-recursive calls, if optimized, the stack will not grow, and therefore, no matter how many times the call will not cause a stack overflow.

Unfortunately, most programming languages do not optimized for tail recursion, Python interpreter did not do optimization, so that even the above fact(n)functions into a tail-recursive manner, can lead to stack overflow.

summary

The advantage of using recursive function is simple and clear logic, the disadvantage is too deep can cause a stack overflow calls.

For tail recursion optimization of language prevents stack overflow via tail recursion. Tail-recursive loop and in fact are equivalent, there is no loop cycle programming language can only be achieved through the tail recursion.

Standard Python interpreter did not do tail recursion is optimized for any recursive function stack overflow problem exists.

Guess you like

Origin www.cnblogs.com/AaronY/p/12571838.html