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.

2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.

3) No disk may be placed on top of a smaller disk.

Pseudo Code

TOH(n, x, y, z) { if (n >= 1) { // put (n-1) disk to z by using y TOH((n-1), x, z, y) // move larger disk to right place move:x-->y // put (n-1) disk to right place TOH((n-1), z, y, x) } }

Analysis of Recursion

Recursive Equation :

Solving it by BackSubstitution :

Put value of T(n-2) in equation–2 with help of equation-3

Put value of T(n-1) in equation-1 with help of equation-4

After Generalization :

Base condition T(0) == 1

n – k = 0

n = k;

put, k = n

It is GP series, and sum is

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program for Tower of Hanoi
- Recursive Tower of Hanoi using 4 pegs / rods
- Cost Based Tower of Hanoi
- Twisted Tower of Hanoi Problem
- Practice Questions on Time Complexity Analysis
- Complexity Analysis of Binary Search
- Complexity analysis of various operations of Binary Min Heap
- Analysis of Algorithms | Set 4 (Analysis of Loops)
- Analysis of Algorithm | Set 5 (Amortized Analysis Introduction)
- Analysis of Algorithms | Big-O analysis
- Analysis of Algorithms | Set 1 (Asymptotic Analysis)
- Time Complexity of building a heap
- An interesting time complexity question
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Time complexity of recursive Fibonacci program
- Python Code for time Complexity plot of Heap Sort
- C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
- Time Complexity of Loop with Powers
- A Time Complexity Question

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.