Tower of Hanoi Python Program

If you’re looking for the Tower of Hanoi python program then you’ve reached the right place. In this blog, we will see the python program.

History of Tower of Hanoi

The Tower of Hanoi is also known as the Tower of Brahma or the Indian Puzzle.

The earliest European references to the puzzle are in the writings of the French mathematician Cladis in 1883 and German mathematician Ernst Eduard Kummer in 1885.

The puzzle was popularized in the West by the French mathematician Edouard Lucas in 1883.

The Tower of Hanoi has been used as a mathematical model for studying the dynamics of physical systems and has been applied to the study of artificial intelligence.

The game can be played with any number of disks, but the most popular version is with three disks.

Now, let’s first understand the problem statement.

Problem statement:

The Tower of Hanoi problem is a mathematical game or puzzle. It consists of three rods and a number of disks of different sizes, which can slide onto any rod. The puzzle starts with the disks in a stack in ascending order of size on one rod, the largest at the bottom, thus making a conical shape.

The objective of the problem is to move the entire stack to another rod, obeying the following simple rules:

1. Only one disk can be moved at a time.

2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.

3. No larger disk can be placed on top of a smaller disk.

Tower of Hanoi python program

#Tower of Hanoi python program

def TowerOfHanoi(n , source, destination, auxiliary): 

    if n==1: 

        print ("Move disk 1 from source",source,"to destination",destination)
        return

    TowerOfHanoi(n-1,source,auxiliary, destination)

    print("Move disk",n,"from source",source,"to destination",destination)

    TowerOfHanoi(n-1,auxiliary,destination,source)

n=3    
        
TowerOfHanoi(n, 'A', 'B', 'C')

#Tower of Hanoi python program

Leave a Comment