Taxicab numbers: Difference between revisions

Content deleted Content added
Mihailp (talk | contribs)
add PicoLisp
m →‎{{header|REXX}}: mode some cosmetic changes.
Line 2,366: Line 2,366:
<lang rexx>/*REXX program displays the specified first (lowest) taxicab numbers (for three ranges).*/
<lang rexx>/*REXX program displays the specified first (lowest) taxicab numbers (for three ranges).*/
parse arg L.1 H.1 L.2 H.2 L.3 H.3 . /*obtain optional arguments from the 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 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 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.2=='' | L.2=="," then L.2= 454 /*L2 " " low " " 2nd " */
if L.3=='' | L.3=="," then L.3=2000 /*L3 " " low " " 3rd " */
if H.2=='' | H.2=="," then H.2= 456 /*H2 " " high " " " " */
if H.3=='' | H.3=="," then H.3=2006 /*H3 " " 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 numbers needed.*/
mx=max(L.1, H.1, L.2, H.2, L.3, H.3) /*find how many taxicab numbers needed.*/
mx=mx + mx % 10 /*cushion; compensate for the triples.*/
mx=mx + mx % 10 /*cushion; compensate for the triples.*/
Line 2,382: Line 2,378:
@.=.; #=0; @@.=0; @and=" ──and── " /*set some REXX vars and handy literals*/
@.=.; #=0; @@.=0; @and=" ──and── " /*set some REXX vars and handy literals*/
$.= /* [↓] generate extra taxicab numbers.*/
$.= /* [↓] generate extra taxicab numbers.*/
do j=1 until #>=mx; C=j**3 /*taxicab numbers may not be in order. */
do j=1 until #>=mx; C=j**3 /*taxicab numbers may not be in order. */
!.j=C /*use memoization for cube calculation.*/
!.j=C /*use memoization for cube calculation.*/
do k=1 for j-1; s=C + !.k /*define a whole bunch of cube sums. */
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. */
if @.s==. then do; @.s=j; b.s=k /*Cube not defined? Then process it. */
@.s=j; b.s=k /*define @.S and B.S≡sum of 2 cubes*/
iterate /*define @.S and B.S≡sum of 2 cubes*/
iterate /* ··· and then go and do another.*/
end /* [↑] define one cube sum at a time. */
end /* [↑] define one cube sum at a time. */
has=@@.s /*has this number been defined before? */
has=@@.s /*has this number been defined before? */
Line 2,398: Line 2,393:
end /*j*/ /* [↑] complete with overage numbers. */
end /*j*/ /* [↑] complete with overage numbers. */
A.=
A.=
do k=1 for mx; _=#.k; A.k=$._ /*re─assign disjoint $. elements to A. */
do k=1 for mx; _=#.k; A.k=$._ /*re─assign disjoint $. elements to A. */
end /*k*/
end /*k*/

call Esort mx /*sort taxicab #s with an exchange sort*/
call Esort mx /*sort taxicab #s with an exchange sort*/
do grp=1 to 3; call tell L.grp, H.grp /*display the three grps of numbers. */

end /*grp*/
do range=1 to 3; call tell L.range H.range /*display the three ranges of numbers. */
end /*range*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,410: Line 2,403:
U: return right(arg(1), w)'^3'arg(2) /*right─justify a number, append "^3" */
U: return right(arg(1), w)'^3'arg(2) /*right─justify a number, append "^3" */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Esort: procedure expose A.; parse arg N; h=N /*Esort when items have blanks.*/
Esort: procedure expose A.; parse arg N; h=N /*Esort when items have blanks.*/
do while h>1; h=h % 2
do while h>1; h=h % 2
do i=1 for N-h; j=i; k=h + i
do i=1 for N-h; k=h + i; j=i
do forever; parse var A.k xk .; parse var A.j xj .; if xk>=xj then leave
do forever; parse var A.k xk .; parse var A.j xj .; if xk>=xj then leave
_=A.j; A.j=A.k; A.k=_ /*swap two elements of A. */
_=A.j; A.j=A.k; A.k=_ /*swap two elements of A. array*/
if h>=j then leave; j=j - h; k=k-h
if h>=j then leave; j=j - h; k=k-h
end /*... */
end /*forever*/
end /*i*/
end /*i*/
end /*while h>1*/
end /*while h>1*/; return</lang>
return</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>