Last active
August 29, 2015 14:01
-
-
Save vickychijwani/44a2e31d1cb434cc6159 to your computer and use it in GitHub Desktop.
Recursive solution to Ogres of Hanoi
This file contains hidden or 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
(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) |
This file contains hidden or 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 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