Towers of Hanoi: Difference between revisions

Added a solution for MATLAB
No edit summary
(Added a solution for MATLAB)
Line 593:
Print["Move dist from pole ", from, " to ", to, "."];
Hanoi[n-1, via, from, to])</lang>
 
=={{header|MATLAB}}==
 
This is a direct translation from the Python example given in the Wikipedia entry for the Tower of Hanoi puzzle.
<lang MATLAB>function towerOfHanoi(n,A,C,B)
if (n~=0)
towerOfHanoi(n-1,A,B,C);
disp(sprintf('Move plate %d from tower %d to tower %d',[n A C]));
towerOfHanoi(n-1,B,C,A);
end
end</lang>
 
Sample output:
<lang MATLAB>towerOfHanoi(3,1,3,2)
Move plate 1 from tower 1 to tower 3
Move plate 2 from tower 1 to tower 2
Move plate 1 from tower 3 to tower 2
Move plate 3 from tower 1 to tower 3
Move plate 1 from tower 2 to tower 1
Move plate 2 from tower 2 to tower 3
Move plate 1 from tower 1 to tower 3</lang>
 
=={{header|Modula-3}}==
Anonymous user