Taxicab numbers: Difference between revisions

Added Forth entry
(Added XPL0 example.)
(Added Forth entry)
Line 1,268:
2006 : {1677646971, [{891, 990}, {99, 1188}]}
</pre>
 
 
 
=={{header|Forth}}==
 
{{works with|gforth|0.7.3}}
 
<lang forth>variable taxicablist
variable searched-cubessum
73 constant max-constituent \ uses magic numbers
 
: cube dup dup * * ;
: cubessum cube swap cube + ;
 
: ?taxicab ( a b -- c d true | false )
\ does exist an (c, d) such that c^3+d^3 = a^3+b^3 ?
2dup cubessum searched-cubessum !
dup 1- rot 1+ do \ c is possibly in [a+1 b-2]
dup i 1+ do \ d is possibly in [c+1 b-1]
j i cubessum searched-cubessum @ = if drop j i true unloop unloop exit then
loop
loop drop false ;
 
: swap-taxi ( n -- )
dup 5 cells + swap do
i @ i 5 cells - @ i ! i 5 cells - !
cell +loop ;
 
: bubble-taxicablist
here 5 cells - taxicablist @ = if exit then \ not for the first one
taxicablist @ here 5 cells - do
i @ i 5 cells - @ > if unloop exit then \ no (more) need to reorder
i swap-taxi
5 cells -loop ;
 
: store-taxicab ( a b c d -- )
2dup cubessum , swap , , swap , ,
bubble-taxicablist ;
 
: build-taxicablist
here taxicablist !
max-constituent 3 - 1 do \ a in [ 1 ; max-3 ]
i 3 + max-constituent swap do \ b in [ a+3 ; max ]
j i ?taxicab if j i store-taxicab then
loop
loop ;
 
: .nextcube cell + dup @ . ." ^3 " ;
: .taxi
dup @ .
." = " .nextcube ." + " .nextcube ." = " .nextcube ." + " .nextcube
drop ;
 
: taxicab 5 cells * taxicablist @ + ;
 
: list-taxicabs ( n -- )
0 do
cr I 1+ . ." : "
I taxicab .taxi
loop ;
 
build-taxicablist
25 list-taxicabs</lang>
 
{{out}}
<pre>1 : 1729 = 1 ^3 + 12 ^3 = 9 ^3 + 10 ^3
2 : 4104 = 2 ^3 + 16 ^3 = 9 ^3 + 15 ^3
3 : 13832 = 2 ^3 + 24 ^3 = 18 ^3 + 20 ^3
4 : 20683 = 10 ^3 + 27 ^3 = 19 ^3 + 24 ^3
5 : 32832 = 4 ^3 + 32 ^3 = 18 ^3 + 30 ^3
6 : 39312 = 2 ^3 + 34 ^3 = 15 ^3 + 33 ^3
7 : 40033 = 9 ^3 + 34 ^3 = 16 ^3 + 33 ^3
8 : 46683 = 3 ^3 + 36 ^3 = 27 ^3 + 30 ^3
9 : 64232 = 17 ^3 + 39 ^3 = 26 ^3 + 36 ^3
10 : 65728 = 12 ^3 + 40 ^3 = 31 ^3 + 33 ^3
11 : 110656 = 4 ^3 + 48 ^3 = 36 ^3 + 40 ^3
12 : 110808 = 6 ^3 + 48 ^3 = 27 ^3 + 45 ^3
13 : 134379 = 12 ^3 + 51 ^3 = 38 ^3 + 43 ^3
14 : 149389 = 8 ^3 + 53 ^3 = 29 ^3 + 50 ^3
15 : 165464 = 20 ^3 + 54 ^3 = 38 ^3 + 48 ^3
16 : 171288 = 17 ^3 + 55 ^3 = 24 ^3 + 54 ^3
17 : 195841 = 9 ^3 + 58 ^3 = 22 ^3 + 57 ^3
18 : 216027 = 3 ^3 + 60 ^3 = 22 ^3 + 59 ^3
19 : 216125 = 5 ^3 + 60 ^3 = 45 ^3 + 50 ^3
20 : 262656 = 8 ^3 + 64 ^3 = 36 ^3 + 60 ^3
21 : 314496 = 4 ^3 + 68 ^3 = 30 ^3 + 66 ^3
22 : 320264 = 18 ^3 + 68 ^3 = 32 ^3 + 66 ^3
23 : 327763 = 30 ^3 + 67 ^3 = 51 ^3 + 58 ^3
24 : 373464 = 6 ^3 + 72 ^3 = 54 ^3 + 60 ^3
25 : 402597 = 42 ^3 + 69 ^3 = 56 ^3 + 61 ^3 ok</pre>
 
One can use 1200 as magic number rather than 73 and display 2006 taxicab numbers ('2006 list-taxicabs') but the 10 triple taxicabs will slide the count...
 
 
=={{header|Fortran}}==
<lang fortran>
Anonymous user