Scientific Calculator

(public) vasilkadifeli/TowersOfHanoi

By vasilkadifeli Vasil Kadifeli

Towers Of Hanoi - Problem solved recursively. Three pegs and a large amount of disks.

Tagged: printout recursion towers_of_hanoi

// Towers Of Hanoi Problem Solved with Recursion
// Three pegs and a large amount of disks.
// call as :  var printout = TowersOfHanoi(5);
// then go and look at var printout at the Variables window 

var TowersOfHanoi = function (n){
  var solution = []; 
  var a = "";
  var move = function(n,Pfrom,Pto,Pusing) {
     n > 0 ? (move(n-1,Pfrom,Pusing,Pto);
              push(solution,concat("move disc ",string(n)," from ",Pfrom," to ",Pto));
              move(n-1,Pusing,Pto,Pfrom);)
           :  0 ;
  };
  move(n,"A","C","B");
  solution;
};


spam? | offensive?

0 Comments

Sign in to leave a comment