Smallest power of 6 whose decimal expansion contains n: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{Draft task}} ;Task: Smallest power of 6 whose decimal expansion contains n, where '''n < 22''' <br><br> =={{header|Ring}}== <lang ring> load "stdlib.ring" decimals(0) see...")
 
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 4: Line 4:
Smallest power of 6 whose decimal expansion contains n, where '''n < 22'''
Smallest power of 6 whose decimal expansion contains n, where '''n < 22'''
<br><br>
<br><br>
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds the smallest (decimal) power of 6 which contains N, where N < 22. */
numeric digits 100 /*ensure enough decimal digs for power.*/
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 22 /*Not specified? Then use the default.*/
w= 50 /*width of a number in any column. */
@smp6= ' smallest power of six (expressed in decimal) which contains N'
say ' N │ power │'center(@smp6, 20 + w ) /*display the title of the output. */
say '─────┼───────┼'center("" , 20 + w, '─') /* " " separator " " " */

do j=0 for hi /*look for a power of 6 that contains N*/
do p=0; x= 6**p /*compute a power of six (in decimal). */
if pos(j, x)>0 then leave /*does the power contain an N ? */
end /*p*/
c= commas(x) /*maybe add commas to the powe of six. */
z= right(c, max(w, length(c) ) ) /*show a power of six, allow biger #s. */
say center(j, 5)'│'center(p, 7)"│" z /*display what we have so far (cols). */
end /*j*/

say '─────┴───────┴'center("" , 20 + w, '─') /* " " 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>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
N │ power │ smallest power of six (expressed in decimal) which contains N
─────┼───────┼──────────────────────────────────────────────────────────────────────
0 │ 9 │ 10,077,696
1 │ 0 │ 1
2 │ 3 │ 216
3 │ 2 │ 36
4 │ 6 │ 46,656
5 │ 6 │ 46,656
6 │ 1 │ 6
7 │ 5 │ 7,776
8 │ 12 │ 2,176,782,336
9 │ 4 │ 1,296
10 │ 9 │ 10,077,696
11 │ 16 │ 2,821,109,907,456
12 │ 4 │ 1,296
13 │ 13 │ 13,060,694,016
14 │ 28 │ 6,140,942,214,464,815,497,216
15 │ 18 │ 101,559,956,668,416
16 │ 3 │ 216
17 │ 10 │ 60,466,176
18 │ 15 │ 470,184,984,576
19 │ 21 │ 21,936,950,640,377,856
20 │ 26 │ 170,581,728,179,578,208,256
21 │ 3 │ 216
─────┴───────┴──────────────────────────────────────────────────────────────────────
</pre>

=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<lang ring>

Revision as of 08:03, 7 April 2021

Smallest power of 6 whose decimal expansion contains n 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 power of 6 whose decimal expansion contains n, where n < 22

REXX

<lang rexx>/*REXX pgm finds the smallest (decimal) power of 6 which contains N, where N < 22. */ numeric digits 100 /*ensure enough decimal digs for power.*/ parse arg hi . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 22 /*Not specified? Then use the default.*/ w= 50 /*width of a number in any column. */

              @smp6= ' smallest power of  six  (expressed in decimal)  which contains  N'

say ' N │ power │'center(@smp6, 20 + w ) /*display the title of the output. */ say '─────┼───────┼'center("" , 20 + w, '─') /* " " separator " " " */

     do j=0  for hi                             /*look for a power of 6 that contains N*/
                    do p=0;   x= 6**p           /*compute a power of six (in decimal). */
                    if pos(j, x)>0  then leave  /*does the power contain an   N ?      */
                    end   /*p*/
     c= commas(x)                               /*maybe add commas to the powe of six. */
     z= right(c, max(w, length(c) ) )           /*show a power of six, allow biger #s. */
     say center(j, 5)'│'center(p, 7)"│"   z     /*display what we have so far  (cols). */
     end   /*j*/

say '─────┴───────┴'center("" , 20 + w, '─') /* " " 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 input:
  N  │ power │   smallest power of  six  (expressed in decimal)  which contains  N
─────┼───────┼──────────────────────────────────────────────────────────────────────
  0  │   9   │                                         10,077,696
  1  │   0   │                                                  1
  2  │   3   │                                                216
  3  │   2   │                                                 36
  4  │   6   │                                             46,656
  5  │   6   │                                             46,656
  6  │   1   │                                                  6
  7  │   5   │                                              7,776
  8  │  12   │                                      2,176,782,336
  9  │   4   │                                              1,296
 10  │   9   │                                         10,077,696
 11  │  16   │                                  2,821,109,907,456
 12  │   4   │                                              1,296
 13  │  13   │                                     13,060,694,016
 14  │  28   │                      6,140,942,214,464,815,497,216
 15  │  18   │                                101,559,956,668,416
 16  │   3   │                                                216
 17  │  10   │                                         60,466,176
 18  │  15   │                                    470,184,984,576
 19  │  21   │                             21,936,950,640,377,856
 20  │  26   │                        170,581,728,179,578,208,256
 21  │   3   │                                                216
─────┴───────┴──────────────────────────────────────────────────────────────────────

Ring

<lang ring> load "stdlib.ring"

decimals(0) see "working..." + nl see "Smallest power of 6 whose decimal expansion contains n:" + nl

num = 0 limit = 200

for n = 1 to 21

   strn = string(n)
   for m = 0 to limit
       strpow = string(pow(6,m))
       ind = substr(strpow,strn)
       if ind > 0
          see "" + n + ". " + strpow + nl 
          exit
       ok
   next

next

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

Output:
working...
Smallest power of 6 whose decimal expansion contains n:
1. 1
2. 216
3. 36
4. 46656
5. 46656
6. 6
7. 7776
8. 2176782336
9. 1296
10. 10077696
11. 2821109907456
12. 1296
13. 13060694016
14. 6140942214464815497216
15. 101559956668416
16. 216
17. 60466176
18. 470184984576
19. 21936950640377856
20. 170581728179578208256
21. 216
done...