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

m
m (sort languages alphabetically)
m (→‎{{header|Wren}}: Minor tidy)
(One intermediate revision by one other user not shown)
Line 807:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
like [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix|Phix]] using stringbyte, and convert than to ASCII.
Because the values are calculated one by one, one can use addition to get rid of calculating squares, by using binomial formula.
<syntaxhighlight lang="pascal">
The best is shown in [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#ALGOL_68|Algol_68]] that it ends in a cyclic pattern of lenght 9.
<syntaxhighlight lang="pascal">{
10^pot+k -> prepend a 1 1113-> 11113
(10^pot+k)^2 = 10^(2*pot)+ 2*10^pot*k + k^2
(10^pot+k)^2 = 10^pot*(10^pot+2*k) + k^2
s_lastsqr = k*k
s_DeltaSqr = (10^pot+2*k) => 1222....2226
shift s_DeltaSqr by pot digits in SumMyDeltaSqr => 10^pot*(10^pot+2*k)
}
program OnesAppend3AndSquare;
const
MAX = 103700;//37
type
tmyNumb = array of byte;
var
res :AnsiString;
 
procedure AddNumStringsOutMyNumb(var s:Ansistring;const n:Ansistring tmyNUmb;Offsetl,w: integer);
var
li,sum,carryofs : integer;
begin
l :+= length(n)1;
carryif :=w 0;< l then
w := l+1;
repeat
setlength(res,w);
sum := Ord(s[l+Offset])+Ord(n[l])-2*Ord('0')+carry;
fillchar(res[1],w,' ');
If sum >= 10 then
ofs := beginw-l;
For i := 1 dec(sum,10);to l do
carryres[i+ofs] := 1chr(n[l-i]+48);
write(res);
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 Out_k_sqr(const k,sqr_k: tmyNUmb;pot:integer);
procedure OnesAppend3AndSquare(CntOfOnes:integer;var s:AnsiString);
var
facdgtcnt : Ansistringinteger;
Begin
i,l : integer;
write(pot:4);
dgtcnt := 22;
if pot > 10 then
dgtcnt := 78;
OutMyNumb(k,pot,dgtcnt-4);
if pot > 10 then
writeln;
OutMyNumb(sqr_k,2*pot,dgtcnt);
writeln;
end;
procedure SumMyDeltaSqr(var res:tmyNUmb;const s:tmyNumb;pot: integer);
//res = s_lastsqr
//s = s_DeltaSqr
//shift s by l => (10^pot) * s_DeltaSqr = 10^pot*(10^pot+2*k)
var
i,sum,carry : integer;
begin
lcarry := CntOfOnes+10;
For i := 0 to pot+1 do
setlength(fac,l);
begin
FillChar(fac[1],l-1,'1');
sum := res[i+pot]+s[i]+carry;
fac[l]:= '3';
carry := ord(sum>9);
write(fac:MAX+1);
sum -= (-carry) AND 10;
repeat
res[i+pot] := Ord(fac[l])-Ord('0')sum;
end;
if i > 0 then
repeat
AddNumStrings(s,fac,l);
dec(i)
until i<=0;
dec(l);
until l <=0;
end;
 
var
s,s_DeltaSqr,s_lastsqr : tmyNumb;
s : AnsiString;
i,j,l pot: integer;
Begin
setlength(s,MAX);
For i := 0 to MAX do
setlength(s_DeltaSqr,Max+1);
begin
setlength(s_lastsqr,2*Max+1);
l := i+1;
pot inc(l,l):= 0;
s[pot] := 3;
setlength(s,l);
Fillchar(ss_lastsqr[1pot],l,'0') := 9;
repeat
OnesAppend3AndSquare(i,s);
SumMyDeltaSqr(s_lastsqr,s_DeltaSqr,pot);
//remove leading '0'
j :=if 1;pot < 10 then
Out_k_sqr(s,s_lastsqr,pot);
while (j < l) AND (s[j] = '0') do
if pot = 37 then
begin
Out_k_sqr(s[j]:= ' ',s_lastsqr,pot);
 
inc(j);
if pot > 0 then
end;
s_DeltaSqr[pot]:= 2 // =>2*s[i] 2222...26
writeln(s:2*MAX+2);
end; else
s_DeltaSqr[0] := 6;// =>2*s[0] 6
end.</syntaxhighlight>
inc(pot);
s_DeltaSqr[pot]:= 1; //1...6
s[pot] := 1;
until pot = MAX;
Writeln('Finished til ',MAX);
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
0 3 9
1 13 169
2 113 12769
3 1113 1238769
4 11113 123498769
5 111113 12346098769
6 1111113 1234572098769
7 11111113 123456832098769
8 111111113 12345679432098769
9 1111111113 1234567905432098769
37 11111111111111111111111111111111111113
11111111113 123456790165432098769
123456790123456790123456790123456790165432098765432098765432098765432098769
..37 '1'
Finished til 3700
11111111111111111111111111111111111113 123456790123456790123456790123456790165432098765432098765432098765432098769
Real time: 0.106 s CPU share: 99.03 %
@home real 0m0.016s
</pre>
 
Line 1,386 ⟶ 1,399:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var a = Fn.new { |n|
9,482

edits