Distinct power numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: elided an output line.)
m (→‎{{header|REXX}}: used a faster method of listing the numbers in order for larger lists.)
Line 531: Line 531:
say ' index │'center(title, 1 + cols*(w+1) )
say ' index │'center(title, 1 + cols*(w+1) )
say '───────┼'center("" , 1 + cols*(w+1), '─')
say '───────┼'center("" , 1 + cols*(w+1), '─')
@.= .; mx= 0 /*the default value for the @. array.*/
@.= .; $$= /*the default value for the @. array.*/
do a=lo to hi /*traipse through A values (LO──►HI).*/
do a=lo to hi /*traipse through A values (LO──►HI).*/
do b=lo to hi /* " " B " " " */
do b=lo to hi /* " " B " " " */
x= a**b; if @.x\==. then iterate /*Has it been found before? Then skip.*/
x= a ** b; if @.x\==. then iterate /*Has it been found before? Then skip.*/
@.x= x; mx= max(mx, x) /*assign the power product; fix the max*/
@.x= x; $$= $$ x /*assign power product; append to $$ */
end /*b*/
end /*b*/
end /*a*/
end /*a*/
found= 0; idx= 1 /*initialize # distinct power integers.*/
$=; idx= 1 /*$$: a list of distinct power integers*/
do j=1 while words($$)>0; call getMin $$ /*obtain smallest number in the $$ list*/
$= /*a list of distinct power integers. */
$= $ right(commas(z), max(w, length(z) ) ) /*add a distinct power number ──► list.*/
do j=0 for mx+1; if @.j==. then iterate /*Number not found in 1st DO loop? Skip*/
found= found + 1; c= commas(j) /*maybe add commas to the number. */
if j//cols\==0 then iterate /*have we populated a line of output? */
$= $ right(c, max(w, length(c) ) ) /*add a distinct power number ──► list.*/
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
if found//cols\==0 then iterate /*have we populated a line of output? */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/


if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─')
say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say
say 'Found ' commas(found) title
say 'Found ' commas(j-1) title
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
getMin: parse arg z .; p= 1; #= words($$) /*assume min; # words in $$.*/
do m=2 for #-1; a= word($$, m); if a>=z then iterate; z= a; p= m
end /*m*/; $$= delword($$, p, 1); return /*delete the smallest number.*/</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>