Show the (decimal) value of a number of 1s appended with a 3, then squared: Difference between revisions
Drkameleon (talk | contribs) (Added Arturo implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9: | Line 9: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">L(i) 0..7 {print(‘( ’(‘1’ * i)‘3 ) ^ 2 = ’(Int64((‘1’ * i)‘3’) ^ 2))}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 24: | Line 24: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
with Ada.Numerics.Big_Numbers.Big_Integers; |
with Ada.Numerics.Big_Numbers.Big_Integers; |
||
Line 44: | Line 44: | ||
Root := @ + 10**(N + 1); |
Root := @ + 10**(N + 1); |
||
end loop; |
end loop; |
||
end Ones_Plus_Three;</ |
end Ones_Plus_Three;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3 9 |
<pre> 3 9 |
||
Line 58: | Line 58: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Assuming LONG INT is large enough (as in e.g. ALGOL 68G). |
Assuming LONG INT is large enough (as in e.g. ALGOL 68G). |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
LONG INT n := 0; |
LONG INT n := 0; |
||
FOR i TO 8 DO |
FOR i TO 8 DO |
||
Line 65: | Line 65: | ||
n *:= 10 +:= 1 |
n *:= 10 +:= 1 |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 80: | Line 80: | ||
Alternative version that shows the values for higher numbers of ones. |
Alternative version that shows the values for higher numbers of ones. |
||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
PR precision 250 PR |
PR precision 250 PR |
||
LONG LONG INT n := 0; |
LONG LONG INT n := 0; |
||
Line 101: | Line 101: | ||
n *:= 10 +:= 1 |
n *:= 10 +:= 1 |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 137: | Line 137: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">loop 0..7 'x [ |
||
num: to :integer(repeat "1" x) ++ "3" |
num: to :integer(repeat "1" x) ++ "3" |
||
print [num num^2] |
print [num num^2] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 154: | Line 154: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SHOW_THE_DECIMAL_VALUE_OF_A_NUMBER_OF_1S_APPENDED_WITH_A_3_THEN_SQUARED.AWK |
# syntax: GAWK -f SHOW_THE_DECIMAL_VALUE_OF_A_NUMBER_OF_1S_APPENDED_WITH_A_3_THEN_SQUARED.AWK |
||
# converted from FreeBASIC |
# converted from FreeBASIC |
||
Line 170: | Line 170: | ||
return(t+3) |
return(t+3) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 184: | Line 184: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
Line 200: | Line 200: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 216: | Line 216: | ||
{{libheader|System.Numerics}} |
{{libheader|System.Numerics}} |
||
For 0 <= n < 22 |
For 0 <= n < 22 |
||
< |
<syntaxhighlight lang="csharp">using System; using BI = System.Numerics.BigInteger; |
||
class Program { static void Main(string[] args) { |
class Program { static void Main(string[] args) { |
||
for (BI x = 3; BI.Log10(x) < 22; x = (x - 2) * 10 + 3) |
for (BI x = 3; BI.Log10(x) < 22; x = (x - 2) * 10 + 3) |
||
Console.WriteLine("{1,43} {0,-20}", x, x * x); } }</ |
Console.WriteLine("{1,43} {0,-20}", x, x * x); } }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 9 3 |
<pre> 9 3 |
||
Line 245: | Line 245: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">ones_plus_three = proc (n: int) returns (int) |
||
r: int := 0 |
r: int := 0 |
||
for i: int in int$from_to(1, n) do |
for i: int in int$from_to(1, n) do |
||
Line 265: | Line 265: | ||
stream$putl(po, "") |
stream$putl(po, "") |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3^2 = 9 |
<pre> 3^2 = 9 |
||
Line 277: | Line 277: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. ONES-THREE-SQUARED. |
PROGRAM-ID. ONES-THREE-SQUARED. |
||
Line 308: | Line 308: | ||
ADD-ONE. |
ADD-ONE. |
||
MULTIPLY 10 BY ONES-3. |
MULTIPLY 10 BY ONES-3. |
||
ADD 1 TO ONES-3.</ |
ADD 1 TO ONES-3.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3^2 = 9 |
<pre> 3^2 = 9 |
||
Line 320: | Line 320: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
[3L;13L;113L;1113L;11113L;111113L;1111113L;11111113L;111111113L]|>List.iter(fun n->printfn "%10d->%d" n (n*n)) |
[3L;13L;113L;1113L;11113L;111113L;1111113L;11111113L;111111113L]|>List.iter(fun n->printfn "%10d->%d" n (n*n)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 339: | Line 339: | ||
<big>{{math|a(<var>n</var>) = ((10<sup><var>n</var>+1</sup> - 1) / 9 + 2)<sup>2</sup> }}</big> |
<big>{{math|a(<var>n</var>) = ((10<sup><var>n</var>+1</sup> - 1) / 9 + 2)<sup>2</sup> }}</big> |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math math.functions prettyprint ; |
||
: a ( n -- e m ) 1 + 10^ 1 - 9 / 2 + dup sq ; |
: a ( n -- e m ) 1 + 10^ 1 - 9 / 2 + dup sq ; |
||
8 [ a swap pprint bl . ] each-integer</ |
8 [ a swap pprint bl . ] each-integer</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 357: | Line 357: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">Func Make13(n) = m:=0; while n>0 do m:=10*(m+1);n:=n-1; od; m:=3+m; m. |
||
for i=0 to 7 do !Make13(i);!' ';!Make13(i)^2;!!'' od</ |
for i=0 to 7 do !Make13(i);!' ';!Make13(i)^2;!!'' od</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 372: | Line 372: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function make13(n as uinteger) as uinteger |
||
dim as uinteger t = 0 |
dim as uinteger t = 0 |
||
while n |
while n |
||
Line 386: | Line 386: | ||
m = make13(n)^2 |
m = make13(n)^2 |
||
print make13(n), m |
print make13(n), m |
||
next n</ |
next n</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 400: | Line 400: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: 1s+3 |
||
0 swap |
0 swap |
||
begin dup while |
begin dup while |
||
Line 420: | Line 420: | ||
8 show-upto |
8 show-upto |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 435: | Line 435: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 453: | Line 453: | ||
a(n) |
a(n) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 468: | Line 468: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Text.Printf (printf) |
||
onesPlusThree :: [Integer] |
onesPlusThree :: [Integer] |
||
Line 481: | Line 481: | ||
main = |
main = |
||
(putStr . unlines . take 8) $ |
(putStr . unlines . take 8) $ |
||
format <$> onesPlusThree</ |
format <$> onesPlusThree</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 498: | Line 498: | ||
For large values of n, the unbounded-precision integer arithmetic of gojq will ensure accuracy. |
For large values of n, the unbounded-precision integer arithmetic of gojq will ensure accuracy. |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# For gojq |
# For gojq |
||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
||
Line 508: | Line 508: | ||
(range(0;8) as $n |
(range(0;8) as $n |
||
| ((("1"*$n) + "3") | tonumber) as $number |
| ((("1"*$n) + "3") | tonumber) as $number |
||
| ($n|lpad(3)) + ($number|lpad(10)) + ($number|power(2)|lpad(20)) )</ |
| ($n|lpad(3)) + ($number|lpad(10)) + ($number|power(2)|lpad(20)) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 524: | Line 524: | ||
Encoding "123456790" as "A", "987654320" as Z", and lastly "9876" as "N": |
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 |
| ((("1"*$n) + "3") | tonumber) as $number |
||
| ($n|lpad(4)) + " " |
| ($n|lpad(4)) + " " |
||
+ ($number|power(2)|tostring| gsub("123456790";"A") | gsub("987654320";"Z") | gsub("9876";"N") | lpad(40)) |
+ ($number|power(2)|tostring| gsub("123456790";"A") | gsub("987654320";"Z") | gsub("9876";"N") | lpad(40)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 540: | Line 540: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">println("n (10^(n+1) - 1) ÷ 9 + 2) squared") |
||
for n in 0:7 |
for n in 0:7 |
||
println(rpad(n, 14), rpad((big"10"^(n+1) - 1) ÷ 9 + 2, 19), ((big"10"^(n+1) - 1) ÷ 9 + 2)^2) |
println(rpad(n, 14), rpad((big"10"^(n+1) - 1) ÷ 9 + 2, 19), ((big"10"^(n+1) - 1) ÷ 9 + 2)^2) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
n (10^(n+1) - 1) ÷ 9 + 2) squared |
n (10^(n+1) - 1) ÷ 9 + 2) squared |
||
Line 557: | Line 557: | ||
</pre> |
</pre> |
||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
VECTOR VALUES FMT = $I8,7H **2 = ,I15*$ |
VECTOR VALUES FMT = $I8,7H **2 = ,I15*$ |
||
THROUGH LOOP, FOR I=0, 1, I.G.7 |
THROUGH LOOP, FOR I=0, 1, I.G.7 |
||
Line 565: | Line 565: | ||
N = N*10 + 3 |
N = N*10 + 3 |
||
LOOP PRINT FORMAT FMT,N,N*N |
LOOP PRINT FORMAT FMT,N,N*N |
||
END OF PROGRAM</ |
END OF PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3**2 = 9 |
<pre> 3**2 = 9 |
||
Line 577: | Line 577: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">{#, #^2} & /@ |
||
Table[FromDigits[PadLeft[{3}, n, 1]], {n, 9}] // TableForm</ |
Table[FromDigits[PadLeft[{3}, n, 1]], {n, 9}] // TableForm</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
Line 593: | Line 593: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
iterator genNumbers(maxOnes: Natural): int = |
iterator genNumbers(maxOnes: Natural): int = |
||
Line 603: | Line 603: | ||
for i in genNumbers(7): |
for i in genNumbers(7): |
||
echo &"{i:8} {i*i:18}"</ |
echo &"{i:8} {i*i:18}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 617: | Line 617: | ||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
Only 5 entries are shown due to Plain English's 32-bit signed integers. |
Only 5 entries are shown due to Plain English's 32-bit signed integers. |
||
< |
<syntaxhighlight lang="plain english">To run: |
||
Start up. |
Start up. |
||
Put 0 into a counter. |
Put 0 into a counter. |
||
Line 633: | Line 633: | ||
Repeat. |
Repeat. |
||
Wait for the escape key. |
Wait for the escape key. |
||
Shut down.</ |
Shut down.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 646: | Line 646: | ||
===One Liner=== |
===One Liner=== |
||
The most Pythonic way............. |
The most Pythonic way............. |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
[print("( " + "1"*i + "3 ) ^ 2 = " + str(int("1"*i + "3")**2)) for i in range(0,8)] |
[print("( " + "1"*i + "3 ) ^ 2 = " + str(int("1"*i + "3")**2)) for i in range(0,8)] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 662: | Line 662: | ||
===Procedural=== |
===Procedural=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/python |
||
def make13(n): |
def make13(n): |
||
Line 673: | Line 673: | ||
for n in range(0,7): |
for n in range(0,7): |
||
m = make13(n)**2 |
m = make13(n)**2 |
||
print("{:d}\t\t{:d}".format(make13(n),m))</ |
print("{:d}\t\t{:d}".format(make13(n),m))</syntaxhighlight> |
||
===Functional=== |
===Functional=== |
||
Taking the first n terms from an infinite series: |
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 |
from itertools import islice |
||
Line 764: | Line 764: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 3^2 = 9 |
<pre> 3^2 = 9 |
||
Line 776: | Line 776: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ char 1 swap of |
||
char 3 join |
char 3 join |
||
$->n drop ] is 1's+3 ( n -> n ) |
$->n drop ] is 1's+3 ( n -> n ) |
||
Line 784: | Line 784: | ||
dup echo |
dup echo |
||
say " --> " |
say " --> " |
||
dup * echo cr ]</ |
dup * echo cr ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 798: | Line 798: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">Make13(n)=m=0;while(n>0,m=10*(m+1);n=n-1);m=3+m;return(m) |
||
for(i=0,7,print(Make13(i)," ",Make13(i)^2))</ |
for(i=0,7,print(Make13(i)," ",Make13(i)^2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 9 |
<pre>3 9 |
||
Line 813: | Line 813: | ||
==={{header|Free Pascal}}=== |
==={{header|Free Pascal}}=== |
||
like [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix|Phix]] using string |
like [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix|Phix]] using string |
||
< |
<syntaxhighlight lang="pascal"> |
||
program OnesAppend3AndSquare; |
program OnesAppend3AndSquare; |
||
const |
const |
||
Line 893: | Line 893: | ||
writeln(s:2*MAX+2); |
writeln(s:2*MAX+2); |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out|@TIO.RUN}} |
{{out|@TIO.RUN}} |
||
<pre> |
<pre> |
||
Line 912: | Line 912: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared |
use strict; # https://rosettacode.org/wiki/Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared |
||
Line 922: | Line 922: | ||
my $number = 1 x $_ . 3; |
my $number = 1 x $_ . 3; |
||
print "$number ", $number ** 2, "\n"; |
print "$number ", $number ** 2, "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 937: | Line 937: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Perfect opportunity for a little string math, why not... |
Perfect opportunity for a little string math, why not... |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">37</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">37</span> <span style="color: #008080;">do</span> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'3'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">'9'</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'3'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">'9'</span> |
||
Line 951: | Line 951: | ||
<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;">"%38s %75s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">'3'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</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;">"%38s %75s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">'3'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 997: | Line 997: | ||
In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime. |
In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime. |
||
<lang |
<syntaxhighlight lang="raku" line>say "$_, {.²}" for (^∞).map({ ( 1 x $_ ~ 3)} ).grep({ .is-prime })[^8]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3, 9 |
<pre>3, 9 |
||
Line 1,010: | Line 1,010: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
A little extra code was added to pre-compute the biggest number to find the widths for output alignment. |
A little extra code was added to pre-compute the biggest number to find the widths for output alignment. |
||
< |
<syntaxhighlight 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. */ |
numeric digits 1000 /*be able to handle huge numbers. */ |
||
parse arg n . /*obtain optional argument from the CL.*/ |
parse arg n . /*obtain optional argument from the CL.*/ |
||
Line 1,023: | Line 1,023: | ||
exit 0 /*stick a fork in it, we're all done. */ |
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 ?</ |
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</syntaxhighlight> |
||
{{out|output|text= when using the input of: <tt> 37 </tt>}} |
{{out|output|text= when using the input of: <tt> 37 </tt>}} |
||
Line 1,069: | Line 1,069: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,094: | Line 1,094: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,110: | Line 1,110: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 1,121: | Line 1,121: | ||
writeln(a <& " " <& a * a); |
writeln(a <& " " <& a * a); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,135: | Line 1,135: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">0..^8 -> each {|n| |
||
var k = ((10**(n+1) - 1)/9 + 2) |
var k = ((10**(n+1) - 1)/9 + 2) |
||
say [k, k**2] |
say [k, k**2] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,153: | Line 1,153: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var a = Fn.new { |n| |
var a = Fn.new { |n| |
||
Line 1,161: | Line 1,161: | ||
} |
} |
||
for (n in 0..7) a.call(n)</ |
for (n in 0..7) a.call(n)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,177: | Line 1,177: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
Only 32-bit integers are available, but the standard 64-bit floating point (real) provides 15 decimal digits. |
Only 32-bit integers are available, but the standard 64-bit floating point (real) provides 15 decimal digits. |
||
< |
<syntaxhighlight lang="xpl0">int N, M; |
||
real X; |
real X; |
||
[Format(16, 0); |
[Format(16, 0); |
||
Line 1,189: | Line 1,189: | ||
CrLf(0); |
CrLf(0); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 14:49, 28 August 2022
- Task
Show here (on this page) the decimal numbers formed by:
- (n 1's appended by the digit 3) and then square the result, where 0 <= n < 8
11l
L(i) 0..7 {print(‘( ’(‘1’ * i)‘3 ) ^ 2 = ’(Int64((‘1’ * i)‘3’) ^ 2))}
- Output:
( 3 ) ^ 2 = 9 ( 13 ) ^ 2 = 169 ( 113 ) ^ 2 = 12769 ( 1113 ) ^ 2 = 1238769 ( 11113 ) ^ 2 = 123498769 ( 111113 ) ^ 2 = 12346098769 ( 1111113 ) ^ 2 = 1234572098769 ( 11111113 ) ^ 2 = 123456832098769
Ada
with Ada.Text_Io;
with Ada.Numerics.Big_Numbers.Big_Integers;
procedure Ones_Plus_Three is
use Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Text_Io;
Root : Big_Natural := 3;
Squared : Big_Natural;
begin
for N in 0 .. 8 loop
Squared := Root**2;
Put (To_String (Root, Width => 12));
Put (" ");
Put (To_String (Squared, Width => 20));
New_Line;
Root := @ + 10**(N + 1);
end loop;
end Ones_Plus_Three;
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769 111111113 12345679432098769
ALGOL 68
Assuming LONG INT is large enough (as in e.g. ALGOL 68G).
BEGIN
LONG INT n := 0;
FOR i TO 8 DO
LONG INT n3 = ( n * 10 ) + 3;
print( ( whole( n3, 0 ), " ", whole( n3 * n3, 0 ), newline ) );
n *:= 10 +:= 1
OD
END
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Alternative version that shows the values for higher numbers of ones.
BEGIN
PR precision 250 PR
LONG LONG INT n := 0;
FOR i FROM 0 TO 111 DO
LONG LONG INT n3 = ( n * 10 ) + 3;
IF i > 84 THEN
STRING v := whole( n3 * n3, 0 );
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 : ]
OD;
pattern := "987654320";
WHILE string in string( pattern, pos, v ) DO
v := v[ 1 : pos - 1 ] + "Z" + v[ pos + p len : ]
OD;
print( ( whole( i, -3 ), " ", v, newline ) )
FI;
n *:= 10 +:= 1
OD
END
- Output:
As the 111...113^2 values get rather large, the code above replaces "123456790" with "A" and "987654320" with "Z". The number of ones is shown on the left.
85 AAAAAAAAA1234ZZZZZZZZZ98769 86 AAAAAAAAA123460ZZZZZZZZZ98769 87 AAAAAAAAA12345720ZZZZZZZZZ98769 88 AAAAAAAAA1234568320ZZZZZZZZZ98769 89 AAAAAAAAA123456794320ZZZZZZZZZ98769 90 AAAAAAAAAA54320ZZZZZZZZZ98769 91 AAAAAAAAAA1654320ZZZZZZZZZ98769 92 AAAAAAAAAA127654320ZZZZZZZZZ98769 93 AAAAAAAAAA12387654320ZZZZZZZZZ98769 94 AAAAAAAAAA1234ZZZZZZZZZZ98769 95 AAAAAAAAAA123460ZZZZZZZZZZ98769 96 AAAAAAAAAA12345720ZZZZZZZZZZ98769 97 AAAAAAAAAA1234568320ZZZZZZZZZZ98769 98 AAAAAAAAAA123456794320ZZZZZZZZZZ98769 99 AAAAAAAAAAA54320ZZZZZZZZZZ98769 100 AAAAAAAAAAA1654320ZZZZZZZZZZ98769 101 AAAAAAAAAAA127654320ZZZZZZZZZZ98769 102 AAAAAAAAAAA12387654320ZZZZZZZZZZ98769 103 AAAAAAAAAAA1234ZZZZZZZZZZZ98769 104 AAAAAAAAAAA123460ZZZZZZZZZZZ98769 105 AAAAAAAAAAA12345720ZZZZZZZZZZZ98769 106 AAAAAAAAAAA1234568320ZZZZZZZZZZZ98769 107 AAAAAAAAAAA123456794320ZZZZZZZZZZZ98769 108 AAAAAAAAAAAA54320ZZZZZZZZZZZ98769 109 AAAAAAAAAAAA1654320ZZZZZZZZZZZ98769 110 AAAAAAAAAAAA127654320ZZZZZZZZZZZ98769 111 AAAAAAAAAAAA12387654320ZZZZZZZZZZZ98769
Arturo
loop 0..7 'x [
num: to :integer(repeat "1" x) ++ "3"
print [num num^2]
]
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
AWK
# syntax: GAWK -f SHOW_THE_DECIMAL_VALUE_OF_A_NUMBER_OF_1S_APPENDED_WITH_A_3_THEN_SQUARED.AWK
# converted from FreeBASIC
BEGIN {
for (i=0; i<=7; i++) {
m = make13(i)
printf("%1d %9s^2 %'20d\n",i,m,m*m)
}
exit(0)
}
function make13(n, t) {
while (n--) {
t = 10 * (t+1)
}
return(t+3)
}
- Output:
0 3^2 9 1 13^2 169 2 113^2 12,769 3 1113^2 1,238,769 4 11113^2 123,498,769 5 111113^2 12,346,098,769 6 1111113^2 1,234,572,098,769 7 11111113^2 123,456,832,098,769
C
#include <stdio.h>
#include <stdint.h>
uint64_t ones_plus_three(uint64_t ones) {
uint64_t r = 0;
while (ones--) r = r*10 + 1;
return r*10 + 3;
}
int main() {
uint64_t n;
for (n=0; n<8; n++) {
uint64_t x = ones_plus_three(n);
printf("%8lu^2 = %15lu\n", x, x*x);
}
return 0;
}
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769 11111113^2 = 123456832098769
C#
For 0 <= n < 22
using System; using BI = System.Numerics.BigInteger;
class Program { static void Main(string[] args) {
for (BI x = 3; BI.Log10(x) < 22; x = (x - 2) * 10 + 3)
Console.WriteLine("{1,43} {0,-20}", x, x * x); } }
- Output:
9 3 169 13 12769 113 1238769 1113 123498769 11113 12346098769 111113 1234572098769 1111113 123456832098769 11111113 12345679432098769 111111113 1234567905432098769 1111111113 123456790165432098769 11111111113 12345679012765432098769 111111111113 1234567901238765432098769 1111111111113 123456790123498765432098769 11111111111113 12345679012346098765432098769 111111111111113 1234567901234572098765432098769 1111111111111113 123456790123456832098765432098769 11111111111111113 12345679012345679432098765432098769 111111111111111113 1234567901234567905432098765432098769 1111111111111111113 123456790123456790165432098765432098769 11111111111111111113 12345679012345679012765432098765432098769 111111111111111111113 1234567901234567901238765432098765432098769 1111111111111111111113
CLU
ones_plus_three = proc (n: int) returns (int)
r: int := 0
for i: int in int$from_to(1, n) do
r := r*10 + 1
end
return(r*10 + 3)
end ones_plus_three
start_up = proc ()
po: stream := stream$primary_output()
for i: int in int$from_to(0, 7) do
n: int := ones_plus_three(i)
nsq: int := n**2
stream$putright(po, int$unparse(n), 8)
stream$puts(po, "^2 = ")
stream$putright(po, int$unparse(nsq), 15)
stream$putl(po, "")
end
end start_up
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769 11111113^2 = 123456832098769
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. ONES-THREE-SQUARED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 N PIC 9.
03 ONES-3 PIC 9(9).
03 SQUARE PIC 9(15).
01 FMT.
03 FMT-ONES-3 PIC Z(7)9.
03 FILLER PIC X(5) VALUE "^2 = ".
03 FMT-SQUARE PIC Z(14)9.
PROCEDURE DIVISION.
BEGIN.
PERFORM N-ONES-3 VARYING N FROM 0 BY 1 UNTIL N IS EQUAL TO 8.
STOP RUN.
N-ONES-3.
MOVE ZERO TO ONES-3.
PERFORM ADD-ONE N TIMES.
MULTIPLY 10 BY ONES-3.
ADD 3 TO ONES-3.
MULTIPLY ONES-3 BY ONES-3 GIVING SQUARE.
MOVE ONES-3 TO FMT-ONES-3.
MOVE SQUARE TO FMT-SQUARE.
DISPLAY FMT.
ADD-ONE.
MULTIPLY 10 BY ONES-3.
ADD 1 TO ONES-3.
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769 11111113^2 = 123456832098769
F#
[3L;13L;113L;1113L;11113L;111113L;1111113L;11111113L;111111113L]|>List.iter(fun n->printfn "%10d->%d" n (n*n))
- Output:
3->9 13->169 113->12769 1113->1238769 11113->123498769 111113->12346098769 1111113->1234572098769 11111113->123456832098769 111111113->12345679432098769
Factor
a(n) = ((10n+1 - 1) / 9 + 2)2
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
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Fermat
Func Make13(n) = m:=0; while n>0 do m:=10*(m+1);n:=n-1; od; m:=3+m; m.
for i=0 to 7 do !Make13(i);!' ';!Make13(i)^2;!!'' od
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
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
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Forth
: 1s+3
0 swap
begin dup while
swap 10 * 1+ swap 1-
repeat
drop 10 * 3 +
;
: sqr dup * ;
: show dup . ." ^2 = " sqr . cr ;
: show-upto
0 swap
begin over over < while
swap dup 1s+3 show 1+ swap
repeat
2drop
;
8 show-upto
bye
- Output:
3 ^2 = 9 13 ^2 = 169 113 ^2 = 12769 1113 ^2 = 1238769 11113 ^2 = 123498769 111113 ^2 = 12346098769 1111113 ^2 = 1234572098769 11111113 ^2 = 123456832098769
Go
package main
import (
"fmt"
"strconv"
"strings"
)
func a(n int) {
s, _ := strconv.Atoi(strings.Repeat("1", n) + "3")
t := s * s
fmt.Printf("%d %d\n", s, t)
}
func main() {
for n := 0; n <= 7; n++ {
a(n)
}
}
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Haskell
import Text.Printf (printf)
onesPlusThree :: [Integer]
onesPlusThree =
(3 +) . (10 *)
<$> iterate (succ . (10 *)) 0
format :: Integer -> String
format = printf "%8lu^2 = %15lu" <*> (^ 2)
main :: IO ()
main =
(putStr . unlines . take 8) $
format <$> onesPlusThree
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769 11111113^2 = 123456832098769
jq
Works with gojq, the Go implementation of jq
For large values of n, the unbounded-precision integer arithmetic of gojq will ensure accuracy.
# For gojq
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
# For pretty-printing
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
" n number number^2",
(range(0;8) as $n
| ((("1"*$n) + "3") | tonumber) as $number
| ($n|lpad(3)) + ($number|lpad(10)) + ($number|power(2)|lpad(20)) )
- Output:
n number number^2 0 3 9 1 13 169 2 113 12769 3 1113 1238769 4 11113 123498769 5 111113 12346098769 6 1111113 1234572098769 7 11111113 123456832098769
For 100 <= n <= 105
Encoding "123456790" as "A", "987654320" as Z", and lastly "9876" as "N":
range(100; 106) as $n
| ((("1"*$n) + "3") | tonumber) as $number
| ($n|lpad(4)) + " "
+ ($number|power(2)|tostring| gsub("123456790";"A") | gsub("987654320";"Z") | gsub("9876";"N") | lpad(40))
- Output:
100 AAAAAAAAAAA1654320ZZZZZZZZZZN9 101 AAAAAAAAAAA127654320ZZZZZZZZZZN9 102 AAAAAAAAAAA12387654320ZZZZZZZZZZN9 103 AAAAAAAAAAA1234ZZZZZZZZZZZN9 104 AAAAAAAAAAA123460ZZZZZZZZZZZN9 105 AAAAAAAAAAA12345720ZZZZZZZZZZZN9
Julia
println("n (10^(n+1) - 1) ÷ 9 + 2) squared")
for n in 0:7
println(rpad(n, 14), rpad((big"10"^(n+1) - 1) ÷ 9 + 2, 19), ((big"10"^(n+1) - 1) ÷ 9 + 2)^2)
end
- Output:
n (10^(n+1) - 1) ÷ 9 + 2) squared 0 3 9 1 13 169 2 113 12769 3 1113 1238769 4 11113 123498769 5 111113 12346098769 6 1111113 1234572098769 7 11111113 123456832098769
MAD
NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $I8,7H **2 = ,I15*$
THROUGH LOOP, FOR I=0, 1, I.G.7
N = 0
THROUGH ONES, FOR J=1, 1, J.G.I
ONES N = N*10 + 1
N = N*10 + 3
LOOP PRINT FORMAT FMT,N,N*N
END OF PROGRAM
- Output:
3**2 = 9 13**2 = 169 113**2 = 12769 1113**2 = 1238769 11113**2 = 123498769 111113**2 = 12346098769 1111113**2 = 1234572098769 11111113**2 = 123456832098769
Mathematica / Wolfram Language
{#, #^2} & /@
Table[FromDigits[PadLeft[{3}, n, 1]], {n, 9}] // TableForm
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769 111111113 12345679432098769
Nim
import strformat
iterator genNumbers(maxOnes: Natural): int =
var ones = 0
yield 3
for _ in 1..maxOnes:
ones = 10 * ones + 10
yield ones + 3
for i in genNumbers(7):
echo &"{i:8} {i*i:18}"
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Plain English
Only 5 entries are shown due to Plain English's 32-bit signed integers.
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.
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769
Python
One Liner
The most Pythonic way.............
[print("( " + "1"*i + "3 ) ^ 2 = " + str(int("1"*i + "3")**2)) for i in range(0,8)]
- Output:
( 3 ) ^ 2 = 9 ( 13 ) ^ 2 = 169 ( 113 ) ^ 2 = 12769 ( 1113 ) ^ 2 = 1238769 ( 11113 ) ^ 2 = 123498769 ( 111113 ) ^ 2 = 12346098769 ( 1111113 ) ^ 2 = 1234572098769 ( 11111113 ) ^ 2 = 123456832098769
Procedural
#!/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))
Functional
Taking the first n terms from an infinite series:
'''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()
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769
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 ]
- Output:
3 --> 9 13 --> 169 113 --> 12769 1113 --> 1238769 11113 --> 123498769 111113 --> 12346098769 1111113 --> 1234572098769 11111113 --> 123456832098769
PARI/GP
Make13(n)=m=0;while(n>0,m=10*(m+1);n=n-1);m=3+m;return(m)
for(i=0,7,print(Make13(i)," ",Make13(i)^2))
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Pascal
Free Pascal
like Phix using string
program OnesAppend3AndSquare;
const
MAX = 10;//37
procedure AddNumStrings(var s:Ansistring;const n:Ansistring;Offset:integer);
var
l,sum,carry : integer;
begin
l := length(n);
carry := 0;
repeat
sum := Ord(s[l+Offset])+Ord(n[l])-2*Ord('0')+carry;
If sum >= 10 then
begin
dec(sum,10);
carry := 1;
end
else
carry := 0;
s[l+Offset]:= chr(sum+Ord('0'));
dec(l)
until l <= 0;
//correct carry, never used for '3' but > '3' only once
while (Offset>0) AND(carry = 1) do
begin
sum := Ord(s[Offset])-Ord('0')+carry;
If sum >= 10 then
begin
dec(sum,10);
carry := 1;
end
else
carry := 0;
s[Offset]:= chr(sum+Ord('0'));
dec(Offset)
end;
end;
procedure OnesAppend3AndSquare(CntOfOnes:integer;var s:AnsiString);
var
fac : Ansistring;
i,l : integer;
begin
l := CntOfOnes+1;
setlength(fac,l);
FillChar(fac[1],l-1,'1');
fac[l]:= '3';
write(fac:MAX+1);
repeat
i := Ord(fac[l])-Ord('0');
if i > 0 then
repeat
AddNumStrings(s,fac,l);
dec(i)
until i<=0;
dec(l);
until l <=0;
end;
var
s : AnsiString;
i,j,l : integer;
Begin
For i := 0 to MAX do
begin
l := i+1;
inc(l,l);
setlength(s,l);
Fillchar(s[1],l,'0');
OnesAppend3AndSquare(i,s);
//remove leading '0'
j := 1;
while (j < l) AND (s[j] = '0') do
begin
s[j]:= ' ';
inc(j);
end;
writeln(s:2*MAX+2);
end;
end.
- @TIO.RUN:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769 111111113 12345679432098769 1111111113 1234567905432098769 11111111113 123456790165432098769 ..37 '1' 11111111111111111111111111111111111113 123456790123456790123456790123456790165432098765432098765432098765432098769
Perl
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared
use warnings;
#use bignum; # uncomment for larger than 9 or 32-bit perls
for ( 0 .. 7 )
{
my $number = 1 x $_ . 3;
print "$number ", $number ** 2, "\n";
}
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Phix
Perfect opportunity for a little string math, why not...
for n=0 to 37 do string res = repeat('3',n)&'9' for i=1 to n do res = "0" & res integer digit = 3 for j=length(res)-i to 1 by -1 do digit += res[j]-'0' res[j] = remainder(digit,10)+'0' digit = floor(digit/10)+1 end for end for printf(1,"%38s %75s\n",{repeat('1',n)&'3',res}) end for
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769 111111113 12345679432098769 1111111113 1234567905432098769 11111111113 123456790165432098769 111111111113 12345679012765432098769 1111111111113 1234567901238765432098769 11111111111113 123456790123498765432098769 111111111111113 12345679012346098765432098769 1111111111111113 1234567901234572098765432098769 11111111111111113 123456790123456832098765432098769 111111111111111113 12345679012345679432098765432098769 1111111111111111113 1234567901234567905432098765432098769 11111111111111111113 123456790123456790165432098765432098769 111111111111111111113 12345679012345679012765432098765432098769 1111111111111111111113 1234567901234567901238765432098765432098769 11111111111111111111113 123456790123456790123498765432098765432098769 111111111111111111111113 12345679012345679012346098765432098765432098769 1111111111111111111111113 1234567901234567901234572098765432098765432098769 11111111111111111111111113 123456790123456790123456832098765432098765432098769 111111111111111111111111113 12345679012345679012345679432098765432098765432098769 1111111111111111111111111113 1234567901234567901234567905432098765432098765432098769 11111111111111111111111111113 123456790123456790123456790165432098765432098765432098769 111111111111111111111111111113 12345679012345679012345679012765432098765432098765432098769 1111111111111111111111111111113 1234567901234567901234567901238765432098765432098765432098769 11111111111111111111111111111113 123456790123456790123456790123498765432098765432098765432098769 111111111111111111111111111111113 12345679012345679012345679012346098765432098765432098765432098769 1111111111111111111111111111111113 1234567901234567901234567901234572098765432098765432098765432098769 11111111111111111111111111111111113 123456790123456790123456790123456832098765432098765432098765432098769 111111111111111111111111111111111113 12345679012345679012345679012345679432098765432098765432098765432098769 1111111111111111111111111111111111113 1234567901234567901234567901234567905432098765432098765432098765432098769 11111111111111111111111111111111111113 123456790123456790123456790123456790165432098765432098765432098765432098769
Raku
In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime.
say "$_, {.²}" for (^∞).map({ ( 1 x $_ ~ 3)} ).grep({ .is-prime })[^8]
- 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.
/*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 ?
- 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
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
- Output:
working... {3,9} {13,169} {113,12769} {1113,1238769} {11113,123498769} {111113,12346098769} {1111113,1234572098769} {11111113,123456832098769} done...
Seed7
$ include "seed7_05.s7i";
const proc: main is func
local
var integer: a is 0;
var integer: n is 0;
begin
for n range 0 to 7 do
a := (10 ** (n + 1) - 1) div 9 + 2;
writeln(a <& " " <& a * a);
end for;
end func;
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Sidef
0..^8 -> each {|n|
var k = ((10**(n+1) - 1)/9 + 2)
say [k, k**2]
}
- Output:
[3, 9] [13, 169] [113, 12769] [1113, 1238769] [11113, 123498769] [111113, 12346098769] [1111113, 1234572098769] [11111113, 123456832098769]
Wren
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)
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
XPL0
Only 32-bit integers are available, but the standard 64-bit floating point (real) provides 15 decimal digits.
int N, M;
real X;
[Format(16, 0);
for N:= 0 to 8-1 do
[X:= 0.;
for M:= 0 to N-1 do
X:= X*10. + 1.;
X:= X*10. + 3.;
RlOut(0, X);
RlOut(0, X*X);
CrLf(0);
];
]
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769