Anonymous user
Untouchable numbers: Difference between revisions
m
→{{header|Pascal}}: corrected SieveOneSieve Uint64(1) shl.. not 1 shl.., the 1 is Uint32 ...#@!... Now only use TIO.RUN
(added pascal fast but with problems after 40,000,000 count -> 6,430,223 < wrong by -1) |
m (→{{header|Pascal}}: corrected SieveOneSieve Uint64(1) shl.. not 1 shl.., the 1 is Uint32 ...#@!... Now only use TIO.RUN) |
||
Line 892:
see also math.dartmouth.edu/~carlp/uupaper3.pdf with list count up to 1e9
<lang pascal>program UntouchableNumbers;
// gets factors of consecutive integers fast
// limited to 1.2e11 ( = sqr(821641)) aka max(smallprimes)
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
Line 899 ⟶ 900:
{$ENDIF}
uses
sysutils,strutils {Numb2USA commatize}
{$IFDEF WINDOWS},Windows{$ENDIF}
;
const
//100*1000*1000;//10*1000*1000;//5*1000*1000;//1000*1000;
LIMIT = 1000*1000;
//384;//164;//110;//64;
LIMIT_mul = 64;
//######################################################################
//prime decomposition
const
SizePrDeFe = 6*8192;//*size of(tprimeFac) =
type
tdigits = array [0..31] of Uint32;
//the first number with 12 different prime factors =
//2*3*5*7*11*13*17*19*23*29*31*37 = 7,
//
tprimeFac = packed record
pfSumOfDivs,
Line 1,067 ⟶ 1,073:
var
dgt:tDigits;
i,j,k,pr,fac,n,MaxP :
begin
n := pdfOfs;
Line 1,098 ⟶ 1,104:
pfpotMax[0] := j;
pfRemain := (n+i) shr j;
pfSumOfDivs := (Uint64(1) shl (j+1))-1;
// writeln(n+i,' # ',j,' ## ',pfRemain,' ### ',pfSumOfDivs);
pfDivCnt := j+1;
end;
Line 1,197 ⟶ 1,204:
end;
procedure CheckRest(n: Uint64;pUntouch:pByte);
var
k : Uint64;
begin
repeat
k := GetNextPrimeDecomp^.pfSumOfDivs-n;
inc(n);
if (k <= LIMIT) then
pUntouch[k ] := 1;
until n >LIMIT_mul*LIMIT;
end;
function CheckPrime(n:Uint64;prmEndIdx:NativeInt;pUntouch:pByte):NativeInt;
var
i,k: NativeInt;
Begin
//n= prime,n+1 would be marked by n*n with proper factors 1,n
//here n is aready n+1
pUntouch[n] := 1;
//marked by prime*n with proper factors 1,(prime),n
For i := 0 to prmEndIdx do
begin
k := smallprimes[i]+n;
If k > LIMIT then
Begin
dec(prmEndIdx);
BREAK;
end;
pUntouch[k] := 1;
end;
result := prmEndIdx;
end;
var
Untouch : array of byte;
pUntouch: pByte;
T0:Int64;
n,k,lim,
Begin
setlength(untouch,LIMIT+1);
pUntouch := @untouch[0];
InitSmallPrimes;
T0 := GetTickCount64;
inc(
until smallprimes[
writeln(
writeln(LIMIT_mul);
n := 0;
Init_Sieve(
pUntouch[0] := 1;
pUntouch[1] := 1;//all primes
repeat
inc(n);//n-> n+1
begin
If k <> 1 then
prmEndIdx := CheckPrime(n,prmEndIdx,pUntouch);
end;
until n >LIMIT;
writeln('runtime for n<= LIMIT ',(GetTickCount64-T0)/1000:0:3,' s');
writeln('Check the rest ',Numb2USA(IntToStr((LIMIT_mul-1)*Limit)));
CheckRest(n,pUntouch);
T0 := GetTickCount64-T0;
writeln('runtime ',T0/1000:0:3,' s');
Line 1,259 ⟶ 1,283:
if n = lim then
Begin
writeln(Numb2USA(IntToStr(lim)):10,Numb2USA(IntToStr(k)):10);
lim *= 10;
end;
Line 1,269 ⟶ 1,293:
if n = lim then
Begin
writeln(Numb2USA(IntToStr(lim)):10,Numb2USA(IntToStr(k)):10);
lim += LIMIT DIV 10;
end;
Line 1,278 ⟶ 1,302:
{{out}}
<pre>
TIO.RUN
runtime for n<= LIMIT 0.070 s
Check the rest 63,000,000
runtime 4.214 s
10 2
100 5
10,000
100,000
200,000
300,000
400,000
500,000
600,000
700,000
800,000
900,000
1,000,000 150,232
//url=https://math.dartmouth.edu/~carlp/uupaper3.pdf
6000000 936244
Line 1,351 ⟶ 1,337:
100000000 16246940
</pre>
=={{header|Perl}}==
{{libheader|ntheory}}
|