Tower of hanoi using recursion in python
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another
rod, obeying the following simple rules: Python3
Output Move disk 1 from source A to destination C Move disk 2 from source A to destination B Move disk 1 from source C to destination B Move disk 3 from source A to destination C Move disk 1 from source B to destination A Move disk 2 from source B to destination C Move disk 1 from source A to destination C Move disk 4 from source A to destination B Move disk 1 from source C to destination B Move disk 2 from source C to destination A Move disk 1 from source B to destination A Move disk 3 from source C to destination B Move disk 1 from source A to destination C Move disk 2 from source A to destination B Move disk 1 from source C to destination B Time Complexity: O(2n) Auxiliary Space: O(n) Please refer complete article on
Program for Tower of Hanoi for more details! In this tutorial, we will implement the famous Tower of Hanoi puzzle using the Python program. We will solve this problem by using recursion function. If you aren't familiar with the Recursion function concepts, visit our Python recursive function tutorial (https://www.javatpoint.com/python-factorial-number-using-recursion). What is the Tower of Hanoi?In 1883, the Tower of Hanoi mathematical puzzle was invented by the French mathematician Edouard Lucas. The inspiration came from a legend that states - In Ancient Hindu temple, this puzzle was presented to the young priest. The puzzle is, there are three poles, and 64 disks, and each disk is smaller than the other. To solve this problem, move all 64 disks from one of the three poles to another pole without violating the essential constraints. The disks can be moved one disk at a time and they should place a smaller disk top of a larger one. Other folktale states, when they would solve this puzzle, the temple would smash into dust, and the world would end. But it would take a lot of time because to solve this problem 264 - 1 moves are necessary i.e., 18,446,744,073,709,551,615 per second that is equal to 5, 84,942,417,355 years according to the rules. Rules of the gameThe rules of "Tower of Hanoi" are quite simple, but the solution is slightly hard. There are three rods. The disks are stacked in the descending order; the largest disk stacked at the bottom and the smallest one on top. The task is to transfer the disks from one source rod to another target rod. The following rule must not be violated
The number of moves can be calculated as 2n - 1. Solution:At the beginning of this tutorial, we have mentioned that we will use the recursive function to find the solution. Suppose we have three disks on the first rod; we need total 7 moves from the above formula. The most left rod is called SOURCE, and the rightmost rod is called TARGET. The middle rod is referred to as an AUX. The AUX is needed to deposit disks temporarily. Problem Approach
Python Program/ Source CodeOutput: Enter the number of disks: 3 Move disk 1 from rod A to rod C. Move disk 2 from rod A to rod B. Move disk 1 from rod C to rod B. Move disk 3 from rod A to rod C. Move disk 1 from rod B to rod A. Move disk 2 from rod B to rod C. Move disk 1 from rod A to rod C. Case - 2: According to the formula, the moves can be happened 2n - 1, so n = 3 took 7 moves and, n = 2 took 3 moves. Can Tower of Hanoi be solved using recursion?So there you go now , the above program can help you solve the Tower of Hanoi puzzle for any number of disks in C++ and Python using a recursive approach.
What is Tower of Hanoi problem in Python?Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time.
How do you make a Tower of Hanoi in Python?Python Program/ Source Code
Enter the number of disks: 3 Move disk 1 from rod A to rod C. Move disk 2 from rod A to rod B. Move disk 1 from rod C to rod B. Move disk 3 from rod A to rod C.
How does the Tower of Hanoi relate to recursion?In our Towers of Hanoi solution, we recurse on the largest disk to be moved. That is, we will write a recursive function that takes as a parameter the disk that is the largest disk in the tower we want to move.
|