All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Three of Hanoi (Posted on 2005-07-01) Difficulty: 3 of 5
  • When solving the Towers of Hanoi puzzle, it's easy to show that on every other turn you must move the smallest disk... can you do this?
  • Then again, can you also prove that the smallest disk will move cyclically -- for example, from post 1 to post 2 to post 3 to post 1, and so on?
  • Finally, using that fact, can you provide an iterative (that is, not recursive) algorithm for solving the puzzle?
  • See The Solution Submitted by Federico Kereki    
    Rating: 4.5000 (2 votes)

    Comments: ( Back to comment list | You must be logged in to post comments.)
    My solution | Comment 2 of 3 |

     

    The first 2 are trivial, and left as an exercise to the reader.

    Numbering the towers 1,2,3 where 2 is the starting tower:

    move N moves a disk between tower (N mod 3)+1  and tower ((N+1) mod 3)+1

    I gave this solution in a programming class that tried to teach recursive programming. It didn't follow the teacher's method, but I did answer the task, according to the limitations specified by the teacher.

    I later did some more thinking, and found:

    The disk number that is moved in move N is the lowest bit in N that is set (odd numbers, for example, have bit 1 set, so odd moves move disk 1 etc)

    Odd numbered disks always move one way (in my above example, odd disks always move left) and even numbered disks the opposite way.

    So, to put it in programming terms, assuming the function "reverse" reverses a string, "binary" converts a number to a string of "1"s and "0"s, and "move" is the number of the move:

    disk=positionof("1",reverse(binary(move)))
    tower1=(move mod 3)+1
    tower2=((move+1) mod 3)+1
    if (odd(disk)) then (tower1,tower2)=(tower2,tower1)
    print "move ",move,": move disk ",disk," from tower ",tower1," to ",tower2

    Enjoy


      Posted by Jort Bloem on 2006-10-23 23:14:50
    Please log in:
    Login:
    Password:
    Remember me:
    Sign up! | Forgot password


    Search:
    Search body:
    Forums (1)
    Newest Problems
    Random Problem
    FAQ | About This Site
    Site Statistics
    New Comments (6)
    Unsolved Problems
    Top Rated Problems
    This month's top
    Most Commented On

    Chatterbox:
    Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information