Distribution of 0 digits in factorial series: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: reworded a comment.) |
m (→{{header|Pascal}}: delete unused arrays, reorder, more comments) |
||
Line 206: | Line 206: | ||
CountOfZero : array[0..999] of byte; |
CountOfZero : array[0..999] of byte; |
||
SumOfRatio :array[0..LIMIT] of extended; |
SumOfRatio :array[0..LIMIT] of extended; |
||
CoZ_Fac : array[0..LIMIT] of Uint32; |
|||
CoD_Fac : array[0..LIMIT] of Uint32; |
|||
⚫ | |||
// for testing |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
procedure InitCoZ; |
procedure InitCoZ; |
||
//Init Lookup table for 3 digits |
|||
var |
var |
||
x,y : integer; |
|||
begin |
begin |
||
fillchar(CountOfZero,SizeOf(CountOfZero),#0); |
fillchar(CountOfZero,SizeOf(CountOfZero),#0); |
||
CountOfZero[0] := 3; |
CountOfZero[0] := 3; //000 |
||
For |
For x := 1 to 9 do |
||
Begin |
Begin |
||
CountOfZero[ |
CountOfZero[x] := 2; //00x |
||
CountOfZero[10* |
CountOfZero[10*x] := 2; //0x0 |
||
CountOfZero[100* |
CountOfZero[100*x] := 2; //x00 |
||
y := 10; |
|||
repeat |
repeat |
||
CountOfZero[ |
CountOfZero[y+x] := 1; //0yx |
||
CountOfZero[10* |
CountOfZero[10*y+x] := 1; //y0x |
||
CountOfZero[10*( |
CountOfZero[10*(y+x)] := 1; //yx0 |
||
inc( |
inc(y,10) |
||
until |
until y > 100; |
||
end; |
end; |
||
end; |
end; |
||
Line 243: | Line 252: | ||
end; |
end; |
||
function CntZero(pMul:tpMul;Lmt :NativeInt):NativeUint; |
|||
//count zeros in Base 1,000,000,000 number |
|||
//for checking purposes |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// Numbers in base 1,000,000,000 divide in 3 sections |
|||
var |
var |
||
s : string[15]; |
|||
q,r : LongWord; |
q,r : LongWord; |
||
i : NativeInt; |
i : NativeInt; |
||
begin |
begin |
||
result := 0; |
result := 0; |
||
For i := |
For i := Lmt-1 downto 0 do |
||
Begin |
Begin |
||
q := pMul[i]; |
q := pMul[i]; |
||
r := q DIV 1000; |
r := q DIV 1000; |
||
result +=CountOfZero[q-1000*r]; |
result +=CountOfZero[q-1000*r];//q-1000*r == q mod 1000 |
||
q := r; |
q := r; |
||
r := q DIV 1000; |
r := q DIV 1000; |
||
Line 272: | Line 271: | ||
result +=CountOfZero[q-1000*r]; |
result +=CountOfZero[q-1000*r]; |
||
end; |
end; |
||
//special case first digits no leading '0' |
|||
q := pMul[lmt]; |
q := pMul[lmt]; |
||
while q >= 1000 do |
while q >= 1000 do |
||
Line 279: | Line 279: | ||
q := r; |
q := r; |
||
end; |
end; |
||
while q > 0 do |
|||
begin |
|||
r := q DIV 10; |
|||
result += Ord( q-10*r= 0); |
|||
q := r; |
|||
⚫ | |||
end; |
end; |
||
end; |
end; |
||
function GetCoD(pMul:tpMul;Lmt :NativeInt):NativeUint; |
function GetCoD(pMul:tpMul;Lmt :NativeInt):NativeUint; |
||
//count of decimal digits |
|||
//calculate used digits |
|||
var |
var |
||
i : longWord; |
i : longWord; |
||
Line 305: | Line 304: | ||
inc(result); |
inc(result); |
||
end; |
end; |
||
⚫ | |||
procedure DoChecks(pMul:tpMul;Lmt,i :NativeInt); |
|||
//(extended(1.0)* makes TIO.RUN faster // only using FPU? |
|||
⚫ | |||
⚫ | |||
end; |
|||
function MulByI(pMul:tpMul;UL,i :NativeInt):NativeInt; |
|||
var |
|||
prod : Uint64; |
|||
j : nativeInt; |
|||
carry : LongWord; |
|||
begin |
|||
result := UL; |
|||
⚫ | |||
For j := 0 to result do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end; |
|||
⚫ | |||
Begin |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end; |
end; |
||
Line 311: | Line 339: | ||
MulArr : tMul; |
MulArr : tMul; |
||
pMul : tpMul; |
pMul : tpMul; |
||
i,ul : NativeInt; |
|||
i,j,ul : NativeInt; |
|||
begin |
begin |
||
i := getFactorialDecDigits(n) DIV 9 +10; |
i := getFactorialDecDigits(n) DIV 9 +10; |
||
Line 321: | Line 348: | ||
i := 1; |
i := 1; |
||
repeat |
repeat |
||
UL := MulByI(pMul,UL,i); |
|||
//Now do what you like to do with i! |
|||
DoChecks(pMul,UL,i); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
CoD_Fac[i] := GetCoD(pMul,UL); |
|||
CoZ_Fac[i] := CheckForZero(pMul,UL); |
|||
⚫ | |||
inc(i); |
inc(i); |
||
until i> n; |
until i> n; |
||
Line 348: | Line 361: | ||
writeln(i:8,SumOfRatio[i]/i:18:15); |
writeln(i:8,SumOfRatio[i]/i:18:15); |
||
end; |
end; |
||
var |
var |
||
i : integer; |
i : integer; |
||
Line 353: | Line 367: | ||
InitCoZ; |
InitCoZ; |
||
SumOfRatio[0]:= 0; |
SumOfRatio[0]:= 0; |
||
⚫ | |||
CoZ_Fac[0]:= 0; |
|||
getFactorialExact(LIMIT); |
getFactorialExact(LIMIT); |
||
Out_(100); |
Out_(100); |
||
Line 371: | Line 383: | ||
end.</lang> |
end.</lang> |
||
{{out}} |
{{out}} |
||
⚫ | |||
<pre>TIO.RUN |
|||
⚫ | |||
1000 0.203544551103165 |
1000 0.203544551103165 |
||
10000 0.173003848241866 |
10000 0.173003848241866 |
||
50000 0.159620054602269 |
50000 0.159620054602269 |
||
First ratio < 0.16 47332 0.15999999579985665 |
First ratio < 0.16 47332 0.15999999579985665 |
||
Real time: |
Real time: 4.898 s CPU share: 99.55 % // 2.67s on 2200G freepascal 3.2.2</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |