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

m
syntax highlighting fixup automation
(Added Arturo implementation)
m (syntax highlighting fixup automation)
Line 9:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">L(i) 0..7 {print(‘( ’(‘1’ * i)‘3 ) ^ 2 = ’(Int64((‘1’ * i)‘3’) ^ 2))}</langsyntaxhighlight>
 
{{out}}
Line 24:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Numerics.Big_Numbers.Big_Integers;
 
Line 44:
Root := @ + 10**(N + 1);
end loop;
end Ones_Plus_Three;</langsyntaxhighlight>
{{out}}
<pre> 3 9
Line 58:
=={{header|ALGOL 68}}==
Assuming LONG INT is large enough (as in e.g. ALGOL 68G).
<langsyntaxhighlight lang="algol68">BEGIN
LONG INT n := 0;
FOR i TO 8 DO
Line 65:
n *:= 10 +:= 1
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 80:
Alternative version that shows the values for higher numbers of ones.
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68">BEGIN
PR precision 250 PR
LONG LONG INT n := 0;
Line 101:
n *:= 10 +:= 1
OD
END</langsyntaxhighlight>
 
{{out}}
Line 137:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">loop 0..7 'x [
num: to :integer(repeat "1" x) ++ "3"
print [num num^2]
]</langsyntaxhighlight>
 
{{out}}
Line 154:
 
=={{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
# converted from FreeBASIC
Line 170:
return(t+3)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 184:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
 
Line 200:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 216:
{{libheader|System.Numerics}}
For 0 <= n < 22
<langsyntaxhighlight lang="csharp">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); } }</langsyntaxhighlight>
{{out}}
<pre> 9 3
Line 245:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">ones_plus_three = proc (n: int) returns (int)
r: int := 0
for i: int in int$from_to(1, n) do
Line 265:
stream$putl(po, "")
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 3^2 = 9
Line 277:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ONES-THREE-SQUARED.
 
Line 308:
ADD-ONE.
MULTIPLY 10 BY ONES-3.
ADD 1 TO ONES-3.</langsyntaxhighlight>
{{out}}
<pre> 3^2 = 9
Line 320:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
[3L;13L;113L;1113L;11113L;111113L;1111113L;11111113L;111111113L]|>List.iter(fun n->printfn "%10d->%d" n (n*n))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 339:
<big>{{math|a(<var>n</var>) &#61; ((10<sup><var>n</var>+1</sup> - 1) / 9 + 2)<sup>2</sup> }}</big>
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight 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</langsyntaxhighlight>
{{out}}
<pre>
Line 357:
 
=={{header|Fermat}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
{{out}}
<pre>
Line 372:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function make13(n as uinteger) as uinteger
dim as uinteger t = 0
while n
Line 386:
m = make13(n)^2
print make13(n), m
next n</langsyntaxhighlight>
{{out}}
<pre>
Line 400:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: 1s+3
0 swap
begin dup while
Line 420:
 
8 show-upto
bye</langsyntaxhighlight>
 
{{out}}
Line 435:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 453:
a(n)
}
}</langsyntaxhighlight>
 
{{out}}
Line 468:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
 
onesPlusThree :: [Integer]
Line 481:
main =
(putStr . unlines . take 8) $
format <$> onesPlusThree</langsyntaxhighlight>
{{out}}
 
Line 498:
 
For large values of n, the unbounded-precision integer arithmetic of gojq will ensure accuracy.
<syntaxhighlight lang="jq">
<lang jq>
# For gojq
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
Line 508:
(range(0;8) as $n
| ((("1"*$n) + "3") | tonumber) as $number
| ($n|lpad(3)) + ($number|lpad(10)) + ($number|power(2)|lpad(20)) )</langsyntaxhighlight>
{{out}}
<pre>
Line 524:
 
Encoding "123456790" as "A", "987654320" as Z", and lastly "9876" as "N":
<langsyntaxhighlight lang="jq">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))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 540:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="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
</langsyntaxhighlight>{{out}}
<pre>
n (10^(n+1) - 1) ÷ 9 + 2) squared
Line 557:
</pre>
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $I8,7H **2 = ,I15*$
THROUGH LOOP, FOR I=0, 1, I.G.7
Line 565:
N = N*10 + 3
LOOP PRINT FORMAT FMT,N,N*N
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre> 3**2 = 9
Line 577:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">{#, #^2} & /@
Table[FromDigits[PadLeft[{3}, n, 1]], {n, 9}] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 593:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
iterator genNumbers(maxOnes: Natural): int =
Line 603:
 
for i in genNumbers(7):
echo &"{i:8} {i*i:18}"</langsyntaxhighlight>
 
{{out}}
Line 617:
=={{header|Plain English}}==
Only 5 entries are shown due to Plain English's 32-bit signed integers.
<langsyntaxhighlight lang="plain english">To run:
Start up.
Put 0 into a counter.
Line 633:
Repeat.
Wait for the escape key.
Shut down.</langsyntaxhighlight>
{{out}}
<pre>
Line 646:
===One Liner===
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)]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 662:
===Procedural===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def make13(n):
Line 673:
for n in range(0,7):
m = make13(n)**2
print("{:d}\t\t{:d}".format(make13(n),m))</langsyntaxhighlight>
 
===Functional===
 
Taking the first n terms from an infinite series:
<langsyntaxhighlight lang="python">'''Sequence of 1s appended with a 3, then squared'''
 
from itertools import islice
Line 764:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 3^2 = 9
Line 776:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ char 1 swap of
char 3 join
$->n drop ] is 1's+3 ( n -> n )
Line 784:
dup echo
say " --> "
dup * echo cr ]</langsyntaxhighlight>
 
{{out}}
Line 798:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight 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))</langsyntaxhighlight>
{{out}}
<pre>3 9
Line 813:
==={{header|Free Pascal}}===
like [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix|Phix]] using string
<langsyntaxhighlight lang="pascal">
program OnesAppend3AndSquare;
const
Line 893:
writeln(s:2*MAX+2);
end;
end.</langsyntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 912:
 
=={{header|Perl}}==
<langsyntaxhighlight 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
Line 922:
my $number = 1 x $_ . 3;
print "$number ", $number ** 2, "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 937:
=={{header|Phix}}==
Perfect opportunity for a little string math, why not...
<!--<langsyntaxhighlight Phixlang="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: #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:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 997:
In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime.
 
<syntaxhighlight lang="raku" perl6line>say "$_, {.²}" for (^∞).map({ ( 1 x $_ ~ 3)} ).grep({ .is-prime })[^8]</langsyntaxhighlight>
{{out}}
<pre>3, 9
Line 1,010:
=={{header|REXX}}==
A little extra code was added to pre-compute the biggest number to find the widths for output alignment.
<langsyntaxhighlight 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.*/
Line 1,023:
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 ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 37 </tt>}}
 
Line 1,069:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,094:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,110:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,121:
writeln(a <& " " <& a * a);
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,135:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">0..^8 -> each {|n|
var k = ((10**(n+1) - 1)/9 + 2)
say [k, k**2]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,153:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var a = Fn.new { |n|
Line 1,161:
}
 
for (n in 0..7) a.call(n)</langsyntaxhighlight>
 
{{out}}
Line 1,177:
=={{header|XPL0}}==
Only 32-bit integers are available, but the standard 64-bit floating point (real) provides 15 decimal digits.
<langsyntaxhighlight XPL0lang="xpl0">int N, M;
real X;
[Format(16, 0);
Line 1,189:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits