Towers of Hanoi: Difference between revisions

Content added Content deleted
Line 888:
Using it (5 is the number of disks.)
<lang joy>[source destination temp] 5 hanoi.</lang>
 
=={{header|K}}==
<lang K> h:{[n;a;b;c]if[n>0;_f[n-1;a;c;b];`0:,//$($n,":",$a,"->",$b,"\n");_f[n-1;c;b;a]]}
h[4;1;2;3]
1:1->3
2:1->2
1:3->2
3:1->3
1:2->1
2:2->3
1:1->3
4:1->2
1:3->2
2:3->1
1:2->1
3:3->2
1:1->3
2:1->2
1:3->2</lang>
 
The disk to move in the i'th step is the same as the position of the leftmost 1 in the binary representation of 1..2^n.
<lang K> s:();{[n;a;b;c]if[n>0;_f[n-1;a;c;b];s,:n;_f[n-1;c;b;a]]}[4;1;2;3];s
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
 
1_{*1+&|x}'a:(2_vs!_2^4)
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1</lang>
 
=={{header|Liberty BASIC}}==