MOD 6 - RECURSION TOWER

CALCulates 2^h - 1 using recursive power function

by SHELDON PASCIAK

HTML

<!--

    THAT'S A LOT OF MOVES!

    OUCH! 18446744073709552000 moves needed for 64 disks


// if more is needed in this program, please let me know 
// I will rewrite, add, and modify all programs as needed
// I'm trying to make sure I get a running version of all
// then based on my learning, I hope to go back and improve
// them based on input/feedback.  


Assignment
 
We are not going to solve Tower of Hanoi, you are simply going to write a program to demonstrate computational complexity. 

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. 

Should we be concerned with concerned with the legend of the world ending when the 64 disk solution is physically solved?)

-->


This program uses a recursive Power function to determine the number of moves required to solve a Tower of Hanoi puzzle with [n] disks.  (2^n)-1   

<br />
<br />
How Many Disks? 
<input type="number" min=1 id="disks" placeholder="How many disks?" value=3 />
<input type="button" id="calcButton" value="Calculate" />

JavaScript

// MODULE 6 - PROGRAM 1 - SHELDON PASCIAK

// recursive power function needed to calculate moves required to solve tower of hanoi puzzle

var myPower = function (num,pow) {
    
    console.log(num + " ---- " + pow);
    
    if (pow<0) return null;
    
    if (pow==0) return 1; // anything^0 = 1
    
    return (num * myPower(num,pow-1));
    
}

$("#calcButton").click( function() { 
    
    //demo the power function
    console.log(" 3^3 = " + myPower(3,3));
    
    var diskCount = $('#disks').val();    
  
    //referenced wiki article for calculatios of moves - https://en.wikipedia.org/wiki/Tower_of_Hanoi
    //Using recurrence relations, the exact number of moves that this solution requires can be calculated by: 2^h - 1.    
   
    var movesNeeded = ( myPower(2,diskCount)-1 );    
    
   	alert ( movesNeeded + " moves needed for " + diskCount + " disks " );
    
    
});