Show the (decimal) value of a number of 1s appended with a 3, then squared: Difference between revisions
Show the (decimal) value of a number of 1s appended with a 3, then squared (view source)
Revision as of 15:48, 5 February 2024
, 3 months ago→{{header|Wren}}: Minor tidy
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
Because the values are calculated one by one, one can use addition to get rid of calculating squares, by using binomial formula.
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 =
type
tmyNumb = array of byte;
var
res :AnsiString;
procedure
var
begin
l
w := l+1;
setlength(res,w);
fillchar(res[1],w,' ');
ofs :=
For i := 1
write(res);
end;
procedure Out_k_sqr(const k,sqr_k: tmyNUmb;pot:integer);
var
Begin
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
For i := 0 to pot+1 do
begin
sum := res[i+pot]+s[i]+carry;
carry := ord(sum>9);
sum -= (-carry) AND 10;
res[i+pot] :=
end;
end;
var
s,s_DeltaSqr,s_lastsqr : tmyNumb;
Begin
setlength(s,MAX);
setlength(s_DeltaSqr,Max+1);
setlength(s_lastsqr,2*Max+1);
pot
s[pot] := 3;
repeat
SumMyDeltaSqr(s_lastsqr,s_DeltaSqr,pot);
Out_k_sqr(s,s_lastsqr,pot);
if pot = 37 then
Out_k_sqr(s
if pot > 0 then
s_DeltaSqr[pot]:= 2 // =>2*s[i] 2222...26
s_DeltaSqr[0] := 6;// =>2*s[0] 6
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
37 11111111111111111111111111111111111113
123456790123456790123456790123456790165432098765432098765432098765432098769
Finished til 3700
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="
var a = Fn.new { |n|
|