Hanoi Tower - How many to move disks.

How to move disks in Hanoi tower : programming example

Welcome to the blog post where we showcase the capabilities of our Function Calculator by solving one of the most famous problems in computer science and mathematics: The Tower of Hanoi. This post will introduce how to program to solve hanoi problem.

What is the Tower of Hanoi?

The Tower of Hanoi is a mathematical puzzle consisting of three rods (pegs) and a number of disks of different sizes. The puzzle starts with all disks stacked on one rod in decreasing size, with the largest disk at the bottom. The objective is to move all disks from the source rod to the destination rod, following these rules:

  • Only one disk can be moved at a time.

  • A disk can only be placed on top of a larger disk or an empty rod.

  • You must use an auxiliary rod to assist in moving the disks.

Below is code examples that solve hanoi tower problem.
You can copy below code and go to the Factory page in the Function Calculator and paste it into the editor.  
Click the run button to implement the code and if needed click the save button  to save your math functions.

1. numberOfHanoiMoves

This function calculates the minimum number of moves required to solve the Tower of Hanoi problem for n disks.

Function Definition:

def numberOfHanoiMoves(n, source, target, spare) =numberOfHanoiMoves(n−1, source, spare, target)
+ 1
+numberOfHanoiMoves(n−1, spare, target, source); numberOfHanoiMoves(1,source,target,spare)=1;

How It Works:

  • This recursive function breaks the problem into three steps:

    1. Move n-1 disks from the source to the auxiliary peg.

    2. Move the largest disk directly from the source to the destination peg.

    3. Move n-1 disks from the auxiliary peg to the destination peg.

  • The base case is when there is only one disk (n = 1), requiring exactly one move.

Example:

For n = 3 disks:

numberOfHanoiMoves(3, 1, 2, 3);

Output:

7

This means it takes exactly 7 moves to solve the Tower of Hanoi with 3 disks.

2. trackHanoiMoves

This function tracks all moves required to solve the Tower of Hanoi problem for n disks recursively.

Function Definition:

def trackHanoiMoves(n,source,target,spare){ if(n==1){ return source*10+target; } var spareToTarget=trackHanoiMoves(n−1,spare,target,source); var k=floor(log10(spareToTarget)); var sourceToTarget=source*10^(k+2)+target*10^(k+1); var sourceToSpare=trackHanoiMoves(n−1,source,spare,target)*10^(k+3); return sourceToSpare+sourceToTarget+spareToTarget; }

How It Works:

  • This recursive function encodes each move as a two-digit number (fromPeg * 10 + toPeg) and concatenates all moves into a single number.

  • It follows the same recursive logic as numberOfHanoiMoves, but instead of counting moves, it tracks each step.

Example:

For n = 3source = 1target = 2, and spare = 3:

trackHanoiMoves(3, 1, 2, 3);

Output:

12132312313212

This represents the sequence of moves:

  • Move disk from peg 1 to peg 2.

  • Move disk from peg 1 to peg 3.

  • Move disk from peg 2 to peg 3.

  • ...and so on.

3. hanoiMovesIterative

This function solves the Tower of Hanoi problem iteratively using a stack-based simulation. It tracks all moves without recursion.

Function Definition:


def hanoiMovesIterative(n,s,d,a){
    var result=0;
    var maxStackSize=100;
    var stack=0;
    var sp=0;
    var diskArr=0;
    var sourceArr=0;
    var destArr=0;
    var auxArr=0;
    var stageArr=0;
    var i=0;
    while(i<maxStackSize){
        diskArr=diskArr*10+0;
        sourceArr=sourceArr*10+0;
        destArr=destArr*10+0;
        auxArr=auxArr*10+0;
        stageArr=stageArr*10+0;
        i=i+1;
    }
    var tempDisk=diskArr;
    diskArr=diskArr*10+n;
    var tempSrc=sourceArr;
    sourceArr=sourceArr*10+s;
    var tempDst=destArr;
    destArr=destArr*10+d;
    var tempAux=auxArr;
    auxArr=auxArr*10+a;
    var tempStg=stageArr;
    stageArr=stageArr*10+0;
    sp=sp+1;
    while(sp>0){
        var currDisk=diskArr%10;
        diskArr=floor(diskArr/10);
        var currSrc=sourceArr%10;
        sourceArr=floor(sourceArr/10);
        var currDst=destArr%10;
        destArr=floor(destArr/10);
        var currAux=auxArr%10;
        auxArr=floor(auxArr/10);
        var currStg=stageArr%10;
        stageArr=floor(stageArr/10);
        sp=sp−1;
        if(currDisk==1){
            result=result*100+currSrc*10+currDst;
        }
        elseif(currStg==0){
            diskArr=diskArr*10+currDisk;
            sourceArr=sourceArr*10+currSrc;
            destArr=destArr*10+currDst;
            auxArr=auxArr*10+currAux;
            stageArr=stageArr*10+1;
            sp=sp+1;
            diskArr=diskArr*10+(currDisk−1);
            sourceArr=sourceArr*10+currSrc;
            destArr=destArr*10+currAux;
            auxArr=auxArr*10+currDst;
            stageArr=stageArr*10+0;
            sp=sp+1;
        }
        elseif(currStg==1){
            result=result*100+currSrc*10+currDst;
            diskArr=diskArr*10+currDisk;
            sourceArr=sourceArr*10+currSrc;
            destArr=destArr*10+currDst;
            auxArr=auxArr*10+currAux;
            stageArr=stageArr*10+2;
            sp=sp+1;
            diskArr=diskArr*10+(currDisk−1);
            sourceArr=sourceArr*10+currAux;
            destArr=destArr*10+currDst;
            auxArr=auxArr*10+currSrc;
            stageArr=stageArr*10+0;
            sp=sp+1;
        }
    }
    return result;
}


Comments

Popular posts from this blog

Prompt for AI

How Deep Is the Well?

Function Examples