<center> <h2><div id="t">Assignment 7</div></h2> </center>
<h3><center> Recursions</center></h3>
<h4><center>The complexity of the Tower of Hanoi is 2^(n - 1) and 2(-2^n) </center></h4> Should we be concerned with concerned with the legend of the world ending when the 64 disk solution is physically solved it it takes 2 seconds for each move? No
<br><br>
<input type="text" id="numOfDisks" value="3" size="5">
<input type="button" value="Start" onclick="numberOfMoves()">
<div id="results">
<br>
</div>
JavaScript
var Node = function(_content) {
this.next = null;
this.content = _content;
}
var Chain = function() {
this.head = null;
this.tail = null;
this.push = function(_content) {
if (this.tail == null) {
this.tail = new Node(_content);
this.head = this.tail;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.head;
this.head.next = addedNode;
this.head = addedNode;
return this;
}
}
function TowerofHanoi(disk) {
if (disk > 0) {
TowerofHanoi(disk - 1);
counter++;
TowerofHanoi(disk - 1);
}
}
function numberOfMoves() {
var disks = parseInt(document.getElementById("numOfDisks").value);
fillStack();
if (disks) {
TowerofHanoi(disks);
document.getElementById("results").innerHTML = "</br> It took " + counter + " moves to complete the Tower of Hanoi with " + disks + " disks";
}
}
function fillStack() {
var disks = document.getElementById("numOfDisks").value;
for (i = disks; i > 0; i--) {}
}
var stackOne = new Chain();
var stackTwo = new Chain();
var stackThree = new Chain();
var counter = 0;
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.