Created
April 30, 2017 17:04
-
-
Save obiora22/25019ddf9bda89b95543672d51758bf8 to your computer and use it in GitHub Desktop.
Implementation of the Tower of Hanoi algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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