
#HANOI TOWERS PUZZLE CODE#
“I think that is right, but you can code it up in your favorite language”, said Melvin. Melvin grabbed a paper napkin and wrote a quick block of pseudo-code: I wish I could remember the code for it.” “Nah!” said Bugsy, “Once you see the pattern it solves itself. “Wow!" said Melvin, “that was fast! I am surprised.” He put the original stack back in place and quickly shuffled the disks into place. You then do the same thing to get the biggest remaining disk to the destination. That leaves you with a stack of (n-1) disks. “Do you see the pattern? You move the biggest disk to the destination peg and forget about it it is out of the game. “Try again,” said Bugsy, resetting the disks. Unfortunately, her first move was to put the smallest disk on the middle peg and she had to make a lot of extra moves to recover. Jane gulped her sandwich down and started moving the disks. Now, Jane, you try it for three disks.”Īnimated GIF solution for four disks by André Karwath He then put two disks on the first peg “This is for two disks top disk goes to the middle peg, the bottom disk goes to the end, and finally we put the smallest disk on the top of the destination stack. “This is the case for one disk and yes it is easy,” said Melvin.

Melvin took the disks off the peg, held up the smallest one and then put it on the first peg. “ I have seen those dolls at a toy store, but I don't get it,” replied Jane. “No, Matryoshkas are those Russian nested dolls that are identical and get smaller as you unnest them until you get to the last tiny doll.” “I don't know her what department does May Troyski work in?” said Jane between bites of her sandwich. “Recursion is a programming and mathematical technique where you solve a problem by reducing it down to smaller and smaller versions of itself, like Matryoshka dolls.” “This puzzle was popular for teaching recursion in computer science classes when I was in school”, said Melvin. and you can move only one disk at a time, with the restriction that the disks on any peg cannot sit on a smaller disk.”

Ignoring this, Bugsy turned the puzzle back to its original position, “. Jane grabbed the stack of disks and put them on the final peg. First, you have to move the disks from peg to peg and leave the base where it is. “The goal is to move all the disks from the left side to the right side.” Before he could finish, Jane spun the base of the puzzle and announced, “Done! That is too easy!” “It's a classic puzzle”, said Melvin Frammis.

The disks had a hole in their center like a Chinese coin and they got smaller toward the top of the peg. It was a board with three pegs in a row, and stack of disks on one of the end pegs.

Jane Smith from Marketing sat down at the company lunch table, pulled her sandwich from a brown paper bag and picked up the toy. Bugsy Cottman, a junior programmer at International Storm Door & Software, looked up from his lunch, and said “ Yep, I was looking for my old Rubik's Cube and I found this.” “Bugsy, is that a Towers of Hanoi puzzle?” asked Melvin Frammis.
#HANOI TOWERS PUZZLE SOFTWARE#
This puzzle is one of the Sharpen Your Coding Skills series which introduces Melvin and Bugsy and the rest of the programming staff at International Storm Door and Software write beautiful code in whatever programming language they have to hand. But before that, he was an honest developer obsessed with finding good algorithms and clean code. Joe Celko is best known as the database expert who writes books on SQL, data and databases. Here you are challenged to find solutions to some variations, after first explaining the original version. Towers of Hanoi is a classic puzzle and is often used to illustrate the idea of recursion.
