Towers of Hanoi

by chris richarde

HTML

<input type="textbox" id="value" />
<input type="button" id="stack" value="enter number" onClick="int();" />
<p id="output"></p><br/>
//the complexity of the tower is O(2^n)
//((2^64-1)*2) seconds. Yes the world will more than likely not survive  that length of time. Neither will the sun or maybe even the galaxy.

JavaScript

function int(){
i=0;
var value = document.getElementById("value").value; 
 for(var n=1;n<=value;n++){
  	a.push(n);
  
  document.getElementById("output").innerHTML=
  "sou = " + a +"<br/>"+ ""+"<br/>";
  }
hanoi(value,a,b,c);
document.getElementById("output").innerHTML+= "took " + i + " turns" +"<br/>" ;

}

var i = 0;
var a = [];
var b = [];
var c = [];

var hanoi= function(n,sou,tar,aux){
	
  if (n > 0){
  	hanoi(n-1, sou, aux, tar);
		tar.push(sou.pop())
    document.getElementById("output").innerHTML+= "sou = " + a +"<br/>" + "tar = " + b +"<br/>"+"aux = " + c +"<br/>"+ "" + "<br/>";
    hanoi(n-1, aux,tar,sou);
		i++;
    }

}