Skip to content

Instantly share code, notes, and snippets.

@vickychijwani
Last active August 29, 2015 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vickychijwani/44a2e31d1cb434cc6159 to your computer and use it in GitHub Desktop.
Recursive solution to Ogres of Hanoi
(defn move [from to]
(.say this "move" (to-array [from to])))
(defn hanoi [num from to via]
(if (= num 1)
(move from to)
(do
(hanoi (dec num) from via to)
(move from to)
(hanoi (dec num) via to from))))
(hanoi 5 2 1 3)
var that = this;
function hanoi(n, from, to, via) {
if (n === 1) {
that.say("move", [from, to]);
} else {
hanoi(n-1, from, via, to);
that.say("move", [from, to]);
hanoi(n-1, via, to, from);
}
}
hanoi(5, 2, 3, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment