7. Recursion

Hello everyone, I am a pig arched by cabbage.

  • What is recursion?
    Dream in Dream Space, Dream in Dream, Dream in Dream, see whether MMP is a dream or reality in the end. Recursion can also be understood as dreaming, and the premise of dreaming must be based on the original. Therefore, we call the programming technique of calling the program itself recursively .
  • Fibonacci sequence
    We can use recursion to solve the Fibonacci sequence problem.
    1 1 2 3 5 8 13 21 34 ... the current number is equal to the sum of the first two numbers
long 获取斐波那契数(long index) {
		if (index == 1 || index == 2) {
			return 1L;
		} else {
			return 获取斐波那契数(index - 1) + 获取斐波那契数(index - 2);
		}
	}

For ease of understanding, the name of the method here is expressed in Chinese "get the Fibonacci number (long index)". At this time, the method is called again in the method, which is the idea of ​​using recursion.

  • Summary
    Mathematical induction has been learned in high school, and tells you that a1 = xx is established. Assuming that n = k is true, see if n = k + 1 is true. The same is true for recursion, all starting from the first level and then looking for the relationship between the second level and the second level, and so on.
    Therefore, each execution must start from the first level. When the number of operations is large, the time to get the result will increase geometrically.
    This reminds us of the lotus theorem.
    A lotus pond, lotus blooms are very few on the first day, and the second The number of open days is twice that of the first day, and on each subsequent day, the lotus will open twice the amount of the previous day.
    If on the 30th day, the lotus blooms to fill the entire pond, then I would like to ask: On the first few days, the lotus bloomed in half?
    Day 15?
    wrong! It is the 29th day.

Many people ’s lives are like lotus flowers in the pond. At the beginning, they bloomed vigorously and furiously ...
But gradually, you started to feel boring or even bored. You may give up on the 9th, 19th, and 29th days. Insisted.
At this time, it is often only one step away from success.
At the end, it is not luck and cleverness, but perseverance.

I don't know how the two are related ...

Published 24 original articles · praised 4 · visits 2038

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/96721919