Skip to content

Instantly share code, notes, and snippets.

@housecricket
Created September 26, 2020 06:49
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 housecricket/fa7388bc53351bbb2da4cb0bde3abafc to your computer and use it in GitHub Desktop.
Save housecricket/fa7388bc53351bbb2da4cb0bde3abafc to your computer and use it in GitHub Desktop.
/*
* s, d, e represents three pegs (source, destination and extra).
* n is number of discs (All initially in s)
*/
resolver_tower_of_hanoi = function (s, d, e, n) {
// terminating condition
if (n <= 0) {
return
}
resolver_tower_of_hanoi(s, e, d, n - 1)
console.log(`Move Disk-${n} FROM ${s} TO ${d}`);
resolver_tower_of_hanoi(e, d, s, n - 1);
}
resolver_tower_of_hanoi('s', 'd', 'e', 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment