Show the (decimal) value of a number of 1s appended with a 3, then squared
- Task
a(n) = (n 1's followed by a 3)^2, where 0 <= n < 8
Factor
a(n) = ((10n+1 - 1) / 9 + 2)2
<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
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...