Let’s try to solve a puzzle – Tower of Hanoi using recursion. Return x*factorial(x-1) // break into smaller problem(s)ĭetailed explanation to Recursion can be found – Here Tower of Hanoi algorithm explained Let’s take a simple example and try to understand those.įollowing is the pseudo code of finding the factorial of a given number X X using recursion. Recursion is useful in solving problems which can be broken down into smaller problems of the same kind.īut when it comes to solving problems using Recursion there are several things to be taken care of. So it’s like there is a function called d r e a m ( ) dream(), and we are just calling it in itself. Leonardo had a dream, in that dream he had another dream, in that dream he had yet another dream, and that goes on. It will be easier for those who have seen the movie Inception. When a function calls itself, it’s called Recursion. No disk can be placed on top of the smaller disk.īefore we proceed, let’s understand Recursion – What is Recursion?.The objective of the puzzle is to move the stack to another peg following these simple rules. Tower of Hanoi consists of three pegs or towers with n disks placed one over the other. It is good to understand how recursive solutions are arrived at and how parameters for this recursion are implemented. Tower of Hanoi is one of the classic problems to look at if you want to learn recursion. Hence this puzzle is often called Tower of Brahma puzzle. These priests acting on the prophecy, follow the immutable rule by Lord Brahma of moving these disk one at a time. According to a prophecy, when the last move of the puzzle is completed the world will end. These disks are continuously moved by priests in the temple. There is a story about an ancient temple in India (Some say it’s in Vietnam – hence the name Hanoi) has a large room with three towers surrounded by 64 golden disks. Tower of Hanoi game is a puzzle invented by French mathematician Édouard Lucas in 1883.
0 Comments
Leave a Reply. |