[?] Subscribe To This Site

# Tower of Hanoi algorithm

1.- Introduction

2.- Presentation of main ideas and code

3.- Play the Puzzle!!

### 1.- Introduction to the algorithm of the Towers of Hanoi in Matlab

 Would you like to get the Tower of Hanoi algorithm? For the moment, see how you do in this wonderful game of skill and logic... The Towers of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod.

The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, and then making a conical shape.

Try to move all the disks onto another pole. Thing is, you can only
move one disk at a time and you must follow size order (a bigger disk
can't go on a smaller disk). Good luck!

### 2.- Solution using recursivity

The following presentation gives you a very good idea of what recursivity is.
You can examine the full code in Matlab after the presentation.

This is the main function (the one that is going to call itself many times).

function m(n, init, temp, fin)
if n == 1
disp([init ' to ' fin])

else
% move n-1 disks from A to B, with C as temp
m(n-1, init, fin, temp);

% move disk from A to C, with B as temp
m(1, init, temp, fin);

% move n-1 disks from B to C, with A as temp
m(n-1, temp, init, fin);
end

You can call it providing the number of disks and the names of the towers.

n = 3;
m(n, ' A', ' B', ' C')

This is the complete result of the moves for 3 disks...

A to C
A to B
C to B
A to C
B to A
B to C
A to C

### 3.- Play the Game!

Free JavaScripts provided
by

 Top

## Related pages

matlab rc filterdepreciation salvage valuerecursion matlabtrapezoid rule matlabsum matrix matlabdivide the polynomialsbinary hexadecimal conversionmatlab simultaneous equationsmatlab log log plotcalculate nominal interest rateleast square fit matlabpythagorean triangle calculatorwhile loop matlab example5 band resistor calculatorcharging and discharging a capacitor in an r-c circuitcalculate bmi manuallyhalf life equation exponential decaymatlab fminsearch examplematlab tutorial for engineerswhat is an exponential regressionmatlab conditional statementsfliplr matlabintergral calculatorwinspicesimple matlab program examplegraph piecewise functions calculator onlinedraw circle in matlabprintable ascii tablecombinational logicseries expansion of sinxmatlab tutorial bookmaclaurin series expansion of sinxmatlab bisection methodcreate gui in matlabmortgage amorthow to calculate salvage value of an assetsimple matlab gui exampleshow to graph using matlabmatlab gui tutorial for beginnersintegral with limits calculatorhow to find time constant in rc circuitintegral calculatetrapezoidal method examplepower factor correction capacitorsexponential regression calculator onlineinverse of matrix in matlabparallel ohm calculatorinterest calculation spreadsheetmaclaurin series definitiondepreciation spreadsheetlagrange interpolation matlabsemilogx matlabformula to compute compound interesttrigonometric integral calculatorde morgan theorem examplesmatlab interpolation functiondecimal octal conversiondefine piecewise function matlabrc network time constantonline hypotenuse calculatorcontinuous compounding calculatorcount bmi formulaexponential function in matlabdefinite double integral calculatorcircle in matlabmatlab rootsexample of periodic functionrc transient circuitexclusive or xorlaptop depreciation calculatoronline bell curve generatorsalvage value of car calculator