recursion(1)

Introduction to recursion

Recursion is a method of solving problems. The idea is to decompose the problem into smaller-scale identical problems, continue to decompose, and know that the problem is small enough to be solved in a very simple and direct way.

In use, simply call yourself.

for example

Summation: such as seeking the sum of the list ls = [1, 3, 4, 7, 8]

Analysis: In the case of not using loops and enumerations, the addition of two numbers is the initial solution method from contact mathematics. The list ls contains multiple values, so how can it be converted into the addition of two values?

Decomposition process:
sum ( ls ) = sum ( ls [ : 4 ] ) + 8 sum(ls) = sum(ls[:4]) + 8sum(ls)=sum(ls[:4])+8
s u m ( l s [ : 4 ] ) = s u m ( l s [ : 3 ] ) + 7 sum(ls[:4]) = sum(ls[:3]) + 7 sum(ls[:4])=sum(ls[:3])+7
s u m ( l s [ : 3 ] ) = s u m ( l s [ : 2 ] ) + 4 sum(ls[:3]) = sum(ls[:2]) + 4 sum(ls[:3])=sum(ls[:2])+4
s u m ( l s [ : 2 ] ) = s u m ( l s [ : 1 ] ) + 3 sum(ls[:2]) = sum(ls[:1]) + 3 sum(ls[:2])=sum(ls[:1])+3
s u m ( l s [ : 1 ] ) = 1 sum(ls[:1]) = 1 sum(ls[:1])=1

This kind of logic is to recursively decompose the problem into the same small problem. After the decomposition is completed, then the calculation is carried forward. It is obvious that there is a cut-off condition for the decomposition problem. When there is only one value in the list (of course, the sum It doesn't need to be so complicated, just to explain the logic of the recursion).

代码

def get_sum(arr: list):
    if len(arr) == 1:
        return arr[0]
    value = arr.pop()
    _sum = get_sum(arr) + value
    return _sum


if __name__ == '__main__':
    ls = [1, 2, 3, 4, 5, 6]
    print(get_sum(ls))

When writing recursive code, you need to pay attention to two issues:

  • stop condition
  • repeating pattern

In the above formula, the stop condition is that when the length of the list is 1, the repetition rule is the last number of the current list, plus the values ​​of other lists (as a whole).

Fibonacci sequence

The Fibonacci sequence, also known as the golden section sequence, was introduced by the mathematician Leonardo Fibonacci with the example of rabbit breeding, so it is also called the "rabbit sequence". What is more is such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... In mathematics, the Fibonacci sequence is defined recursively as follows: F(0)= 0, F(1)=1, F(n)=F(n - 1)+F(n - 2) (n ≥ 2, n ∈ N*) in modern physics, quasi-crystal structure, chemistry, finance and other fields , the Fibonacci sequence has direct applications. For this reason, the American Mathematical Society has published a mathematical journal named "Fibonacci Sequence Quarterly" since 1963, which is used to publish research results in this area. .

analyze

Stop condition:
F(n)=F(n - 1)+F(n - 2), if n starts from 1, then n-1>=0, n-2>=0, all n>= 3, so the stop condition is n=1, 2 are 1.

Repetition law:
F(n)=F(n - 1)+F(n - 2)

the code

def fibonacci(n):
    if n in [1, 2]:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


if __name__ == '__main__':
    for i in range(1, 11):
        print(fibonacci(i), end=' ')

输出

1 1 2 3 5 8 13 21 34 55

Guess you like

Origin blog.csdn.net/Zeus_daifu/article/details/128435861