Skip to content

Instantly share code, notes, and snippets.

@obiora22
Created April 30, 2017 17:04
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 obiora22/25019ddf9bda89b95543672d51758bf8 to your computer and use it in GitHub Desktop.
Save obiora22/25019ddf9bda89b95543672d51758bf8 to your computer and use it in GitHub Desktop.
Implementation of the Tower of Hanoi algorithm.
var hanoi = function (disc, begin, aux, end) {
if (disc > 0) {
hanoi(disc - 1, begin, end, aux)
hanoi(disc - 1, aux, begin, end )
console.log("Move disc " + disc + " from " + begin + " to " + end)
}
}
hanoi(3,'Src', 'Aux', 'Dst')
function h(n,begin, aux, end) {
if (n === 1) {
console.log(begin + ' ---> ' + end)
} else {
h(n-2, begin, end, aux);
h(1,begin,aux,end);
h(n-2,aux,begin,end)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment