Smallest multiple: Difference between revisions

Content added Content deleted
(julia example)
m (→‎extended: extended to show count of digits til 2 billion)
Line 167: Line 167:
</pre>
</pre>
===extended===
===extended===
Find that the count of digits is nearly a constant x upper rangelimit.<br> The number of factors is the count of primes til limit.See GetFactorList.<br>No need for lcm or prime decomposition and other contortions.<BR>
fascinating find, that the count of digits is nearly a constant x upper rangelimit.<br> The number of factors is the count of primes til limit.See GetFactorList.<br>No need for calculating lcm(lcm(lcm(1,2),3),4..) or prime decomposition<BR>
Using prime sieve.
Using prime sieve.
<lang pascal>{$IFDEF FPC}
<lang pascal>{$IFDEF FPC}
{$MODE DELPHI}
{$MODE DELPHI} {$Optimization On}
{$ELSE}
{$ELSE}
{$APPTAYPE CONSOLE}
{$APPTAYPE CONSOLE}
Line 180: Line 180:
{$ENDIF}
{$ENDIF}
sysutils; //format
sysutils; //format

const
const
UpperLimit = 2*1000*1000;
MAX_LIMIT = 2*1000*1000;
UpperLimit = MAX_LIMIT+1000;// so to find a prime beyond MAX_LIMIT
MAX_UINT64 = 46;
MAX_UINT64 = 46;// unused.Limit to get an Uint64 output
type
type
tFactors = array of Uint32;
tFactors = array of Uint32;
tprimelist = array of byte;
tprimelist = array of byte;
var
var
primelist : tPrimelist;
primeDeltalist : tPrimelist;
factors,

saveFactors:tFactors;
saveFactorsIdx,
maxFactorsIdx : Uint32;
procedure Init_Primes;
procedure Init_Primes;
var
var
pPrime : pByte;
pPrime : pByte;
p ,i: NativeUInt;
p,i,delta,cnt: NativeUInt;
begin
begin
setlength(primelist,UpperLimit+3*8+1);
setlength(primeDeltalist,UpperLimit+3*8+1);
pPrime := @primelist[0];
pPrime := @primeDeltalist[0];
//delete multiples of 2,3
//delete multiples of 2,3
i := 0;
i := 0;
Line 206: Line 209:
inc(i,24);
inc(i,24);
until i>UpperLimit;
until i>UpperLimit;
cnt := 2;// 2,3
p := 5;
p := 5;
delta := 1;//5-3
repeat
repeat
if pPrime[p] <> 0 then
if pPrime[p] <> 0 then
Line 213: Line 218:
if i > UpperLimit then
if i > UpperLimit then
break;
break;
inc(cnt);
pPrime[p-2*delta] := delta;
delta := 0;
repeat
repeat
pPrime[i] := 0;
pPrime[i] := 0;
Line 219: Line 227:
end;
end;
inc(p,2);
inc(p,2);
inc(delta);
until p*p>UpperLimit;
until p*p>UpperLimit;
setlength(saveFactors,cnt);
pPrime[1] := 0;
//convert to delta
pPrime[2] := 1;
repeat
pPrime[3] := 1;
if pPrime[p]<> 0 then
begin
pPrime[p-2*delta] := delta;
inc(cnt);
delta := 0;
end;
inc(p,2);
inc(delta);
until p > UpperLimit;
setlength(factors,cnt);
factors[0] := 2;
factors[1] := 3;
i := 2;
p := 5;
repeat
factors[i] := p;
p += 2*pPrime[p];
i += 1;
until i >= cnt;
setlength(primeDeltalist,0);
// writeln(length(savefactors)); writeln(length(factors));
end;
end;


{$IFDEF USE_GMP}
{$IFDEF USE_GMP}
procedure ConvertToMPZ(const factors:tFactors);
procedure ConvertToMPZ(const factors:tFactors;dgtCnt:UInt32);
const
c19Digits = QWord(10*1000000)*1000000*1000000;
var
var
mp : mpz_t;
mp,mpdiv : mpz_t;
s : AnsiString;
s : AnsiString;
i : integer;
rest,last : Uint64;
f : Uint32;
i :int32;
begin
begin
//Init and allocate space
mpz_init(mp);
mpz_init_set_ui(mp,0);
mpz_init(mpdiv);
mpz_ui_pow_ui(mpdiv,10,dgtCnt);
mpz_add(mp,mp,mpdiv);
mpz_add_ui(mp,mp,1);
mpz_set_ui(mp,1);
mpz_set_ui(mp,1);

for i := 0 to high(factors) do
i := maxFactorsIdx;
mpz_mul_ui(mp,mp,factors[i]);
i := mpz_sizeinbase(mp,10);
rest := 1;
repeat
setlength(s,i+10);
last := rest;
mpz_get_str(@s[1],10,mp);
i := i+10;
f := factors[i];
rest *= f;
while not(s[i] in['0'..'9']) do
if rest div f <> last then
begin
mpz_mul_ui(mp,mp,last);
rest := f;
end;
dec(i);
dec(i);
until i < 0;
setlength(s,i+1);
mpz_mul_ui(mp,mp,rest);
if length(s)> 22 then

If dgtcnt>40 then
begin
begin
rest := mpz_fdiv_ui(mp,c19Digits);
move(s[i-9],s[13],10);
s[11]:= '.';s[12]:= '.';
s := '..'+Format('%.19u',[rest]);
mpz_fdiv_q_ui (mpdiv,mpdiv,c19Digits);
setlength(s,22);
mpz_fdiv_q(mp,mp,mpdiv);
rest := mpz_get_ui(mp);
writeln(rest:19,s);
mpz_clear(mpdiv);
end
else
Begin
setlength(s,dgtCnt+1000);
mpz_get_str(@s[1],10,mp);
writeln(s);
i := length(s);
while not(s[i] in['0'..'9']) do
dec(i);
setlength(s,i+1);
writeln(s);
end;
end;
writeln(s);
mpz_clear(mp);
mpz_clear(mp);
end;
end;
Line 256: Line 316:
procedure CheckDigits(const factors:tFactors);
procedure CheckDigits(const factors:tFactors);
var
var
digCnt : extended;
dgtcnt : extended;
i : integer;
i : integer;
begin
begin
digcnt := 0;
dgtcnt := 0;
for i := 0 to high(factors) do
i := 0;
repeat
digcnt += ln(factors[i]);
i := trunc(digcnt/ln(10)+1);
dgtcnt += ln(factors[i]);
inc(i);
writeln(' has ',length(factors):10,' factors and ',i:10,' digits');
until i > maxFactorsIdx;
dgtcnt := trunc(dgtcnt/ln(10))+1;
writeln(' has ',maxFactorsIdx+1:10,' factors and ',dgtcnt:10:0,' digits');
{$IFDEF USE_GMP}
{$IFDEF USE_GMP}
If i < 10000 then
i := trunc(dgtcnt);
if i < 1000*1000 then
ConvertToMPZ(factors);
ConvertToMPZ(factors,i);
{$ENDIF}
{$ENDIF}
end;
end;
Line 274: Line 338:
i : integer;
i : integer;
begin
begin
if length(factors) >15 then
if maxFactorsIdx >15 then
Exit(0);
Exit(0);
result := 1;
result := 1;
for i := 0 to high(factors) do
for i := 0 to maxFactorsIdx do
result *= factors[i];
result *= factors[i];
end;
end;
Line 287: Line 351:
begin
begin
result := '';
result := '';
for i := 0 to high(factors)-1 do
for i := 0 to maxFactorsIdx-1 do
begin
begin
str(factors[i],s);
str(factors[i],s);
result += s+'*';
result += s+'*';
end;
end;
str(factors[High(factors)],s);
str(factors[maxFactorsIdx],s);
result += s;
result += s;
end;
end;
Line 298: Line 362:
procedure GetFactorList(var factors:tFactors;max:Uint32);
procedure GetFactorList(var factors:tFactors;max:Uint32);
var
var
pPrime : pByte;
p,f,lf : Uint32;
n,f,lf : Uint32;
BEGIN
BEGIN
pPrime := @primeList[0];
p := 2;
n := 2;
lf := 0;
lf := 0;
saveFactors[lf] := p;
setlength(factors,lf);
while p*p <= max do

while n*n <= max do
Begin
Begin
if pPrime[n]<>0 then
saveFactors[lf] := p;
begin
f := p*p;
while f*p <= max do
setlength(factors,lf+1);
f := n*n;
f*= p;
factors[lf] := f;
while f*n <= max do
f*= n;
inc(lf);
factors[lf] := f;
p := factors[lf];
inc(lf);
if p= 0 then HALT;
end;
inc(n);
end;
end;
if lf>0 then
//the rest are all the primes up to max
saveFactorsIdx := lf-1;
For n := n to max do
repeat
if pPrime[n]<>0 then
Begin
inc(lf)
setlength(factors,lf+1);
until factors[lf]>Max;
factors[lf] := n;
maxFactorsIdx := lf-1;
inc(lf);
end;
end;
end;


procedure Check(var factors:tFactors;max:Uint32);
procedure Check(var factors:tFactors;max:Uint32);
var
i: Uint32;
begin
begin
GetFactorList(factors,max);
GetFactorList(factors,max);
write(max:10,': ');
write(max:10,': ');
if length(factors)>15 then
if maxFactorsIdx>15 then
CheckDigits(factors)
CheckDigits(factors)
else
else
writeln(ConvertToUint64(factors):21,' = ',ConvertToStr(factors));
writeln(ConvertToUint64(factors):21,' = ',ConvertToStr(factors));
for i := 0 to saveFactorsIdx do
factors[i] := savefactors[i];
end;
end;


var
var
factors:tFactors;
max: Uint32;
max: Uint32;
BEGIN
BEGIN
Init_Primes;
Init_Primes;


max := 200;
max := 2;
repeat
repeat
check(factors,max);
check(factors,max);
max *=10;
max *=10;
until max > UpperLimit;
until max > MAX_LIMIT;

For max := MAX_UINT64 downto 2 do
writeln;
For max := 10 to 20 do // < MAX_UINT64
check(factors,max);
check(factors,max);
{$IFDEF WINDOWS}
{$IFDEF WINDOWS}
READLN;
READLN;
{$ENDIF}
{$ENDIF}
END.</lang>
END.
</lang>
{{out}}
{{out}}
<pre style="height:300px">
<pre style="height:300px">
TIO.RUN Real time: 0.203 s User time: 0.147 s Sys. time: 0.054 s CPU share: 98.88 %
TIO.RUN Real time: 1.161 s User time: 1.106 s Sys. time: 0.049 s CPU share: 99.49 %
200: has 46 factors and 90 digits
2: 2 = 2
20: 232792560 = 16*9*5*7*11*13*17*19
3372935888..0066992000
200: has 46 factors and 90 digits
3372935888329262646..8060677390066992000
2000: has 303 factors and 867 digits
2000: has 303 factors and 867 digits
1511177948774443153..3786415805463680000
1511177948..5463680000
20000: has 2262 factors and 8676 digits
20000: has 2262 factors and 8676 digits
4879325627288270518..7411295098112000000
4879325627..8112000000
200000: has 17984 factors and 86871 digits
200000: has 17984 factors and 86871 digits
3942319728529926377..9513860925440000000
2000000: has 148933 factors and 868639 digits
2000000: has 148933 factors and 868639 digits
8467191629995920178..6480233472000000000
46: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
{ at home
45: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
20000000: has 1270607 factors and 8686151 digits
44: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
1681437413936981958..6706037760000000000
43: 9419588158802421600 = 32*27*25*7*11*13*17*19*23*29*31*37*41*43
200000000: has 11078937 factors and 86857606 digits
42: 219060189739591200 = 32*27*25*7*11*13*17*19*23*29*31*37*41
2000000000: has 98222287 factors and 868583388 digits
41: 219060189739591200 = 32*27*25*7*11*13*17*19*23*29*31*37*41
}
40: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
39: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
38: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
37: 5342931457063200 = 32*27*25*7*11*13*17*19*23*29*31*37
36: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
35: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
34: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
33: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
32: 144403552893600 = 32*27*25*7*11*13*17*19*23*29*31
31: 72201776446800 = 16*27*25*7*11*13*17*19*23*29*31
30: 2329089562800 = 16*27*25*7*11*13*17*19*23*29
29: 2329089562800 = 16*27*25*7*11*13*17*19*23*29
28: 80313433200 = 16*27*25*7*11*13*17*19*23
27: 80313433200 = 16*27*25*7*11*13*17*19*23
26: 26771144400 = 16*9*25*7*11*13*17*19*23
25: 26771144400 = 16*9*25*7*11*13*17*19*23
24: 5354228880 = 16*9*5*7*11*13*17*19*23
23: 5354228880 = 16*9*5*7*11*13*17*19*23
22: 232792560 = 16*9*5*7*11*13*17*19
21: 232792560 = 16*9*5*7*11*13*17*19
20: 232792560 = 16*9*5*7*11*13*17*19
19: 232792560 = 16*9*5*7*11*13*17*19
18: 12252240 = 16*9*5*7*11*13*17
17: 12252240 = 16*9*5*7*11*13*17
16: 720720 = 16*9*5*7*11*13
15: 360360 = 8*9*5*7*11*13
14: 360360 = 8*9*5*7*11*13
13: 360360 = 8*9*5*7*11*13
12: 27720 = 8*9*5*7*11
11: 27720 = 8*9*5*7*11
10: 2520 = 8*9*5*7
10: 2520 = 8*9*5*7
9: 2520 = 8*9*5*7
11: 27720 = 8*9*5*7*11
8: 840 = 8*3*5*7
12: 27720 = 8*9*5*7*11
7: 420 = 4*3*5*7
13: 360360 = 8*9*5*7*11*13
6: 60 = 4*3*5
14: 360360 = 8*9*5*7*11*13
5: 60 = 4*3*5
15: 360360 = 8*9*5*7*11*13
4: 12 = 4*3
16: 720720 = 16*9*5*7*11*13
3: 6 = 2*3
17: 12252240 = 16*9*5*7*11*13*17
2: 2 = 2
18: 12252240 = 16*9*5*7*11*13*17
19: 232792560 = 16*9*5*7*11*13*17*19
20: 232792560 = 16*9*5*7*11*13*17*19
</pre>
</pre>