Home > Backend Development > Python Tutorial > Tower of Hanoi and Fibonacci sequence implemented in Python based on recursive algorithm

Tower of Hanoi and Fibonacci sequence implemented in Python based on recursive algorithm

不言
Release: 2018-04-18 14:28:11
Original
2570 people have browsed it

This article mainly introduces the Tower of Hanoi and the Fibonacci sequence implemented by Python based on the recursive algorithm. It analyzes the recursive implementation techniques of the Tower of Hanoi and the Fibonacci sequence in the form of examples. Friends in need can refer to the following

This article describes the Tower of Hanoi and Fibonacci sequences implemented in Python based on recursive algorithms. Share it with everyone for your reference, the details are as follows:

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

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

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

① Using while loop, the 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
Copy after login


The running 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 )

, the python2 code is as follows:

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


The running result is 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 best expresses computational thinking. Let’s take f(4) as an example and look at the execution process of recursion:

Same program Although the program using recursion is simple, the execution efficiency of recursion is lower than that of loop, and the system resource consumption is larger than that of loop. Because recursion is called layer by layer, and then returned layer by layer after completion, the execution efficiency of recursion is not high. So why use recursion? Because there are some problems, we can't find a very obvious loop solution, but it's easy to find an obvious recursive solution. Take for example the famous Tower of Hanoi problem.

2. Tower of HanoiThe picture below is a simplified version of the Tower of Hanoi game, with only 4 plates:

The rules of the Tower of Hanoi game are as follows:

The python2 code is as follows:

def hanoi(a,b,c,n):
  if n==1:#递归结束条件
    print a,&#39;->&#39;,c
  else:
    hanoi(a,c,b,n-1)
    print a,&#39;->&#39;,c
    hanoi(b,a,c,n-1)
Copy after login


Run 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

#Related recommendations:

Neural network (BP) algorithm Python implementation and application

The above is the detailed content of Tower of Hanoi and Fibonacci sequence implemented in Python based on recursive algorithm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template