<html>
<body>
<h1>
The Tower of Hanoi
</h1>
There is a story about an Indian temple in Kashi Vishwanath which contains a large room with three time-worn posts in it, surrounded by 64 golden disks. Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks in accordance with the immutable rules of Brahma since that time. The puzzle is therefore also known as the Tower of Brahma puzzle. According to the legend, when the last move of the puzzle is completed, the world will end.
If the legend were true, and if the priests were able to move disks at a rate of one per second, using the smallest number of moves it would take them 2<sup>64</sup> − 1 seconds or roughly 585 billion years to finish, which is about 42 times the current age of the Universe.
<br>
Big O Notaion = 2<sup>n</sup><br>
Tower of Hanoi Formula = 2<sup>n</sup> - 1
<br>
<br>
<fieldset>
<legend>Apply the number of disks for the tower</legend>
<input type='number' id='tbInput'min='1' max='7'>
<input type='button' value='Apply Tower Pieces' onclick='ApplyTower()'><br><br>
<div id='outcome'>
</div>
</fieldset>
<fieldset>
<legend>Tower Moves</legend>
<div id="display" >
</div>
</fieldset>
</body>
</html>
JavaScript
var str = "";
var d ="";
var Node = function(_content){
this.next = null;
this.last = null;
this.content = _content;
return this;
}
var Stack = function(){
this.length = 0;
this.head = null;
this.top = null;
return this;
}
Stack.prototype.push = function(_content){
if(this.top == null){
this.top = new Node(_content);
this.head = this.top;
this.length = 1;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.head;
this.head.next = addedNode;
this.head = addedNode;
this.length++;
return this;
}
Stack.prototype.pop = function(){
if(this.top == null){
alert("The stack is empty");
return null;
}
else if (this.top == this.head) {
this.top = null;
this.head = null;
this.length = 0;
return this.head;
}
var a = this.head
this.head = this.head.last;
this.head.next = null;
this.length--;
return a;
}
Stack.prototype.toString = function(){
var node = this.top;
str = " ";
while(node != null){
str += node.content + "<br>" ;
node = node.next;
}
return str;
}
var tower1 = new Stack();
var tower2= new Stack();
var tower3 = new Stack();
function ApplyTower() {
document.getElementById("display").innerHTML = "";
var hanoiCounter = 0;
var diskNum = document.getElementById("tbInput").value;
if (diskNum > 7){
alert('Disk Number too large.');
return false;
}
hanoi(diskNum, 'Tower 1', 'Tower 2', 'Tower 3');
document.getElementById("outcome").innerHTML += "In " + hanoiCounter + " Moves" ;
}
function hanoi(disc, start, mid, end) {
if (disc == 0) return;
hanoi(disc - 1, start, end, mid);
d += "Move disc " + disc + " from " + start + " to " + end + "<br />";
document.getElementById('display').innerHTML = d;
var numbers = tower1.content;
tower1.pop(numbers);
tower2.push(numbers);
hanoi(disc - 1, end, mid, start);
d++;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.