Show the (decimal) value of a number of 1s appended with a 3, then squared: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Raku}}: Add a Raku example)
(Added Wren)
Line 138: Line 138:
{11111113,123456832098769}
{11111113,123456832098769}
done...
done...
</pre>

=={{header|Wren}}==
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt

var a = Fn.new { |n|
var s = Num.fromString("1" * n + "3")
var t = s * s
Fmt.print("$d $d", s, t)
}

for (n in 0..7) a.call(n)</lang>

{{out}}
<pre>
3 9
13 169
113 12769
1113 1238769
11113 123498769
111113 12346098769
1111113 1234572098769
11111113 123456832098769
</pre>
</pre>

Revision as of 21:06, 5 April 2021

Show the (decimal) value of a number of 1s appended with a 3, then squared 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

a(n) = (n 1's followed by a 3)^2, where 0 <= n < 8

Factor

a(n) = ((10n+1 - 1) / 9 + 2)2

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: io kernel math math.functions prettyprint ;

a ( n -- e m ) 1 + 10^ 1 - 9 / 2 + dup sq ;

8 [ a swap pprint bl . ] each-integer</lang>

Output:
3 9
13 169
113 12769
1113 1238769
11113 123498769
111113 12346098769
1111113 1234572098769
11111113 123456832098769

Raku

In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime.

<lang perl6>say "$_, {.²}" for (^∞).map({ ( 1 x $_ ~ 3)} ).grep({ .is-prime })[^8]</lang>

Output:
3, 9
13, 169
113, 12769
11113, 123498769
111111113, 12345679432098769
11111111113, 123456790165432098769
111111111111111111111113, 12345679012345679012346098765432098765432098769
111111111111111111111111111111111111111111111111111111111111111111111111111111111113, 12345679012345679012345679012345679012345679012345679012345679012345679012345679012765432098765432098765432098765432098765432098765432098765432098765432098765432098769

REXX

A little extra code was added to pre-compute the biggest number to find the widths for output alignment. <lang rexx>/*REXX program appends a "3" to a number of "1"s, and then squares that number. */ numeric digits 1000 /*be able to handle huge numbers. */ parse arg n . /*obtain optional argument from the CL.*/ if n== | n=="," then n= 9 /*Not specified? Then use the default.*/ _= copies(1, n)3 /*compute largest index to get width. */ w1= length( commas(_) ) /*get the width of the largest index. */ w2= length( commas(_**2) ) /* " " " " " " number. */

      do #=0  to n;  _=copies(1, #)3            /*calculate prefix number for output.  */
      say right( commas(_), w1)  right( commas(_**2), w2)       /*show prefix, number. */
      end   /*#*/

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 input of:     37

(Shown at three-quarter size.)

                                                 3                                                                                                   9
                                                13                                                                                                 169
                                               113                                                                                              12,769
                                             1,113                                                                                           1,238,769
                                            11,113                                                                                         123,498,769
                                           111,113                                                                                      12,346,098,769
                                         1,111,113                                                                                   1,234,572,098,769
                                        11,111,113                                                                                 123,456,832,098,769
                                       111,111,113                                                                              12,345,679,432,098,769
                                     1,111,111,113                                                                           1,234,567,905,432,098,769
                                    11,111,111,113                                                                         123,456,790,165,432,098,769
                                   111,111,111,113                                                                      12,345,679,012,765,432,098,769
                                 1,111,111,111,113                                                                   1,234,567,901,238,765,432,098,769
                                11,111,111,111,113                                                                 123,456,790,123,498,765,432,098,769
                               111,111,111,111,113                                                              12,345,679,012,346,098,765,432,098,769
                             1,111,111,111,111,113                                                           1,234,567,901,234,572,098,765,432,098,769
                            11,111,111,111,111,113                                                         123,456,790,123,456,832,098,765,432,098,769
                           111,111,111,111,111,113                                                      12,345,679,012,345,679,432,098,765,432,098,769
                         1,111,111,111,111,111,113                                                   1,234,567,901,234,567,905,432,098,765,432,098,769
                        11,111,111,111,111,111,113                                                 123,456,790,123,456,790,165,432,098,765,432,098,769
                       111,111,111,111,111,111,113                                              12,345,679,012,345,679,012,765,432,098,765,432,098,769
                     1,111,111,111,111,111,111,113                                           1,234,567,901,234,567,901,238,765,432,098,765,432,098,769
                    11,111,111,111,111,111,111,113                                         123,456,790,123,456,790,123,498,765,432,098,765,432,098,769
                   111,111,111,111,111,111,111,113                                      12,345,679,012,345,679,012,346,098,765,432,098,765,432,098,769
                 1,111,111,111,111,111,111,111,113                                   1,234,567,901,234,567,901,234,572,098,765,432,098,765,432,098,769
                11,111,111,111,111,111,111,111,113                                 123,456,790,123,456,790,123,456,832,098,765,432,098,765,432,098,769
               111,111,111,111,111,111,111,111,113                              12,345,679,012,345,679,012,345,679,432,098,765,432,098,765,432,098,769
             1,111,111,111,111,111,111,111,111,113                           1,234,567,901,234,567,901,234,567,905,432,098,765,432,098,765,432,098,769
            11,111,111,111,111,111,111,111,111,113                         123,456,790,123,456,790,123,456,790,165,432,098,765,432,098,765,432,098,769
           111,111,111,111,111,111,111,111,111,113                      12,345,679,012,345,679,012,345,679,012,765,432,098,765,432,098,765,432,098,769
         1,111,111,111,111,111,111,111,111,111,113                   1,234,567,901,234,567,901,234,567,901,238,765,432,098,765,432,098,765,432,098,769
        11,111,111,111,111,111,111,111,111,111,113                 123,456,790,123,456,790,123,456,790,123,498,765,432,098,765,432,098,765,432,098,769
       111,111,111,111,111,111,111,111,111,111,113              12,345,679,012,345,679,012,345,679,012,346,098,765,432,098,765,432,098,765,432,098,769
     1,111,111,111,111,111,111,111,111,111,111,113           1,234,567,901,234,567,901,234,567,901,234,572,098,765,432,098,765,432,098,765,432,098,769
    11,111,111,111,111,111,111,111,111,111,111,113         123,456,790,123,456,790,123,456,790,123,456,832,098,765,432,098,765,432,098,765,432,098,769
   111,111,111,111,111,111,111,111,111,111,111,113      12,345,679,012,345,679,012,345,679,012,345,679,432,098,765,432,098,765,432,098,765,432,098,769
 1,111,111,111,111,111,111,111,111,111,111,111,113   1,234,567,901,234,567,901,234,567,901,234,567,905,432,098,765,432,098,765,432,098,765,432,098,769
11,111,111,111,111,111,111,111,111,111,111,111,113 123,456,790,123,456,790,123,456,790,123,456,790,165,432,098,765,432,098,765,432,098,765,432,098,769

Ring

<lang ring> load "stdlib.ring"

decimals(0)

see "working..." + nl

row = 0 limit = 8

str = "3" for n = 1 to limit

   if n = 1
      strn = number(str) 
      res = pow(strn,2)
      see "{" + strn + "," + res + "}" + nl
   else
      str = "1" + strn
      strn = number(str) 
      res = pow(strn,2)
      see "{" + strn + "," + res + "}" + nl
   ok   

next

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

Output:
working...
{3,9}
{13,169}
{113,12769}
{1113,1238769}
{11113,123498769}
{111113,12346098769}
{1111113,1234572098769}
{11111113,123456832098769}
done...

Wren

Library: Wren-fmt

<lang ecmascript>import "/fmt" for Fmt

var a = Fn.new { |n|

   var s = Num.fromString("1" * n + "3")
   var t = s * s
   Fmt.print("$d $d", s, t)

}

for (n in 0..7) a.call(n)</lang>

Output:
3 9
13 169
113 12769
1113 1238769
11113 123498769
111113 12346098769
1111113 1234572098769
11111113 123456832098769