Taxicab numbers: Difference between revisions

Content added Content deleted
(Added EchoLisp)
m (→‎{{header|REXX}}: added/changed whitespace and comments. simplified a function, corrected syntax of the output (text string).)
Line 1,582: Line 1,582:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program displays the specified first (lowest) taxicab numbers. */
<lang rexx>/*REXX pgm displays the specified 1st (lowest) taxicab numbers (for 3 ranges).*/
parse arg L.1 H.1 L.2 H.2 L.3 H.3 . /*obtain optional ranges from CL.*/
parse arg L.1 H.1 L.2 H.2 L.3 H.3 . /*obtain optional arguments from the CL*/
if L.1=='' | L.1==',' then L.1= 1 /*L1 is the low part of 1st range*/
if H.1=='' | H.1==',' then H.1= 25 /*H1 " " high " " " " */
if L.2=='' | L.2==',' then L.2= 454 /*L2 " " low " " 2nd " */
if H.2=='' | H.2==',' then H.2= 456 /*H2 " " high " " " " */
if L.3=='' | L.3==',' then L.3=2000 /*L3 " " low " " 3rd " */
if H.3=='' | H.3==',' then H.3=2006 /*H3 " " high " " " " */
mx=max(L.1, H.1, L.2, H.2, L.3, H.3) /*find how many taxicab #s needed*/
mx=mx+mx%10; ww=length(mx)*3 /*cushion, compensate for triples*/
w=ww%2; numeric digits max(9,ww) /*prepare to use larger numbers. */
@.=.; #=0; @@.=0; $.=; p='**3' /*initialize some REXX variables.*/
a=right('+',4); and=" ──and── " /* " " " " */
/* [] generate extra taxicab #s*/
do j=1 until #>=mx; C=j**3 /*taxicab #s may not be in order.*/
!.j=C /*might as well calculate a cube.*/
do k=1 for j-1; s=C+!.k /*define whole bunch of cube sums*/
if @.s==. then do /*if cube not defined, then do it*/
@.s=j; b.s=k /*define @.S & B.S≡sum of 2 cubes*/
iterate /* ··· and then go and do another*/
end /* [↑] define one cube at a time.*/
has=@@.s /*has it has been defined before?*/
if \has then $.s=right(s,ww) '───►' r(@.s,a)r(b.s) and r(j,a)r(k)
else $.s=$.s and r(j,a)r(k) /*build strings.*/
@@.s=1 /*mark taxicab# as a sum of cubes*/
if has then iterate /*S is a triple (or better). */
#=#+1 /*bump the taxicab number count. */
#.#=s /*define a #. taxicab number.*/
end /*k*/ /* [↑] build cubes one─at─a─time*/
end /*j*/ /* [↑] complete with overage #s.*/


call Psort # /*invoke sort to sort taxicab #s.*/
if L.1=='' | L.1==',' then L.1= 1 /*L1 is the low part of 1st range. */
if H.1=='' | H.1==',' then H.1= 25 /*H1 " " high " " " " */
do tier=1 for 3

call show L.tier, H.tier; say /*show a range of taxicab numbers*/
if L.2=='' | L.2==',' then L.2= 454 /*L2 " " low " " 2nd " */
end /*tier*/
exit /*stick a fork in it, we're done.*/
if H.2=='' | H.2==',' then H.2= 456 /*H2 " " high " " " " */

/*──────────────────────────────────one-liner subroutines───────────────*/
if L.3=='' | L.3==',' then L.3=2000 /*L3 " " low " " 3rd " */
r: return right(arg(1),w)p||arg(2) /*right─justify a #, append "**3"*/
show: do t=arg(1) to arg(2); _=#.t; say r(t) $._; end; return
if H.3=='' | H.3==',' then H.3=2006 /*H3 " " high " " " " */

/*──────────────────────────────────PSORT subroutine────────────────────*/
mx=max(L.1, H.1, L.2, H.2, L.3, H.3) /*find how many taxicab numbers needed.*/
Psort: parse arg h 1 oh /*H: ½─way point for pivot sort.*/
do while h>1; h=h%2; do i=1 for oh-h; j=i; k=h+i
mx=mx+mx%10; ww=length(mx)*3 /*cushion; compensate for the triples.*/
w=ww%2; numeric digits max(9,ww) /*prepare to use some larger numbers. */
do while #.k<#.j; _=#.j; #.j=#.k; #.k=_
if h>=j then leave; j=j-h; k=k-h; end; end /*i*/
@.=.; #=0; @@.=0; $.= /*initialize some REXX variables. */
end /*while h>1*/ /* [↑] sort the taxicab # array.*/
a=right('+',4); @and=" ──and── " /* " " " literals (vars).*/
return /* [] utilizes a pivot sort. */</lang>
/* [] generate extra taxicab numbers.*/
do j=1 until #>=mx; C=j**3 /*taxicab numbers may not be in order. */
!.j=C /*use memoization for cube calculation.*/
do k=1 for j-1; s=C+!.k /*define a whole bunch of cube sums. */
if @.s==. then do /*Cube not defined? Then process it. */
@.s=j; b.s=k /*define @.S and B.S≡sum of 2 cubes*/
iterate /* ··· and then go and do another.*/
end /* [] define one cube sum at a time. */
has=@@.s /*has this number been defined before? */
if \has then $.s=right(s,ww) '───►' r(@.s,a)r(b.s) @and r(j,a)r(k)
else $.s=$.s @and r(j,a)r(k) /*build a string for display.*/
@@.s=1 /*mark taxicab number as a sum of cubes*/
if has then iterate /*S is a triple (or sometimes better).*/
#=#+1 /*bump the taxicab number counter. */
#.#=s /*define a #. taxicab number. */
end /*k*/ /* [↑] build the cubes one─at─a─time. */
end /*j*/ /* [↑] complete with overage numbers. */

call Psort # /*invoke Psort to sort taxicab numbers.*/
do range=1 to 3; call tell L.range, H.range; say; end /*range*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
r: return right(arg(1),w)'^3'arg(2) /*right─justify a number, append "^3" */
tell: do t=arg(1) to arg(2); _=#.t; say right(t,9)':' $._; end; return
/*────────────────────────────────────────────────────────────────────────────*/
Psort: parse arg h 1 oh /*H: ½─way point for this pivot sort. */
do while h>1; h=h%2; do i=1 for oh-h; j=i; k=h+i
do while #.k<#.j; _=#.j; #.j=#.k; #.k=_
if h>=j then leave; j=j-h; k=k-h; end /*while #.k<#.j*/; end /*i*/
end /*while h>1*/ /* [↑] sort the taxicab numbers array.*/
return /* [↑] utilizes a pivot sort. */</lang>
'''output''' &nbsp; using the default inputs:
'''output''' &nbsp; using the default inputs:
<pre>
<pre>
1**3 1729 ───► 10**3 + 9**3 ──and── 12**3 + 1**3
1: 1729 ───► 10^3 + 9^3 ──and── 12^3 + 1^3
2**3 4104 ───► 15**3 + 9**3 ──and── 16**3 + 2**3
2: 4104 ───► 15^3 + 9^3 ──and── 16^3 + 2^3
3**3 13832 ───► 20**3 + 18**3 ──and── 24**3 + 2**3
3: 13832 ───► 20^3 + 18^3 ──and── 24^3 + 2^3
4**3 20683 ───► 24**3 + 19**3 ──and── 27**3 + 10**3
4: 20683 ───► 24^3 + 19^3 ──and── 27^3 + 10^3
5**3 32832 ───► 30**3 + 18**3 ──and── 32**3 + 4**3
5: 32832 ───► 30^3 + 18^3 ──and── 32^3 + 4^3
6**3 39312 ───► 33**3 + 15**3 ──and── 34**3 + 2**3
6: 39312 ───► 33^3 + 15^3 ──and── 34^3 + 2^3
7**3 40033 ───► 33**3 + 16**3 ──and── 34**3 + 9**3
7: 40033 ───► 33^3 + 16^3 ──and── 34^3 + 9^3
8**3 46683 ───► 30**3 + 27**3 ──and── 36**3 + 3**3
8: 46683 ───► 30^3 + 27^3 ──and── 36^3 + 3^3
9**3 64232 ───► 36**3 + 26**3 ──and── 39**3 + 17**3
9: 64232 ───► 36^3 + 26^3 ──and── 39^3 + 17^3
10**3 65728 ───► 33**3 + 31**3 ──and── 40**3 + 12**3
10: 65728 ───► 33^3 + 31^3 ──and── 40^3 + 12^3
11**3 110656 ───► 40**3 + 36**3 ──and── 48**3 + 4**3
11: 110656 ───► 40^3 + 36^3 ──and── 48^3 + 4^3
12**3 110808 ───► 45**3 + 27**3 ──and── 48**3 + 6**3
12: 110808 ───► 45^3 + 27^3 ──and── 48^3 + 6^3
13**3 134379 ───► 43**3 + 38**3 ──and── 51**3 + 12**3
13: 134379 ───► 43^3 + 38^3 ──and── 51^3 + 12^3
14**3 149389 ───► 50**3 + 29**3 ──and── 53**3 + 8**3
14: 149389 ───► 50^3 + 29^3 ──and── 53^3 + 8^3
15**3 165464 ───► 48**3 + 38**3 ──and── 54**3 + 20**3
15: 165464 ───► 48^3 + 38^3 ──and── 54^3 + 20^3
16**3 171288 ───► 54**3 + 24**3 ──and── 55**3 + 17**3
16: 171288 ───► 54^3 + 24^3 ──and── 55^3 + 17^3
17**3 195841 ───► 57**3 + 22**3 ──and── 58**3 + 9**3
17: 195841 ───► 57^3 + 22^3 ──and── 58^3 + 9^3
18**3 216027 ───► 59**3 + 22**3 ──and── 60**3 + 3**3
18: 216027 ───► 59^3 + 22^3 ──and── 60^3 + 3^3
19**3 216125 ───► 50**3 + 45**3 ──and── 60**3 + 5**3
19: 216125 ───► 50^3 + 45^3 ──and── 60^3 + 5^3
20**3 262656 ───► 60**3 + 36**3 ──and── 64**3 + 8**3
20: 262656 ───► 60^3 + 36^3 ──and── 64^3 + 8^3
21**3 314496 ───► 66**3 + 30**3 ──and── 68**3 + 4**3
21: 314496 ───► 66^3 + 30^3 ──and── 68^3 + 4^3
22**3 320264 ───► 66**3 + 32**3 ──and── 68**3 + 18**3
22: 320264 ───► 66^3 + 32^3 ──and── 68^3 + 18^3
23**3 327763 ───► 58**3 + 51**3 ──and── 67**3 + 30**3
23: 327763 ───► 58^3 + 51^3 ──and── 67^3 + 30^3
24**3 373464 ───► 60**3 + 54**3 ──and── 72**3 + 6**3
24: 373464 ───► 60^3 + 54^3 ──and── 72^3 + 6^3
25**3 402597 ───► 61**3 + 56**3 ──and── 69**3 + 42**3
25: 402597 ───► 61^3 + 56^3 ──and── 69^3 + 42^3


454**3 87483968 ───► 363**3 + 341**3 ──and── 440**3 + 132**3
454: 87483968 ───► 363^3 + 341^3 ──and── 440^3 + 132^3
455**3 87539319 ───► 414**3 + 255**3 ──and── 423**3 + 228**3 ──and── 436**3 + 167**3
455: 87539319 ───► 414^3 + 255^3 ──and── 423^3 + 228^3 ──and── 436^3 + 167^3
456**3 87579037 ───► 370**3 + 333**3 ──and── 444**3 + 37**3
456: 87579037 ───► 370^3 + 333^3 ──and── 444^3 + 37^3


2000**3 1671816384 ───► 944**3 + 940**3 ──and── 1168**3 + 428**3
2000: 1671816384 ───► 944^3 + 940^3 ──and── 1168^3 + 428^3
2001**3 1672470592 ───► 1124**3 + 632**3 ──and── 1187**3 + 29**3
2001: 1672470592 ───► 1124^3 + 632^3 ──and── 1187^3 + 29^3
2002**3 1673170856 ───► 1034**3 + 828**3 ──and── 1164**3 + 458**3
2002: 1673170856 ───► 1034^3 + 828^3 ──and── 1164^3 + 458^3
2003**3 1675045225 ───► 1081**3 + 744**3 ──and── 1153**3 + 522**3
2003: 1675045225 ───► 1081^3 + 744^3 ──and── 1153^3 + 522^3
2004**3 1675958167 ───► 1096**3 + 711**3 ──and── 1159**3 + 492**3
2004: 1675958167 ───► 1096^3 + 711^3 ──and── 1159^3 + 492^3
2005**3 1676926719 ───► 1095**3 + 714**3 ──and── 1188**3 + 63**3
2005: 1676926719 ───► 1095^3 + 714^3 ──and── 1188^3 + 63^3
2006**3 1677646971 ───► 990**3 + 891**3 ──and── 1188**3 + 99**3
2006: 1677646971 ───► 990^3 + 891^3 ──and── 1188^3 + 99^3
</pre>
</pre>