Skip to content

Instantly share code, notes, and snippets.

@cacciatc
Last active December 11, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cacciatc/4551723 to your computer and use it in GitHub Desktop.
Save cacciatc/4551723 to your computer and use it in GitHub Desktop.
Towers Of Hanoi
var hanoi = function(n, source, by, dest){
if(n == 0) return;
// Move all the disks but the bottom disk (n – 1 disks) from the left pole to the middle pole.
hanoi(n-1,source,dest,by);
// Move the bottom disk to the right-most pole.
print("Move " + n + " from " + source + " to " + dest);
// Move all the disks from the middle pole (n – 1 disks) to the right-most pole.
hanoi(n-1,by,source,dest);
}
hanoi(3, 'A', 'B', 'C');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment