Example of Hanoi tower and Fibonacci sequence based on recursive algorithm in Python

@This article comes from the public number: csdn2299, if you like, you can pay attention to the public number programmer academy. The
example of this article describes Python's Hano tower and Fibonacci sequence based on the recursive algorithm. Share with you for your reference, as follows:

Here we use 2 examples to learn the use of recursion in python.

  1. Find the number in the Fibonacci sequence with subscript n (subscript counts from 0)

The form of the Fibonacci sequence is this: 0,1,1,2,3,5,8,13 ...

① Using while loop, python2 code is as follows:

def fib(n):
  a,b=0,1
  count=0
  while count<n:
    a,b=b,a+b
    count=count+1
  print a

The results are as follows:

>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5

② Use recursion (recursion must have boundary conditions), python2 code is as follows:

def fib(n):
  if n==0 or n==1:#递归的边界条件
    return n
  else:
    return fib(n-1)+fib(n-2)

The results are as follows:

>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5

Recursion is one of the algorithms that can best express computational thinking. Let's take f (4) as an example to look at the execution process of recursion: the Insert picture description here
same program, using recursion Although the program is concise, the execution efficiency of recursion is lower than that of the loop, and the system resources The consumption is greater than the cycle. Because recursion is called layer by layer, and returns layer by layer after the end, the execution efficiency of recursion is not high. Why use recursion? Because of some problems, we can't find a very obvious loop scheme, but it is easy to find an obvious recursive scheme. For example, the famous Hanoi Tower problem.

  1. Hanota

The following picture is a simplified version of the Hanoi Tower game, with only 4 plates: The Insert picture description here
Hanoi Tower game rules are as follows: Insert picture description here
Python2 code is as follows:

def hanoi(a,b,c,n):
  if n==1:#递归结束条件
    print a,'->',c
  else:
    hanoi(a,c,b,n-1)
    print a,'->',c
    hanoi(b,a,c,n-1)

operation result:

>>> hanoi('A','B','C',1)
A -> C
>>> hanoi('A','B','C',2)
A -> B
A -> C
B -> C
>>> hanoi('A','B','C',3)
A -> C
A -> B
C -> B
A -> C
B -> A
B -> C
A -> C

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] Want to be yourself, want to be a noble person, come on!

Published 40 original articles · praised 14 · 20,000+ views

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105497901