Please answer this using JsFiddlenet and Include both HTML a
Please answer this using JsFiddle.net and Include both HTML and JavaScript
Using the Wikipedia article I want you to write a computer program that calculates the number of moves necessary to solve Tower of Hanoi given a number of disks.
Tower of Hanoi https://en.wikipedia.org/wiki/Tower_of_Hanoi
You will calculate this by implementing the recursive algorithm of the tower and counting every time a block is moved. You may also want to print out the moves. Also I recommend not allowing input greater that 7 blocks - it starts getting pretty big after that.
This can easily be implemented by using your Stack from previous assignments - all you really do in this is push and pop from the stack.
Please answer this using JsFiddle.net and Include both HTML and JavaScript
Answer the following questions in the interface of your code;
1. What is the Complexity (In Big O)?
2. 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?
Solution
TOWER OF HANOI PROBLEM SOLVING BY USING RECURSIVE ALGORITHM AND COUNTING EVERYTIME A BLOCK IS MOVED IN JAVASCRIPT AND HTML USING JSFIDDLE.NET:
FIRST HTML
<pre id=\"output\"></pre>
JAVA SCRIPT
var counter = 0,
lists;
var hanoi = function (disc, src, aux, dst) {
if (disc > 7) {
alert(\'THAT CAN TAKE A WHILE !\ Please use a lower number of discs...\');
return;
}
if (!lists) {
lists = {
src: [],
dst: [],
aux: []
};
for (var i = disc; i > 0; i--) {
lists.src.push(i);
}
console.log(\'Initially, src, dst and aux: \');
console.log(lists.src)
console.log(lists.dst)
console.log(lists.aux)
}
if (disc > 0) {
hanoi(disc - 1, src, dst, aux);
document.querySelector(\'#output \').innerHTML = document.querySelector(\'#output \').innerHTML + ++counter + \". Move disc \" + disc + \" from \" + src + \" to \" + dst + \"\ \";
lists[dst].push(lists[src].pop());
console.log(\'After step \' + counter + \', src, dst and aux: \');
console.log(lists.src)
console.log(lists.dst)
console.log(lists.aux)
hanoi(disc - 1, aux, src, dst);
}
};
hanoi(7, \"src\", \"aux\", \"dst\");

