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

m
sort languages alphabetically
m (added OEIS link)
m (sort languages alphabetically)
Line 2:
 
;Task:
Show here (on this page) the decimal numbers formed by:
:: <big><big>(</big></big>'''n''' &nbsp; '''1''''s &nbsp; appended by the digit &nbsp; '''3'''<big><big>)</big></big> &nbsp; and then square the result, &nbsp; &nbsp; where &nbsp; <big> '''0 &nbsp; <= &nbsp; n &nbsp; < &nbsp; 8''' </big>
<br><br>
 
;See also
Line 12 ⟶ 11:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">L(i) 0..7 {print(‘( ’(‘1’ * i)‘3 ) ^ 2 = ’(Int64((‘1’ * i)‘3’) ^ 2))}</syntaxhighlight>
 
Line 93 ⟶ 91:
INT pos := 0;
STRING pattern := "123456790";
INT p len := ( UPB pattern - LWB pattern ) + 1;
WHILE string in string( pattern, pos, v ) DO
v := v[ 1 : pos - 1 ] + "A" + v[ pos + p len : ]
Line 139 ⟶ 137:
111 AAAAAAAAAAAA12387654320ZZZZZZZZZZZ98769
</pre>
=={{header|Arturo}}==
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">loop 0..7 'x [
num: to :integer(repeat "1" x) ++ "3"
Line 147 ⟶ 145:
 
{{out}}
 
<pre>3 9
13 169
Line 380 ⟶ 377:
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure OnesPlusThree(Memo: TMemo);
Line 414 ⟶ 409:
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 466 ⟶ 460:
1111113 1234572098769
11111113 123456832098769
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function make13(n as uinteger) as uinteger
dim as uinteger t = 0
while n
t = 10*(t+1)
n-=1
wend
return t+3
end function
 
dim as ulongint m
 
for n as uinteger = 0 to 7
m = make13(n)^2
print make13(n), m
next n</syntaxhighlight>
{{out}}
<pre>
3 9
13 169
113 12769
1113 1238769
11113 123498769
111113 12346098769
1111113 1234572098769
11111113 123456832098769
</pre>
 
Line 508 ⟶ 474:
: show dup . ." ^2 = " sqr . cr ;
 
: show-upto
0 swap
begin over over < while
Line 520 ⟶ 486:
 
{{out}}
 
<pre>3 ^2 = 9
13 ^2 = 169
Line 529 ⟶ 494:
1111113 ^2 = 1234572098769
11111113 ^2 = 123456832098769</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function make13(n as uinteger) as uinteger
dim as uinteger t = 0
while n
t = 10*(t+1)
n-=1
wend
return t+3
end function
 
dim as ulongint m
 
for n as uinteger = 0 to 7
m = make13(n)^2
print make13(n), m
next n</syntaxhighlight>
{{out}}
<pre>
3 9
13 169
113 12769
1113 1238769
11113 123498769
111113 12346098769
1111113 1234572098769
11111113 123456832098769
</pre>
 
=={{header|Go}}==
Line 580 ⟶ 573:
format <$> onesPlusThree</syntaxhighlight>
{{out}}
 
<pre> 3^2 = 9
13^2 = 169
Line 654 ⟶ 646:
'''For 100 <= n <= 105'''
 
Encoding "123456790" as "A", "987654320" as Z", and lastly "9876" as "N":
<syntaxhighlight lang="jq">range(100; 106) as $n
| ((("1"*$n) + "3") | tonumber) as $number
Line 742 ⟶ 734:
11111113**2 = 123456832098769</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">{#, #^2} & /@
Table[FromDigits[PadLeft[{3}, n, 1]], {n, 9}] // TableForm</syntaxhighlight>
Line 798 ⟶ 790:
1111113 1234572098769
11111113 123456832098769</pre>
 
=={{header|Plain English}}==
Only 5 entries are shown due to Plain English's 32-bit signed integers.
<syntaxhighlight lang="plain english">To run:
Start up.
Put 0 into a counter.
Loop.
If the counter is greater than 4, break.
Put 10 into a number.
Raise the number to the counter plus 1.
Subtract 1 from the number.
Divide the number by 9.
Add 2 to the number.
Put the number into a squared number.
Raise the squared number to 2.
Write the number then " " then the squared number on the console.
Bump the counter.
Repeat.
Wait for the escape key.
Shut down.</syntaxhighlight>
{{out}}
<pre>
3 9
13 169
113 12769
1113 1238769
11113 123498769
</pre>
 
=={{header|Python}}==
===One Liner===
The most Pythonic way.............
<syntaxhighlight lang="python">
[print("( " + "1"*i + "3 ) ^ 2 = " + str(int("1"*i + "3")**2)) for i in range(0,8)]
</syntaxhighlight>
{{out}}
<pre>
( 3 ) ^ 2 = 9
( 13 ) ^ 2 = 169
( 113 ) ^ 2 = 12769
( 1113 ) ^ 2 = 1238769
( 11113 ) ^ 2 = 123498769
( 111113 ) ^ 2 = 12346098769
( 1111113 ) ^ 2 = 1234572098769
( 11111113 ) ^ 2 = 123456832098769
</pre>
===Procedural===
{{trans|FreeBASIC}}
<syntaxhighlight lang="python">#!/usr/bin/python
 
def make13(n):
t = 0
while n:
t = 10*(t+1)
n -= 1
return t+3
 
for n in range(0,7):
m = make13(n)**2
print("{:d}\t\t{:d}".format(make13(n),m))</syntaxhighlight>
 
===Functional===
 
Taking the first n terms from an infinite series:
<syntaxhighlight lang="python">'''Sequence of 1s appended with a 3, then squared'''
 
from itertools import islice
 
 
# seriesOfOnesEndingWithThree :: [Int]
def seriesOfOnesEndingWithThree():
'''An ordered and non-finite stream of integers
whose decimal digits end in 3, preceded only by a
series of (zero or more) ones.
(3, 13, 113, 1113 ...)
'''
def go(n):
return lambda x: n + 10 * x
 
return fmapGen(go(3))(
iterate(go(1))(0)
)
 
 
# showSquare :: (Int, Int, Int) -> String
def showSquare(ew, vw, n):
'''A string representation of the square of n,
both as an expression and as a value, with a
right-justfied expression column of width ew,
and a right-justified value column of width vw.
'''
return f'{str(n).rjust(ew)}^2 = {str(n ** 2).rjust(vw)}'
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Listing of the first 7 values of the series.'''
 
xs = take(7)(
seriesOfOnesEndingWithThree()
)
 
final = xs[-1]
w = len(str(final))
w1 = len(str(final ** 2))
print('\n'.join([
showSquare(w, w1, x) for x in xs
]))
 
 
# ----------------------- GENERIC ------------------------
 
# fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
def fmapGen(f):
'''A function f mapped over a
non finite stream of values.
'''
def go(g):
while True:
v = next(g, None)
if None is not v:
yield f(v)
else:
return
return go
 
 
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
while True:
yield v
v = f(v)
return go
 
 
# take :: Int -> [a] -> [a]
def take(n):
'''The first n values of xs.
'''
return lambda xs: list(islice(xs, n))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre> 3^2 = 9
13^2 = 169
113^2 = 12769
1113^2 = 1238769
11113^2 = 123498769
111113^2 = 12346098769
1111113^2 = 1234572098769</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ char 1 swap of
char 3 join
$->n drop ] is 1's+3 ( n -> n )
 
8 times
[ i^ 1's+3
dup echo
say " --> "
dup * echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre>3 --> 9
13 --> 169
113 --> 12769
1113 --> 1238769
11113 --> 123498769
111113 --> 12346098769
1111113 --> 1234572098769
11111113 --> 123456832098769</pre>
 
=={{header|PARI/GP}}==
Line 994 ⟶ 804:
11111113 123456832098769
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Line 1,000 ⟶ 811:
program OnesAppend3AndSquare;
const
MAX = 10;//37
 
procedure AddNumStrings(var s:Ansistring;const n:Ansistring;Offset:integer);
Line 1,020 ⟶ 831:
dec(l)
until l <= 0;
//correct carry, never used for '3' but > '3' only once
while (Offset>0) AND(carry = 1) do
begin
Line 1,177 ⟶ 988:
11111111111111111111111111111111111113 123456790123456790123456790123456790165432098765432098765432098765432098769
</pre>
 
=={{header|Plain English}}==
Only 5 entries are shown due to Plain English's 32-bit signed integers.
<syntaxhighlight lang="plain english">To run:
Start up.
Put 0 into a counter.
Loop.
If the counter is greater than 4, break.
Put 10 into a number.
Raise the number to the counter plus 1.
Subtract 1 from the number.
Divide the number by 9.
Add 2 to the number.
Put the number into a squared number.
Raise the squared number to 2.
Write the number then " " then the squared number on the console.
Bump the counter.
Repeat.
Wait for the escape key.
Shut down.</syntaxhighlight>
{{out}}
<pre>
3 9
13 169
113 12769
1113 1238769
11113 123498769
</pre>
 
=={{header|Python}}==
===One Liner===
The most Pythonic way...
<syntaxhighlight lang="python">
[print("( " + "1"*i + "3 ) ^ 2 = " + str(int("1"*i + "3")**2)) for i in range(0,8)]
</syntaxhighlight>
{{out}}
<pre>
( 3 ) ^ 2 = 9
( 13 ) ^ 2 = 169
( 113 ) ^ 2 = 12769
( 1113 ) ^ 2 = 1238769
( 11113 ) ^ 2 = 123498769
( 111113 ) ^ 2 = 12346098769
( 1111113 ) ^ 2 = 1234572098769
( 11111113 ) ^ 2 = 123456832098769
</pre>
 
===Procedural===
<syntaxhighlight lang="python">#!/usr/bin/python
 
def make13(n):
return 10 ** (n + 1) // 9 + 2
 
for n in map(make13, range(8)):
print('%9d%16d' % (n, n * n))</syntaxhighlight>
 
===Functional===
Taking the first n terms from an infinite series:
<syntaxhighlight lang="python">'''Sequence of 1s appended with a 3, then squared'''
 
from itertools import islice
 
 
# seriesOfOnesEndingWithThree :: [Int]
def seriesOfOnesEndingWithThree():
'''An ordered and non-finite stream of integers
whose decimal digits end in 3, preceded only by a
series of (zero or more) ones.
(3, 13, 113, 1113 ...)
'''
def go(n):
return lambda x: n + 10 * x
 
return fmapGen(go(3))(
iterate(go(1))(0)
)
 
 
# showSquare :: (Int, Int, Int) -> String
def showSquare(ew, vw, n):
'''A string representation of the square of n,
both as an expression and as a value, with a
right-justfied expression column of width ew,
and a right-justified value column of width vw.
'''
return f'{str(n).rjust(ew)}^2 = {str(n ** 2).rjust(vw)}'
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Listing of the first 7 values of the series.'''
 
xs = take(7)(
seriesOfOnesEndingWithThree()
)
 
final = xs[-1]
w = len(str(final))
w1 = len(str(final ** 2))
print('\n'.join([
showSquare(w, w1, x) for x in xs
]))
 
 
# ----------------------- GENERIC ------------------------
 
# fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
def fmapGen(f):
'''A function f mapped over a
non finite stream of values.
'''
def go(g):
while True:
v = next(g, None)
if None is not v:
yield f(v)
else:
return
return go
 
 
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
while True:
yield v
v = f(v)
return go
 
 
# take :: Int -> [a] -> [a]
def take(n):
'''The first n values of xs.
'''
return lambda xs: list(islice(xs, n))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre> 3^2 = 9
13^2 = 169
113^2 = 12769
1113^2 = 1238769
11113^2 = 123498769
111113^2 = 12346098769
1111113^2 = 1234572098769</pre>
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ char 1 swap of
char 3 join
$->n drop ] is 1's+3 ( n -> n )
 
8 times
[ i^ 1's+3
dup echo
say " --> "
dup * echo cr ]</syntaxhighlight>
 
{{out}}
<pre>3 --> 9
13 --> 169
113 --> 12769
1113 --> 1238769
11113 --> 123498769
111113 --> 12346098769
1111113 --> 1234572098769
11111113 --> 123456832098769</pre>
 
=={{header|Raku}}==
In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime.
 
<syntaxhighlight lang="raku" line>say "$_, {.²}" for (^∞).map({ ( 1 x $_ ~ 3)} ).grep({ .is-prime })[^8]</syntaxhighlight>
{{out}}
Line 1,266 ⟶ 1,250:
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
 
Line 1,295 ⟶ 1,279:
=={{header|RPL}}==
{{works with|HP|49}}
≪ { }
0 7 '''FOR''' n
10 n ^ 1 - 9 / 10 * 3 + SQ +
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
Line 1,307 ⟶ 1,291:
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">m = 8
(0..m).each do |n|
ones3 = "1"*n +"3"
puts ones3.ljust(m+2) + ( ones3.to_i**2).to_s
559

edits