Questions tagged [towers-of-hanoi]

A simple puzzle involving moving discs between rods. Commonly used as a programming kata/exercise and frequently set as homework when introducing the concept of recursion.

The Tower of Hanoi or Towers of Hanoi , also called the Tower of Brahma or Towers of Brahma, 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 neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

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

  • Only one disk may be moved at a time.
  • Each move consists of taking the upper disk from one of the rods and sliding it onto another
  • rod, on top of the other disks that may already be present on that rod.
  • No disk may be placed on top of a smaller disk.
310 questions
5
votes
5 answers

Towers of Hanoi Python - understanding recursion

I'm completely new to Python and I am currently going over a tutorial about The Towers of Hanoi and recursion. I thought that I understood recursion until they gave this example: def moveTower(height,fromPole, toPole, withPole): if height >= 1: …
heather
  • 109
  • 1
  • 2
  • 10
5
votes
7 answers

Is this solution to an open conundrum correct?

I believe that I have solved an open problem in complexity theory but I want to make sure that it's right. I problem in question is: ``How many moves does it take to solve the Towers of Hanoi puzzle as the number of towers increases?'' What is…
randomusername
  • 7,927
  • 23
  • 50
5
votes
6 answers

Facebook sample puzzle: Towers of Hanoi

Here is a question from Facebook hiring sample test. There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of…
Sankalp
  • 2,796
  • 3
  • 30
  • 43
4
votes
2 answers

Towers of Hanoi puzzle (prolog)

every one know the famous hanoi prolog and you can find it HERE and its great but when i write this query move(3,left,right,center). its not showing these results Move top disk from left to right Move top disk from left to center Move top disk…
Nadia
4
votes
4 answers

Solving the Tower of Hanoi by using a good state space and then a search tree

I want to solve the "Towers of Hanoi" problem by using a good "state space". Using an appropriate state space is a way that is suggested by some AI techniques. Having a good state space, I would then like to be able to build a search tree and then…
Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80
4
votes
2 answers

Can this Haskell kata solution be made more idiomatic?

I'm relearning Haskell after a 10 year hiatus, partly to see what's changed and partly as an antidote to days spent in C#, SQL and JavaScript and partly as it's cool all of a sudden ;-) I decided to set myself the Towers of Hanoi as a coding kata,…
chillitom
  • 24,888
  • 17
  • 83
  • 118
4
votes
1 answer

Cannot make sense of my lecturers recursive algorithm for the Towers of Hanoi

The algorithm is as follows : Algorithm move(k, from, to, spare) if k >0 then move(k−1, from, spare, to) printf (”move top disc from %d to %d\n”, from, to) move(k−1, spare, to, from) k is the number of disks…
4
votes
2 answers

Alternate plain English algorithm for Towers of Hanoi

There are four plain English algorithms for the Towers of Hanoi Puzzle available on Wikipedia, but when I was first solving the puzzle, I came up with an algorithm that is different from any of the solutions I have seen. Wikipedia…
David Greydanus
  • 2,551
  • 1
  • 23
  • 42
4
votes
3 answers

Modified Tower of Hanoi

We all know that the minimum number of moves required to solve the classical towers of hanoi problem is 2n-1. Now, let us assume that some of the discs have same size. What would be the minimum number of moves to solve the problem in that…
n00bc0d3r
  • 275
  • 2
  • 10
4
votes
1 answer

Tower of hanoi, python -> scheme, shows error. What am I missing?

The python implementation import sys def move(src, dst, tmp, num): if num == 1: print 'Move from', src, 'to', dst else: move(src, tmp, dst, num-1) move(src, dst, tmp, 1) move(tmp, dst, src, num-1) move('left',…
sh_
  • 73
  • 7
4
votes
2 answers

Understanding recursive solution to Towers of Hanoi

Can anybody please explain this program? I especially want to know how the parameters are passed to the function tower and how the recursion works. Here is the code: #include #include void main() { int n; clrscr(); …
Boyd Smith
  • 53
  • 4
4
votes
1 answer

Tower of Hanoi C++(using recursion)

I wrote the following code as a practice exercise. I'm getting incorrect output when I print the destination stack. Can anyone please point out where I'm going wrong ? //Tower of Hanoi using…
Ishaan Sharma
  • 65
  • 2
  • 2
  • 10
4
votes
1 answer

Towers of Hanoi Explanation

I'm a beginner to python and would like to know what the role of the if statement is in this Towers of Hanoi function: def hanoi(ndisks, startPeg=1, endPeg=3): if ndisks: hanoi(ndisks-1, startPeg, 6-startPeg-endPeg) print "Move…
Scott Waldron
  • 41
  • 1
  • 3
3
votes
3 answers

Implement a counter on prolog towers of hanoi

I have a program due in the morning using Prolog. I am implementing Towers of Hanoi. What I need help with is each time that it prints Move disc number # from _ to _. I need to increment a counter saying Move X: "String" String being the previous…
Michael Stramel
  • 1,337
  • 1
  • 16
  • 18
3
votes
1 answer

Dyalog APL - Towers of Hanoi - does not loop properly

I cannot make it work in Dyalog APL solve←{ n a c b←⍵ n≤0:⍬ solve(n-1)a b c ⎕←'Move disk from' a 'to' c solve(n-1)b c a } solve 4 'A' 'C' 'B' It loops from the first solve (n-1) a b c but never goes to line 4. The same…
Miroslav Popov
  • 3,294
  • 4
  • 32
  • 55
1
2
3
20 21