Distinct power numbers: Difference between revisions
Content added Content deleted
(Added Wren) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 7: | Line 7: | ||
<br>4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 |
<br>4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 |
||
<br><br> |
<br><br> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX pgm finds and displays distinct power integers: a^b, where a and b are 2≤both≤5*/ |
|||
parse arg n lo hi cols . /*obtain optional argument from the CL.*/ |
|||
if n=='' | n=="," then n= 15 /*Not specified? Then use the default.*/ |
|||
if lo=='' | lo=="," then lo= 2 /* " " " " " " */ |
|||
if hi=='' | hi=="," then hi= 5 /* " " " " " " */ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
|||
w= 10 /*width of a number in any column. */ |
|||
title= ' distinct power integers, a^b, where a and b are: ' lo "≤ both ≤" hi |
|||
say ' index │'center(title, 1 + cols*(w+1) ) |
|||
say '───────┼'center("" , 1 + cols*(w+1), '─') |
|||
@.= .; mx= 0 /*the default value for the @. array.*/ |
|||
do a=lo to hi /*traipse through A values (LO──►HI).*/ |
|||
do b=lo to hi /* " " B " " " */ |
|||
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*/ |
|||
end /*b*/ |
|||
end /*a*/ |
|||
found= 0; idx= 1 /*initialize # distinct power integers.*/ |
|||
$= /*a list of distinct power integers. */ |
|||
do j=0 for mx+1 until found==n /*search number range or until N found.*/ |
|||
if @.j==. then iterate /*this number "found" in 1st DO loop?*/ |
|||
found= found + 1; c= commas(j) /*maybe add commas to the number. */ |
|||
$= $ right(c, max(w, length(c) ) ) /*add a distinct power number ──► list.*/ |
|||
if found//cols\==0 then iterate /*have we populated a line of output? */ |
|||
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.*/ |
|||
say '───────┴'center("" , 1 + cols*(w+1), '─') |
|||
say |
|||
say 'Found ' commas(found) title |
|||
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> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
index │ distinct power integers, a^b, where a and b are: 2 ≤ both ≤ 5 |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 4 8 9 16 25 27 32 64 81 125 |
|||
11 │ 243 256 625 1,024 3,125 |
|||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
Found 15 distinct power integers, a^b, where a and b are: 2 ≤ both ≤ 5 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |