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
3
votes
0 answers

Efficient way to implement Towers of Hanoi with steps returned as string

I'm comparing several algorithms in JS, Rust and Go. I implemented Towsers of Hanoi this way in Go: func Hanoi() (hanoi func(n int, from, via, to string) string) { var moves strings.Builder var hanoiRe func(n int, from, via, to string) …
Michael
  • 2,528
  • 3
  • 21
  • 54
3
votes
1 answer

Hanoi specific problem

Suppose that there are 2*n disks, How could be Hanoi problem solved if odd numbers are disks on bar "A" and even disks are on bar "B"? please Let me know if more information is needed. Thanks
Hosi
  • 545
  • 2
  • 7
  • 16
3
votes
1 answer

TCO optimized Tower of Hanoi in Clojure

I'm reading a Introdution to Haskell course and they're introducing well known Tower of Hanoi problem as a homework for the first class. I was tempted and wrote a solution: type Peg = String type Move = (Peg, Peg) hanoi :: Int -> Peg -> Peg -> Peg…
foki
  • 8,624
  • 6
  • 33
  • 32
3
votes
2 answers

HASKELL : Solving Towers of Hanoi

The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI and swapLID. MoveLOD: moves 1 disc from the first position to third the pin in the third position of the triplet. Additionally a string with information…
1775
  • 661
  • 2
  • 11
  • 15
3
votes
2 answers

Time Analysis of Tower of Hanoi in Java

I was just testing Towers of Hanoi problem in Java and I ran the following code: (I have removed sysouts for convinience) public class Util { public static void main(String[] args) { for (int i = 1; i <= 30; i++) { long…
Dhwaneet Bhatt
  • 601
  • 1
  • 8
  • 19
3
votes
1 answer

Counter for Towers Of Hanoi

I have written a code for Towers Of Hanoi game. I don't know how to implement a counter for this program on how many times it ran. Any help will be much appreciated. public class MainClass { public static void main(String[] args) { int nDisks…
John Smith
  • 61
  • 1
  • 8
2
votes
4 answers

Towers of Hanoi with Named Discs

For an assignment I have to create Towers of Hanoi in Common LISP with named discs. I need to get output that looks something like this: [1]> (hanoi '(Small Medium Large)) Moved SMALL from Peg 1 to Peg 3 Moved MEDIUM from Peg 1 to Peg 2 Moved SMALL…
Ziron
  • 23
  • 1
  • 5
2
votes
5 answers

Towers of Hanoi

I am working on an exercise in a book which asks us to solve the Towers of Hanoi problem using recursive methods. I have come to a solution, but from what I gather after browsing the Internet when done is that my solution may not be correct. Does…
E.O.
  • 794
  • 6
  • 16
  • 24
2
votes
1 answer

Solving Towers of Hanoi with the disks placed arbitrarily?

I am trying to solve the Towers of Hanoi problem using recursion. I understand that if all the rings are on one tower at the start, there's a nice algorithm for solving the problem based on looking at the binary representations of each step in the…
vigilant
  • 41
  • 8
2
votes
2 answers

Solving tower of Hanoi with given arrangements

I am given an arrangement of valid tower of Hanoi disks. The arrangement is given as an array. For example [2,2,0]: the indices of the array are the identifiers of the disks, ordered from small to large, and the values of the array are the positions…
user13560439
2
votes
1 answer

Problem in understanding flow of recursion in towers of hanoi

I am recently learning data structures and algorithm.I got simply working code of towers of hanoi. package linkedlist; public class TowrerOfHanoi { public static void main(String[] args) { TowrerOfHanoi toh=new TowrerOfHanoi(); …
Random guy
  • 883
  • 3
  • 11
  • 32
2
votes
2 answers

Unable to obtain buffer object data through glGetBufferSubData

I was hoping to make a tower of hanoi game using opengl. Eventually i came up to the problem of processing & transfering data from one buffer object to another. I have successfully stored my vertices in a buffer object and bound it with a vertex…
2
votes
1 answer

Haskell do notation and pattern matching?

So here is my code.. move :: [Char] -> [Char] -> IO () move f t = do { putStrLn ("Moving from \"" ++ f ++ "\" to \"" ++ t ++ "\"!") } hanoi :: Integer -> [Char] -> [Char] -> [Char] -> IO () hanoi 0 f _ _ = do { putStrLn ("Lane \""++ f ++ "\"…
2
votes
1 answer

Is it possible to make a proper tail-call of hanoi tower in lua?

I made a code of tower of hanoi problem with recursion and run it on the online lua compiler. If I put the input over 14, it didn't run. local num = io.read("*n") local count = 0 function hanoi(n, st, mid, dst) if n == 1 then count =…
jiny
  • 57
  • 3
2
votes
4 answers

Understanding c++ Code: Tower of Hanoi using Recursion

i am learning about recursion in c++ but have been stumped by the following c++ code used to solve the Tower Of Hanoi problem. void Hanoi(int m, string start, string middle, string end){ cout << "m is equal to: " << m << endl; if(m == 1){ …
VickTree
  • 889
  • 11
  • 26
1 2
3
20 21