<h1>Recursion</h1>
Solving the Tower of Hanoi (pick a number that is 7 or less)
<br />
<input type="number" id="numInput" min="1" max="7" />
<input type="button" class="btnstyle" value="Calculate Moves" id="btnCalcMoves" onClick="calculate();" />
<br />
<br />
<input type="button" class="btnstyle" value="Reset" id="btnReset" onClick="reset();" />
<br />
<div id="message">
</div>
<div class="container" id="output">
</div>
<br />
<div>
Critical Thinking
<br />
<ol>
<li id="Q1">The Big-O notation is a relative representation of the complexity of an algorithm, and complexity is the relative measure of something else.</li>
<li>The solution will take a dramatic number of years before it is ever solved so we shouldnt concern ourselves with it. In solving a 64 disk with one move every second it would take 146,135,511,522.66 years. So to answer the question, yes the world probably would end before the 64 disk is solved. </li>
<ol>
JavaScript
////////////////////////////////////////////////////////////////////////////////////////////
var s;
var moves = 1;
function calculate() {
reset();
try {
var discs = document.getElementById("numInput").value;
var p1 = new doublyList();
var p2 = new doublyList();
var p3 = new doublyList();
if (discs < 1 || discs > 7 || discs != parseInt(discs)) {
document.getElementById("output").innerHTML = "";
throw "error, enter an integer from 1 to 7";
}
console.log("User chose " + discs + " disc(s)")
for (var i = 1; i <= discs; i++){
p1.push("Disc " + i)
}
console.log("Populating peg 1")
console.log(p1)
calcHanoi(discs, p1, p2, p3, "Peg 1", "Peg 2", "Peg 3")
console.log("\n\nLists after recursion")
console.log(p1)
console.log(p2)
console.log(p3)
document.getElementById("output").innerHTML = s
document.getElementById("message").innerHTML = "Moves: " + (moves-1)
document.getElementById("Q1").innerHTML = "The Big-O notation is a relative representation of the complexity of an algorithm, and complexity is the relative measure of something else."
} catch (err) {
document.getElementById("message").innerHTML = err
}
}
function reset(){
console.clear()
moves = 1;
s = ""
document.getElementById("output").innerHTML = ""
document.getElementById("message").innerHTML = "Moves Cleared"
document.getElementById("Q1").innerHTML = "The Big-O notation is a relative representation of the complexity of an algorithm, and complexity is the relative measure of something else."
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function calcHanoi(disc, src, aux, dest, _src, _aux, _dest) {
if (disc == 1)
{
dest.push(src.pop(1));
s += "<div><strong> "+moves+":</strong><em> Move Disc " + disc + " from " + _src + " to " + _dest + "</em></div>";
console.log(" " + moves + "\nMove...
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.