Towers of Hanoi: Difference between revisions

Content added Content deleted
(→‎Using GOSUBs: Fix stack depth.)
m (→‎Using binary method: make style consistent with other examples.)
Line 1,072: Line 1,072:
{{works with|Commodore BASIC}}
{{works with|Commodore BASIC}}
Very fast version in BASIC V2 on Commodore C-64
Very fast version in BASIC V2 on Commodore C-64
<lang gwbasic> 10 def fnm(x)=x-int(x/3)*3:rem modulo
<lang gwbasic> 10 DEF FNM3(X)=X-INT(X/3)*3:REM MODULO 3
20 n=4:gosub 100
20 N=4:GOSUB 100
30 end
30 END
100 rem hanoi
99 REM HANOI
110 :for m=1 to 2^n-1
100 :FOR M=1 TO 2^N-1
120 ::print m;":",fnm(m and m-1)+1;" to ";fnm((m or m-1)+1)+1
110 ::PRINT MID$(STR$(M),2);":",FNM3(M AND M-1)+1;"TO";FNM3((M OR M-1)+1)+1
130 :next
130 :NEXT M
140 return</lang>
140 RETURN</lang>
{{out}}
{{out}}
<pre>1 : 1 to 3
<pre>1: 1 TO 3
2 : 1 to 2
2: 1 TO 2
3 : 3 to 2
3: 3 TO 2
4 : 1 to 3
4: 1 TO 3
5 : 2 to 1
5: 2 TO 1
6 : 2 to 3
6: 2 TO 3
7 : 1 to 3
7: 1 TO 3
8 : 1 to 2
8: 1 TO 2
9 : 3 to 2
9: 3 TO 2
10 : 3 to 1
10: 3 TO 1
11 : 2 to 1
11: 2 TO 1
12 : 3 to 2
12: 3 TO 2
13 : 1 to 3
13: 1 TO 3
14 : 1 to 2
14: 1 TO 2
15 : 3 to 2 </pre>
15: 3 TO 2 </pre>


=={{header|BASIC256}}==
=={{header|BASIC256}}==