How does this iterative Tower of Hanoi work? C

Posted by Nitesh Panchal on Stack Overflow See other posts from Stack Overflow or by Nitesh Panchal
Published on 2010-05-20T02:37:19Z Indexed on 2010/05/20 2:40 UTC
Read the original article Hit count: 213

Filed under:

Hello, while surfing google, i found this interesting solution to Tower Of Hanoi which doesn't even use stack. Can anybody explain me in brief, what is it actually doing? And this solution really acceptable?

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int n, x;
   printf( "How many disks? " );
   scanf( "%d", &n );
   printf("\n");
   for (x=1; x < (1 << n); x++)
      printf( "move from tower %i to tower %i.\n",
         (x&x-1)%3, ((x|x-1)+1)%3 );
return 0;
}

© Stack Overflow or respective owner

Related posts about c