Distribution of 0 digits in factorial series: Difference between revisions

m
→‎{{header|Pascal}}: delete unused arrays, reorder, more comments
m (→‎{{header|REXX}}: reworded a comment.)
m (→‎{{header|Pascal}}: delete unused arrays, reorder, more comments)
Line 206:
CountOfZero : array[0..999] of byte;
SumOfRatio :array[0..LIMIT] of extended;
 
CoZ_Fac : array[0..LIMIT] of Uint32;
 
CoD_Fac : array[0..LIMIT] of Uint32;
functionprocedure CheckForZeroOutMul(pMul:tpMul;Lmt :NativeInt):NativeUint;
// for testing
Begin
write(pMul[lmt]);
For lmt := lmt-1 downto 0 do
write(Format('%.9d',[pMul[lmt]]));
writeln;
end;
 
procedure InitCoZ;
//Init Lookup table for 3 digits
var
ix,jy : integer;
begin
fillchar(CountOfZero,SizeOf(CountOfZero),#0);
CountOfZero[0] := 3; //000
For ix := 1 to 9 do
Begin
CountOfZero[ix] := 2; //00x
CountOfZero[10*ix] := 2; //0x0
CountOfZero[100*ix] := 2; //x00
jy := 10;
repeat
CountOfZero[jy+ix] := 1; //0yx
CountOfZero[10*jy+ix] := 1; //y0x
CountOfZero[10*(jy+ix)] := 1; //yx0
inc(jy,10)
until jy > 100;
end;
end;
Line 243 ⟶ 252:
end;
 
procedurefunction OutMulCntZero(pMul:tpMul;Lmt :NativeInt):NativeUint;
//count zeros in Base 1,000,000,000 number
//for checking purposes
Begin
write(pMul[lmt]);
For lmt := lmt-1 downto 0 do
write(Format('%.9d',[pMul[lmt]]));
writeln;
end;
 
function CheckForZero(pMul:tpMul;Lmt :NativeInt):NativeUint;
// Numbers in base 1,000,000,000 divide in 3 sections
var
s : string[15];
q,r : LongWord;
i : NativeInt;
begin
result := 0;
For i := 0 to Lmt-1 downto 0 do
Begin
q := pMul[i];
r := q DIV 1000;
result +=CountOfZero[q-1000*r];//q-1000*r == q mod 1000
q := r;
r := q DIV 1000;
Line 272 ⟶ 271:
result +=CountOfZero[q-1000*r];
end;
//special case first digits no leading '0'
q := pMul[lmt];
while q >= 1000 do
Line 279:
q := r;
end;
ifwhile q > 0 thendo
Beginbegin
str(r := q,s) DIV 10;
Forresult i :+= LengthOrd(s) downtoq-10*r= 1 do0);
q IF s[i] :='0' thenr;
inc(result);
end;
end;
 
function GetCoD(pMul:tpMul;Lmt :NativeInt):NativeUint;
//count of decimal digits
//calculate used digits
var
i : longWord;
Line 305 ⟶ 304:
inc(result);
end;
end;
 
procedure DoChecks(pMul:tpMul;Lmt,i :NativeInt);
//(extended(1.0)* makes TIO.RUN faster // only using FPU?
Begin
SumOfRatio[i] := SumOfRatio[i-1] + CoZ_Fac[i](extended(1.0)*CntZero(pMul,Lmt))/CoD_Fac[i]GetCoD(pMul,Lmt);
end;
 
function MulByI(pMul:tpMul;UL,i :NativeInt):NativeInt;
var
prod : Uint64;
j : nativeInt;
carry : LongWord;
begin
result := UL;
CoD_Fac[0]carry := 0;
For j := 0 to result do
Begin
prod := i*pMul[j0]+Carry;
Carry := prod Div LongWordDec;
pMul[j0] := Prod - LongWordDec*Carry*LongWordDec;
inc(UlpMul);
end;
 
IF Carry <> 0 then
Begin
inc(result);
pMul[UL0]:= Carry;
End;
end;
 
Line 311 ⟶ 339:
MulArr : tMul;
pMul : tpMul;
prodi,carryul : Uint64NativeInt;
i,j,ul : NativeInt;
begin
i := getFactorialDecDigits(n) DIV 9 +10;
Line 321 ⟶ 348:
i := 1;
repeat
carryUL := 0MulByI(pMul,UL,i);
For//Now jdo :=what 0you like to UL do with i!
DoChecks(pMul,UL,i);
Begin
prod := i*pMul[j]+Carry;
Carry := prod Div LongWordDec;
pMul[j] := Prod - Carry*LongWordDec;
end;
 
IF Carry <> 0 then
Begin
inc(Ul);
pMul[UL]:= Carry;
End;
 
CoD_Fac[i] := GetCoD(pMul,UL);
CoZ_Fac[i] := CheckForZero(pMul,UL);
SumOfRatio[i] := SumOfRatio[i-1] + CoZ_Fac[i]/CoD_Fac[i];
inc(i);
until i> n;
Line 348 ⟶ 361:
writeln(i:8,SumOfRatio[i]/i:18:15);
end;
 
var
i : integer;
Line 353 ⟶ 367:
InitCoZ;
SumOfRatio[0]:= 0;
CoD_Fac[0]:= 0;
CoZ_Fac[0]:= 0;
getFactorialExact(LIMIT);
Out_(100);
Line 371 ⟶ 383:
end.</lang>
{{out}}
<pre> 100 0.246753186167432
<pre>TIO.RUN
100 0.246753186167432
1000 0.203544551103165
10000 0.173003848241866
50000 0.159620054602269
First ratio < 0.16 47332 0.15999999579985665
Real time: 54.521898 s CPU share: 9899.0355 % // 2.67s on 2200G freepascal 3.2.2</pre>
 
=={{header|Python}}==
Anonymous user