Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4595884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:02:11+00:00 2026-05-21T23:02:11+00:00

I am interested in finding how many disks are on each peg at a

  • 0

I am interested in finding how many disks are on each peg at a given move in the towers of Hanoi puzzle. For example, given n = 3 disks we have this sequence of configurations for optimally solving the puzzle:

   0 1 2
0. 3 0 0
1. 2 0 1 (move 0 -> 2)
2. 1 1 1 (move 0 -> 1)
3. 1 2 0 (move 2 -> 1)
4. 0 2 1 (move 0 -> 2)
5. 1 1 1 (move 1 -> 0)
6. 1 0 2 (move 1 -> 2)
7. 0 0 3 (move 0 -> 2)

So given move number 5, I want to return 1 1 1, given move number 6, I want 1 0 2 etc.

This can easily be done by using the classical algorithm and stopping it after a certain number of moves, but I want something more efficient. The wikipedia page I linked to above gives an algorithm under the Binary solutions section. I think this is wrong however. I also do not understand how they calculate n.

If you follow their example and convert the disk positions it returns to what I want, it gives 4 0 4 for n = 8 disks and move number 216. Using the classical algorithm however, I get 4 2 2.

There is also an efficient algorithm implemented in C here that also gives 4 2 2 as the answer, but it lacks documentation and I don’t have access to the paper it’s based on.

The algorithm in the previous link seems correct, but can anyone explain how exactly it works?

A few related questions that I’m also interested in:

  1. Is the wikipedia algorithm really wrong, or am I missing something? And how do they calculate n?
  2. I only want to know how many disks are on each peg at a certain move, not on what peg each disk is on, which is what the literature seems to be more concerned about. Is there a simpler way to solve my problem?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:02:12+00:00Added an answer on May 21, 2026 at 11:02 pm

    1) If your algo says Wikipedia is broken I’d guess you are right…

    2) As for calculating the number of disks in each peg, is it pretty straightfoward to do a recursive algorithm for it:

    (Untested, unelegant and possibly full of +-1 errors code follows:)

    function hanoi(n, nsteps, begin, middle, end, nb, nm, ne)
        // n = number of disks to mive from begin to end
        // nsteps = number of steps to move
        // begin, middle, end = index of the pegs
        // nb, nm, ne = number of disks currently in each of the pegs
    
        if(nsteps = 0) return (begin, middle, end, nb, nm, ne)
    
        //else:
    
        //hanoi goes like
        // a) h(n-1, begin, end, middle) | 2^(n-1) steps
        // b) move 1 from begin -> end   | 1 step
        // c) h(n-1, middle, begin, end) | 2^(n-1) steps
        // Since we know how the pile will look like after a), b) and c)
        // we can skip those steps if nsteps is large...
    
        if(nsteps <= 2^(n-1)){
            return hanoi(n-1, nsteps, begin, end, middle, nb, ne, nm):
        }
        nb -= n;
        nm += (n-1);
        ne += 1;
        nsteps -= (2^(n-1) + 1);
        //we are now between b) and c)
    
        return hanoi((n-1), nsteps, middle, begin, end, nm, nb, ne);
    
    function(h, n, nsteps)
        return hanoi(n, nsteps, 1, 2, 3, n, 0, 0)
    

    If you want effieciency, it should try to convert this to an iterative form (shouldn’t be hard – you don’t need to mantain a stack anyways) and find a way to better represent the state of the program, instead of using 6+ variables willy nilly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in finding out why this is used on some Web sites for
Following on from this question , I am interested in finding out how you
I'm interested in finding the most elegant and 'correct' solution to this problem. I'm
I have taken the FFT in Matlab in this given way. X=fft(t); % Take
Is this possible? I'm interested in finding out which columns were specified in the
I'm interested in finding out how to interface a .NET app (or even just
I'm interested in finding the most precise decimal values in a database field. I'd
I'm interested in finding out more about the Microsoft Office Communicator IM client, and
im interested how many api calls per second or per minute i can do
I'm interested in using the html5 notifications api but have a few questions: Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.