![]() ![]() format(source, destination, auxiliary)) #Recursively asking to move remaining disk from #auxiliary to destination using source. TOH(source, destination, auxiliary, numOfDisk-1) #Moving the disk from source to destination print(“Move 1 disk from. if numOfDisk > 0: #Recursively calling for moving the n-1 disk from source #to auxiliary using destination. Python program for Solving Tower of Hanoi is - def TOH(source, auxiliary, destination, numOfDisk): #Base case of Recursion that when there is no disk to move #then terminate the call. And then last, the n-1 disk will be moved from the auxiliary tower to the destination tower.Īnd these n-1 moves are called recursively such that individual function call is responsible to handle the 1 disk move in each call.Then the nth disk will be moved from the source tower to the destination tower.For any n number of disks, firstly the n-1 disk will be moved from source to auxiliary.Like we have analyzed above for the 4 disk solution, TOH( auxiliary, source, destination, numOfDisk ).Move the disk from source to destination using auxiliary.TOH( source, destination, auxiliary, numOfDisk ).TOH( source, auxiliary, destination, numOfDisk) Move 1 disk from A to C using B.Īfter analyzing these 2 examples of solving the tower of Hanoi with 4 and 5 disks, the algorithm for n number of disks will be. Output - Move 1 disk from A to C using B. This means if the disk with size 2 then it can’t be put on disk with size 1, but it can be put on any other disk that is larger with that disk. No Disk should overlay its smaller disk.In every step, only one disk is allowed to move.Rules for Solving the Tower of Hanoi Problem. If the base case is not present then it will never terminate. There is a base case in the recursion that helps to terminate the recursion. What is Recursion? In terms of programming, A recursion is a function that calls itself. But using the help of recursion it becomes very easy to solve. ![]() This mathematical puzzle is supposed to be unsolvable. And the other version of the story is about the place (Hanoi) in Vietnam. They used to believe that once all 64 disks are successfully moved then the world is going to end. And priests are trying to rearrange that disk on another tower from many years ago. It is being said that in Kashi Vishwanath Temple in India, there is a stack of 64 golden disks that are kept sequentially on top of another. And that is related to the priests of the temple. This problem is believed to be unsolvable. And this is also called the Tower of Brahmas or Lucas Tower. This problem was related to Hindu Mythology. This problem was created by French mathematician Édouard Lucas in the 19th century.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |