Smallest numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Ring}}: flagged as missing the 50th number (N=50)..)
m (→‎{{header|REXX}}: changed the title and title comment.)
Line 5: Line 5:
<br><br>
<br><br>
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds the smallest positive integer K where k**k contains N, N < 51 */
<lang rexx>/*REXX pgm finds the smallest positive integer K where K**K contains N, N < 51 */
numeric digits 200 /*ensure enough decimal digs for k**k */
numeric digits 200 /*ensure enough decimal digs for k**k */
parse arg hi cols . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 11: Line 11:
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 6 /*width of a number in any column. */
w= 6 /*width of a number in any column. */
@spiKK= ' smallest positive integer K where k**k contains N, N < ' commas(hi)
@spiKK=' smallest positive integer K where K**K contains N, 0 N < ' commas(hi)
say ' N │'center(@spiKK, 5 + cols*(w+1) ) /*display the title of the output. */
say ' N │'center(@spiKK, 5 + cols*(w+1) ) /*display the title of the output. */
say '─────┼'center("" , 5 + cols*(w+1), '─') /* " " separator " " " */
say '─────┼'center("" , 5 + cols*(w+1), '─') /* " " separator " " " */
Line 25: Line 25:
end /*j*/
end /*j*/


if $\=='' then say center(idx, 5)"│"substr($, 2) /*possible display residual output.*/
if $\=='' then say center(idx, 5)"│"substr($,2) /*possible display any residual output.*/
say '─────┴'center("" , 5 + cols*(w+1), '─') /* " " separator " " " */
say '─────┴'center("" , 5 + cols*(w+1), '─') /* " " separator " " " */
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
Line 32: Line 32:
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
N │ smallest positive integer K where k**k contains N, N < 51
N │ smallest positive integer K where K**K contains N, 0 ≤ N < 51
─────┼───────────────────────────────────────────────────────────────────────────
─────┼───────────────────────────────────────────────────────────────────────────
0 │ 9 1 3 5 2 4 4 3 7 9
0 │ 9 1 3 5 2 4 4 3 7 9
Line 40: Line 40:
40 │ 16 17 9 7 12 28 6 23 9 24
40 │ 16 17 9 7 12 28 6 23 9 24
50 │ 23
50 │ 23
─────┴───────────────────────────────────────────────────────────────────────────
</pre>
</pre>



Revision as of 03:10, 11 April 2021

Smallest numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Smallest number k > 0 such that the decimal expansion of k^k contains n, where n < 51

REXX

<lang rexx>/*REXX pgm finds the smallest positive integer K where K**K contains N, N < 51 */ numeric digits 200 /*ensure enough decimal digs for k**k */ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 51 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 6 /*width of a number in any column. */ @spiKK=' smallest positive integer K where K**K contains N, 0 ≤ N < ' commas(hi) say ' N │'center(@spiKK, 5 + cols*(w+1) ) /*display the title of the output. */ say '─────┼'center("" , 5 + cols*(w+1), '─') /* " " separator " " " */ $=; idx= 0 /*define $ output list; index to 0.*/

    do j=0  for hi;            n= j + 1         /*look for a power of 6 that contains N*/
                   do k=1  until pos(j, k**k)>0 /*calculate a bunch of powers  (K**K). */
                   end   /*k*/
    c= commas(k)                                /*maybe add commas to the powe of six. */
    $= $ right(c, max(w, length(c) ) )          /*add a  K (power) ──► list, allow big#*/
    if n//cols\==0 | j==0  then iterate         /*have we populated a line of output?  */
    say center(idx, 5)'│'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, 5)"│"substr($,2) /*possible display any residual output.*/ say '─────┴'center("" , 5 + cols*(w+1), '─') /* " " separator " " " */ 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>

output   when using the default inputs:
  N  │  smallest positive integer  K  where  K**K  contains  N,   0  ≤  N  <  51
─────┼───────────────────────────────────────────────────────────────────────────
  0  │     9      1      3      5      2      4      4      3      7      9
 10  │    10     11      5     19     22     26      8     17     16     19
 20  │     9      8     13      7     17      4     17      3     11     18
 30  │    13      5     23     17     18      7     17     15      9     18
 40  │    16     17      9      7     12     28      6     23      9     24
 50  │    23
─────┴───────────────────────────────────────────────────────────────────────────

Ring

This example is incomplete.

The output doesn't show the 50th number,

it stops at the 49th number.

Please ensure that it meets all task requirements and remove this message.

<lang ring> load "stdlib.ring"

decimals(0) see "working..." + nl see "Smallest number k > 0 such that the decimal expansion of k^k contains n are:" + nl

row = 0 limit1 = 49 limit2 = 30

for n = 0 to limit1

   strn = string(n)
   for m = 1 to limit2
       powm = pow(m,m)
       strm = string(powm)
       ind = substr(strm,strn)
       if ind > 0
          exit
       ok
   next
   row = row + 1
   see "" + m + " "
   if row%10 = 0
      see nl
   ok

next

see "done..." + nl </lang>

Output:
working...
Smallest number k > 0 such that the decimal expansion of k^k contains n are:
9 1 3 5 2 4 4 3 7 9 
10 11 5 19 21 18 8 25 16 19 
9 8 13 7 17 4 17 3 11 18 
13 5 19 17 18 7 17 15 9 15 
16 18 9 7 12 25 6 23 9 23 
done...