Product of decimal digits of n: Difference between revisions

From Rosetta Code
Content added Content deleted
(Undo revision 336648 by Petelomax (talk))
Line 9: Line 9:
<br><br>
<br><br>
__TOC__
__TOC__

=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pdd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Product of the decimal digits of 1..100:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pdd</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
{{out}}
<pre>
Product of the decimal digits of 1..100:
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
2 4 6 8 10 12 14 16 18 0
3 6 9 12 15 18 21 24 27 0
4 8 12 16 20 24 28 32 36 0
5 10 15 20 25 30 35 40 45 0
6 12 18 24 30 36 42 48 54 0
7 14 21 28 35 42 49 56 63 0
8 16 24 32 40 48 56 64 72 0
9 18 27 36 45 54 63 72 81 0
</pre>


=={{header|REXX}}==
=={{header|REXX}}==

Revision as of 22:04, 25 June 2021

     

Task

Find the product of the decimal digits of a positive integer   n,   where n <= 100

REXX

<lang rexx>/*REXX pgm finds positive integers when shown in hex that can't be written with dec digs*/ parse arg n cols . /*obtain optional argument from the CL.*/ if n== | n=="," then n = 100 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */

                    title= ' the product of the decimal digits of  N,  where  N  < '  n

say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */ say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */ $=; idx= 1 /*list of products (so far); IDX=index.*/

   do #=1  for n;   L= length(#)                /*find products of the dec. digs of J. */
   p= left(#, 1)                                /*use first digit as the product so far*/
                    do j=2  for L-1  until p==0 /*add an optimization when product is 0*/
                    p= p * substr(#, j, 1)      /*multiply the product by the next dig.*/
                    end   /*j*/
   $= $ right(p, w)                             /*add the product  ───►  the  $  list. */
   if #//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   /*#*/                                  /*stick a fork in it,  we're all done. */

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */</lang>

output   when using the default inputs:
 index │                           the product of the decimal digits of  N,  where  N  <  100
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          1          2          3          4          5          6          7          8          9          0
  11   │          1          2          3          4          5          6          7          8          9          0
  21   │          2          4          6          8         10         12         14         16         18          0
  31   │          3          6          9         12         15         18         21         24         27          0
  41   │          4          8         12         16         20         24         28         32         36          0
  51   │          5         10         15         20         25         30         35         40         45          0
  61   │          6         12         18         24         30         36         42         48         54          0
  71   │          7         14         21         28         35         42         49         56         63          0
  81   │          8         16         24         32         40         48         56         64         72          0
  91   │          9         18         27         36         45         54         63         72         81          0
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Product of decimal digits of n:" + nl

row = 0 limit = 100

for n = 1 to limit

   prod = 1
   strn = string(n)
   for m = 1 to len(strn)
       prod = prod * number(strn[m])
   next    
   see "" + prod + " "
   row = row + 1
   if row%5 = 0
      see nl
   ok

next

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

Output:
working...
Product of decimal digits of n:
1 2 3 4 5 
6 7 8 9 0 
1 2 3 4 5 
6 7 8 9 0 
2 4 6 8 10 
12 14 16 18 0 
3 6 9 12 15 
18 21 24 27 0 
4 8 12 16 20 
24 28 32 36 0 
5 10 15 20 25 
30 35 40 45 0 
6 12 18 24 30 
36 42 48 54 0 
7 14 21 28 35 
42 49 56 63 0 
8 16 24 32 40 
48 56 64 72 0 
9 18 27 36 45 
54 63 72 81 0 
done...