1) If your algo says that Wikipedia is broken, I think you're right ...
2). As for calculating the number of disks in each binding, it is quite easy to make a recursive algorithm for it:
(Unverified, inelegant, and possibly complete error code + -1:
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 to increase efficiency, he should try to convert it to iterative form (it should not be difficult - you donβt have to stack it at all) and find a way to better represent the state of the program, instead of using 6 + willy nilly variables.