Below iterative solution of the tower of hanoi problem...
#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;
}
i am not able to get how " (x&x-1)%3, ((x|x-1)+1)%3 " is able to
produce the output.What is the logic behind.
Can anybody explain it???
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.