Recursion: Tower of Hanoi

The idea of ​​recursion

  1. Recursion is divided into two processes: recursion and regression to
    find n of a number n! For example:
    Recursion: If passerby A can help me find (n-1)!, then I just need to multiply n on this basis, passerby A will think, if passerby B can help me find (n-2) ! Find out, then I just need to multiply (n-1) on this basis.
    If it goes on like this, there will always be one person whose job is to put only 1! Just find it out, then this is very simple, we can define 1!=1.
    Regression: When someone puts 1! =1 is calculated, it is time to start counting 2!, and then start 3! , know n! , this process is called regression.

  2. Recursion cannot be executed endlessly, there must be an end sign
    such as:
def recursion():
     recursion()

        This recursion is pointless because the condition for the recursion to complete is missing. An exception will occur when the compiler executes to the set maximum recursion times.

        In the previous example, define 1! =1 is the condition for the recursion to complete.

Second, a few examples of recursion

  1.    Find the factorial n of a number n!
    First of all, we can use the iterative method to complete it before learning recursion. The code is as follows:
def fact(n):
    result = n
    for i in range(1,n):
        result *= i
    return result
number = int(input('Please enter a number:'))
result = fact(number)
print("The factorial of %d is: %d"%(number,result))
        After learning recursion, you can use the recursive method, the code is as follows:
def fact_recursion(n):
    if n == 1:
        return 1
    return n*fact_recursion(n-1)
number = int(input('Please enter a number:'))
result = fact_recursion(number)
print("The factorial of %d is: %d"%(number,result)

2. Find the Fibonacci sequence

Note that two self-calls are used in the Fibonacci sequence, so two initial values ​​(or completion conditions) are required, namely: f(1)=f(2)=1

code show as below:

def febnaqie(n):
    if n == 1 or n == 2:
        return 1
    return febnaqie(n-1)+febnaqie(n-2)
for i in range(1,13):
    print(febnaqie(i))

In fact, the implementation of recursion is very time-consuming. When the number of recursive layers gradually increases, the time required for the calculation speed will become very long, and the iteration will be very time-saving:

def fibb(n):
    a,b = 0,1
    for i in range(1,n+1):
        a,b =b,a+b #Note that the assignment method used here is to perform the right-hand calculation first before assigning it, you cannot use a = b and then b = a+b
	print(a)
fiber (12)
1
1
2
3
5
8
13
21
34
55
89
144

3. Solve the Tower of Hanoi problem

code show as below:

def hanoi(n,x,y,z): #x is the starting position (column), y is the borrowed column, and z is the destination position
    if n == 1:
        Print(x,'-->',z)#The condition of the end, that is, the work done by the last person when the last step is recursive
    else:
        hanoi(n-1,x,z,y)#In the first step, someone moves n-1 discs from x to y column with the help of z
        print(x,'-->',z)#The second step, I move the last disk from the x column to the z column
        hanoi(n-1,y,x,z)#The third step, someone moves n-1 disks from the y column to the z column with the help of the x column
hanoi (3, 'x', 'y', 'z')

Note that the first and third steps in the code are all completed using functions defined by themselves.

The execution effect is shown as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326007106&siteId=291194637