Taxicab numbers: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments. simplified a function, corrected syntax of the output (text string).
(Added EchoLisp)
m (→‎{{header|REXX}}: added/changed whitespace and comments. simplified a function, corrected syntax of the output (text string).)
Line 1,582:
 
=={{header|REXX}}==
<lang rexx>/*REXX programpgm displays the specified first1st (lowest) taxicab numbers. (for 3 ranges).*/
parse arg L.1 H.1 L.2 H.2 L.3 H.3 . /*obtain optional rangesarguments 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.*/
 
callif PsortL.1=='' | #L.1==',' then L.1= 1 /*L1 is the low part of 1st range. /*invoke sort to sort taxicab #s.*/
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.12=='' | L.12==',' then L.12= 1454 /*L1L2 is the " " low part of 1st range" " 2nd " */
end /*tier*/
exitif H.2=='' | H.2==',' then H.2= 456 /*H2 " " high " " " " /*stick a fork in it, we're done.*/
 
/*──────────────────────────────────one-liner subroutines───────────────*/
if L.23=='' | L.23==',' then L.23= 4542000 /*L2L3 " " low " " 2nd3rd " */
r: return right(arg(1),w)p||arg(2) /*right─justify a #, append "**3"*/
show:if H.3=='' do| tH.3=arg(1)=',' tothen arg(2);H.3=2006 _=#.t;/*H3 say" r(t) " high " " " $._; " end; return*/
 
/*──────────────────────────────────PSORT subroutine────────────────────*/
mx=max(L.1, H.1, L.2, H.2, L.3, H.3) /*find how many taxicab #snumbers needed.*/
Psort: parse arg h 1 oh /*H: ½─way point for pivot sort.*/
mx=mx+mx%10; doww=length(mx)*3 while h>1/*cushion; h=h%2; do i=1 compensate for oh-h;the j=i; k=h+itriples.*/
w=ww%2; numeric digits max(9,ww) /*prepare to use some larger numbers. */
do while #.k<#.j; _=#.j; #.j=#.k; #.k=_
@.=.; #=0; @@.=0; $.= if h>=j then leave; j=j-h; /*initialize k=k-h;some REXX variables. end; end /*i*/
a=right('+',4); @and=" ──and── end " /*while h>1*/ " /* [↑]" sort the" taxicab #literals array(vars).*/
return /* [] utilizesgenerate aextra pivottaxicab sortnumbers. */</lang>
do j=1 until #>=mx; C=j**3 /*taxicab #snumbers may not be in order. */
!.j=C /*mightuse asmemoization wellfor calculatecube a cubecalculation.*/
do k=1 for j-1; s=C+!.k /*define a whole bunch of cube sums. */
if @.s==. then do /*if cubeCube not defined,? then do 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 /* [] generatedefine one cube sum at extraa taxicabtime. #s*/
has=@@.s /*has itthis hasnumber 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 /*build stringsdisplay.*/
@@.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 countcounter. */
#.#=s /*define a #. taxicab number. */
end /*k*/ /* [↑] build the cubes one─at─a─time. */
end /*j*/ /* [↑] complete with overage #snumbers. */
 
call Psort # /*invoke Psort to sort taxicab numbers.*/
do range=1 to 3; call tell L.range, H.range; say; end /*range*/
exit end /*stick [↑]a definefork onein cubeit, at awe're timeall done. */
/*────────────────────────────────────────────────────────────────────────────*/
r: return right(arg(1),w)p||'^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:
<pre>
1**3: 1729 ───► 10**^3 + 9**^3 ──and── 12**^3 + 1**^3
2**3: 4104 ───► 15**^3 + 9**^3 ──and── 16**^3 + 2**^3
3**3: 13832 ───► 20**^3 + 18**^3 ──and── 24**^3 + 2**^3
4**3: 20683 ───► 24**^3 + 19**^3 ──and── 27**^3 + 10**^3
5**3: 32832 ───► 30**^3 + 18**^3 ──and── 32**^3 + 4**^3
6**3: 39312 ───► 33**^3 + 15**^3 ──and── 34**^3 + 2**^3
7**3: 40033 ───► 33**^3 + 16**^3 ──and── 34**^3 + 9**^3
8**3: 46683 ───► 30**^3 + 27**^3 ──and── 36**^3 + 3**^3
9**3: 64232 ───► 36**^3 + 26**^3 ──and── 39**^3 + 17**^3
10**3: 65728 ───► 33**^3 + 31**^3 ──and── 40**^3 + 12**^3
11**3: 110656 ───► 40**^3 + 36**^3 ──and── 48**^3 + 4**^3
12**3: 110808 ───► 45**^3 + 27**^3 ──and── 48**^3 + 6**^3
13**3: 134379 ───► 43**^3 + 38**^3 ──and── 51**^3 + 12**^3
14**3: 149389 ───► 50**^3 + 29**^3 ──and── 53**^3 + 8**^3
15**3: 165464 ───► 48**^3 + 38**^3 ──and── 54**^3 + 20**^3
16**3: 171288 ───► 54**^3 + 24**^3 ──and── 55**^3 + 17**^3
17**3: 195841 ───► 57**^3 + 22**^3 ──and── 58**^3 + 9**^3
18**3: 216027 ───► 59**^3 + 22**^3 ──and── 60**^3 + 3**^3
19**3: 216125 ───► 50**^3 + 45**^3 ──and── 60**^3 + 5**^3
20**3: 262656 ───► 60**^3 + 36**^3 ──and── 64**^3 + 8**^3
21**3: 314496 ───► 66**^3 + 30**^3 ──and── 68**^3 + 4**^3
22**3: 320264 ───► 66**^3 + 32**^3 ──and── 68**^3 + 18**^3
23**3: 327763 ───► 58**^3 + 51**^3 ──and── 67**^3 + 30**^3
24**3: 373464 ───► 60**^3 + 54**^3 ──and── 72**^3 + 6**^3
25**3: 402597 ───► 61**^3 + 56**^3 ──and── 69**^3 + 42**^3
 
454**3: 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
456**3: 87579037 ───► 370**^3 + 333**^3 ──and── 444**^3 + 37**^3
 
2000**3: 1671816384 ───► 944**^3 + 940**^3 ──and── 1168**^3 + 428**^3
2001**3: 1672470592 ───► 1124**^3 + 632**^3 ──and── 1187**^3 + 29**^3
2002**3: 1673170856 ───► 1034**^3 + 828**^3 ──and── 1164**^3 + 458**^3
2003**3: 1675045225 ───► 1081**^3 + 744**^3 ──and── 1153**^3 + 522**^3
2004**3: 1675958167 ───► 1096**^3 + 711**^3 ──and── 1159**^3 + 492**^3
2005**3: 1676926719 ───► 1095**^3 + 714**^3 ──and── 1188**^3 + 63**^3
2006**3: 1677646971 ───► 990**^3 + 891**^3 ──and── 1188**^3 + 99**^3
</pre>