[?] Subscribe To This Site


leftimage for poohprod.ru

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.

puzzle - towers of Hanoi

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
% display your move
disp([init ' to ' fin])

% 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);

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

From 'Tower of Hanoi algorithm' to home

From 'Tower of Hanoi algorithm' to 'Fun menu'


footer for tower of hanoi algorithm page

disk 1h
disk 2
disk 2h
disk 3h
disk 4
disk 4h
disk 5
disk 5h
disk 6
disk 6h
disk 7
disk 7h
post 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