Taxicab numbers: Difference between revisions
Content added Content deleted
Kinitawowi (talk | contribs) (→{{header|ZX Spectrum Basic}}: so many transcription errors; wish my emulator had a LIST to text file option...) |
Kinitawowi (talk | contribs) |
||
Line 3,142: | Line 3,142: | ||
This will, in the strictest sense, work. Don't hold your breath though; after six hours on an emulator at full speed it had generated the first 10 numbers. Getting to 2006 may take a while longer. |
This will, in the strictest sense, work. Don't hold your breath though; after six hours on an emulator at full speed it had generated the first 10 numbers. Getting to 2006 may take a while longer. |
||
You |
You cannot fit the whole 1625-entry table of cubes (and this program on top) into the 16K ZX Spectrum. Replace all 1625s with 1200s to resolve; numerically unjustified as an exhaustive search, but we know this will be sufficient to find the 2006th number. Eventually. |
||
<lang zxbasic>10 DIM f( |
<lang zxbasic>10 DIM f(1625): REM populating a cube table at the start will be faster than computing the cubes on the fly |
||
20 FOR x=1 TO |
20 FOR x=1 TO 1625 |
||
30 LET f(x)=x*x*x: REM x*x*x rather than x^3 as the ZX Spectrum's exponentiation function is legendarily slow |
30 LET f(x)=x*x*x: REM x*x*x rather than x^3 as the ZX Spectrum's exponentiation function is legendarily slow |
||
40 NEXT x |
40 NEXT x |
||
50 LET c=0 |
50 LET c=0 |
||
60 FOR x=1 TO 4294967295: REM the highest number the ZX Spectrum Basic can accurately hold internally; cuberoot max= |
60 FOR x=1 TO 4294967295: REM the highest number the ZX Spectrum Basic can accurately hold internally; floor (cuberoot max)=1625, hence the table limit |
||
70 LET k=0 |
70 LET k=0 |
||
80 FOR m=1 TO |
80 FOR m=1 TO 1625 |
||
90 FOR n=m+1 TO |
90 FOR n=m+1 TO 1625 |
||
100 IF f(m)+f(n)=x THEN GOTO 160 |
100 IF f(m)+f(n)=x THEN GOTO 160 |
||
110 IF f(n)>=x THEN LET n= |
110 IF f(n)>=x THEN LET n=1625: REM overshot, break out of the loop |
||
120 IF f(m)>=x THEN LET m= |
120 IF f(m)>=x THEN LET m=1625 |
||
130 NEXT n |
130 NEXT n |
||
140 NEXT m |
140 NEXT m |
||
Line 3,176: | Line 3,176: | ||
310 PRINT c;":";t$;"=";q;"^3+";r;"^3=";o;"^3+";p;"^3" |
310 PRINT c;":";t$;"=";q;"^3+";r;"^3=";o;"^3+";p;"^3" |
||
320 POKE 23692,10: REM suppress "scroll?" prompt when screen fills up at c=22 |
320 POKE 23692,10: REM suppress "scroll?" prompt when screen fills up at c=22 |
||
330 IF c=2006 THEN LET x=4294967295: LET n= |
330 IF c=2006 THEN LET x=4294967295: LET n=1625: LET m=1625 |
||
340 NEXT n |
340 NEXT n |
||
350 NEXT m |
350 NEXT m |