Anonymous user
Factors of an integer: Difference between revisions
→using Prime decomposition: updated from zumkeller numbers. Fast for consecutive integers.
m (→{{header|REXX}}: changed a comment.) |
(→using Prime decomposition: updated from zumkeller numbers. Fast for consecutive integers.) |
||
Line 4,263:
Insertion sort was much faster, because mostly not so many factors need to be sorted.<BR>
"runtime overhead" +25% instead +100% for quicksort against no sort.<BR>
Especially fast for consecutive integers.
<lang pascal>program
// gets factors of consecutive integers fast
// limited to 1.2e11
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
{$ELSE}
{$ENDIF}
uses
sysutils
{$IFDEF WINDOWS},Windows{$ENDIF}
;
//######################################################################
//prime decomposition
const
//HCN(86) > 1.2E11 = 128,501,493,120 count of divs = 4096 7 3 1 1 1 1 1 1 1
HCN_DivCnt = 4096;
RECCOUNTMAX = 100*1000*1000;
DELTAMAX = 1000*1000;
type
tDivisors = array [0..HCN_DivCnt-1] of tItem;
tpDivisor = pUint64;
const
SizePrDeFe = 12791;//*72 <= 2 Mb ~ level 2 cache -32kB for DIVS
type
tdigits = array
//the first number with 11 different divisors =
// 2*3*5*7*11*13*17*19*23*29*31 = 2E11
tprimeFac = packed record
pfSumOfDivs,
pfpotPrim : array[0..9] of Uint32;//+10*4 = 56 Byte
pfpotMax : array[0..9] of byte; //10 = 66
pfMaxIdx : Uint16; //68
pfDivCnt : Uint32; //72
end;
tpPrimeFac = ^tprimeFac;
tPrimes = array[0..65535] of Uint32;
var
SmallPrimes: tPrimes;
PrimeDecompField :tPrimeDecompField;
pdfIDX,pdfOfs: NativeInt;
procedure InitSmallPrimes;
//get primes Number 0..65535.Sieving only odd numbers
const
MAXLIMIT = (821641-1) shr 1;
var
pr : array[0..MAXLIMIT] of byte;
p,j,d,flipflop :NativeUInt;
Begin
SmallPrimes[0] := 2;
fillchar(pr[0],SizeOf(pr),#0);
p := 0;
repeat
repeat
j :=
if
BREAK;
d := 2*p+1;
repeat
pr[j] := 1;
j += d;
until j>MAXLIMIT;
until false;
SmallPrimes[1] := 3;
SmallPrimes[2] := 5;
j := 3;
d := 7;
flipflop := (2+1)-1;//7+2*2,11+2*1,13,17,19,23
p := 3;
repeat
if pr[p] = 0 then
begin
SmallPrimes[j] := d;
inc(j);
end;
d += 2*flipflop;
p+=flipflop;
flipflop := 3-flipflop;
until (p > MAXLIMIT) OR (j>High(SmallPrimes));
end;
function OutPots(pD:tpPrimeFac;n:NativeInt):Ansistring;
var
s: String[31];
chk,p,i: NativeInt;
Begin
str(n,s);
result := s+' :';
with pd^ do
begin
str(pfDivCnt:3,s);
result += s+' : ';
chk := 1;
For n := 0 to pfMaxIdx-1 do
Begin
if n>0 then
result += '*';
p := pFpotPrim[n];
chk *= p;
str(p,s);
result += s;
i := pfpotMax[n];
if i >1 then
Begin
repeat
chk *= p;
dec(i);
until i <= 1;
end;
p := pfRemain;
If p >1 then
Begin
result += '*'+s;
end;
result += '_chk_'+s+'<';
str(pfSumOfDivs,s);
result += '_SoD_'+s+'<';
end;
end;
function smplPrimeDecomp(n:Uint64):tprimeFac;
var
pr,i,
Begin
with
Begin
pfMaxIdx := 0;
pfpotPrim[0]
pfpotMax[0] := 0;
if Not(ODD(n)) then
begin
pfMaxIdx :=
pfpotMax[0] := pot;
pfRemain := n;
pfSumOfDivs := (1 shl (pot+1))-1;
end;
i := 1;
while i < High(SmallPrimes) do
begin
pr := SmallPrimes[i];
q := n DIV pr;
if pr > q then
BREAK;
if n = pr*q then
Begin
pfpotPrim[pfMaxIdx] := pr;
pot := 0;
fac := pr;
repeat
n := q;
q := n div pr;
pot+=1;
fac *= pr;
until n <> pr*q;
pfpotMax[pfMaxIdx] := pot;
pfDivCnt *= pot+1;
pfSumOfDivs *= (fac-1)DIV(pr-1);
inc(pfMaxIdx);
end;
inc(i);
end;
pfRemain := n;
if n > 1 then
Begin
pfDivCnt *= 2;
pfSumOfDivs *= n+1
end;
end;
end;
function CnvtoBASE(var dgt:tDigits;n:Uint64;base:NativeUint):NativeInt;
//n must be multiple of base
var
q,r: Uint64;
i : NativeInt;
Begin
fillchar(dgt,SizeOf(dgt),#0);
i := 0;
// dgtNum:= n;
n := n div base;
result := 0;
repeat
r := n;
q := n div base;
r -= q*base;
n := q;
dgt[i] := r;
inc(i);
until (q = 0);
result := 0;
while (result<i) AND (dgt[result] = 0) do
inc(result);
inc(result);
end;
function IncByBaseInBase(var dgt:tDigits;base:NativeInt):NativeInt;
var
q :NativeInt;
Begin
inc(result);
until q <> base;
dgt[result] := q;
result +=1;
end;
procedure SieveOneSieve(var pdf:tPrimeDecompField);
var
dgt:tDigits;
i, j, k,pr,fac,n : NativeUInt;
begin
n := pdfOfs;
//init
for i := 0 to SizePrDeFe-1 do
begin
with pdf[i] do
Begin
pfRemain := n+i;
pfMaxIdx := 0;
pfpotPrim[0] := 1;
pfpotMax[0] := 0;
end;
//first factor 2. Make n+i even
i := (pdfIdx+n) AND 1;
IF (n = 0) AND (pdfIdx<2) then
i := 2;
repeat
pfMaxIdx := 1;
pfpotPrim[0] := 2;
pfpotMax[0] := j;
pfRemain := (n+i) shr j;
pfSumOfDivs := (1 shl (j+1))-1;
pfDivCnt := j+1;
end;
i += 2;
until i >=SizePrDeFe;
// i now index in SmallPrimes
i := 0;
repeat
//search next prime that is in bounds of sieve
repeat
inc(i);
if i >= High(SmallPrimes) then
BREAK;
pr := SmallPrimes[i];
k := pr-n MOD pr;
if (k = pr) AND (n>0) then
k:= 0;
if k < SizePrDeFe then
break;
until false;
if i >= High(SmallPrimes) then
BREAK;
//no need to use higher primes
if pr*pr > n+SizePrDeFe then
BREAK;
j := CnvtoBASE(dgt,n+k,pr);
with pdf[k] do
Begin
pfDivCnt
fac
until j<=
k
end;
until
until false;
//correct sum of & count of divisors
for i
if j
begin
pfSumOFDivs *= (j+1);
pfDivCnt *=2;
end;
end;
end;
end;
procedure NextSieve;
begin
dec(pdfIDX,SizePrDeFe);
inc(pdfOfs,SizePrDeFe);
SieveOneSieve(PrimeDecompField);
end;
function GetNextPrimeDecomp:tpPrimeFac;
begin
if pdfIDX >= SizePrDeFe then
NextSieve;
result := @PrimeDecompField[pdfIDX];
inc(pdfIDX);
end;
procedure Init_Sieve(n:NativeUint);
//Init Sieve pdfIdx,pdfOfs are Global
begin
pdfIdx := n MOD SizePrDeFe;
pdfOfs := n-pdfIdx;
SieveOneSieve(PrimeDecompField);
end;
procedure InsertSort(pDiv:tpDivisor; Left, Right : NativeInt );
var
I, J: NativeInt;
Pivot : tItem;
begin
for i:= 1 + Left to Right do
begin
Pivot:= pDiv[i];
j:= i - 1;
while (j >= Left) and (pDiv[j] > Pivot) do
begin
pDiv[j+1]:=pDiv[j];
Dec(j);
end;
pDiv[j+1]:= pivot;
end;
end;
procedure GetDivisors(pD:tpPrimeFac;var Divs:tDivisors);
var
pDivs : tpDivisor;
i,len,j,l,p,k: Int32;
Begin
i :=
pDivs := @Divs[0];
pDivs[0] := 1;
len := 1;
l :=
Begin
For i := 0 to pfMaxIdx-1 do
//Multiply every divisor before with the new primefactors
//and append them to the list
k :=
p :=
pPot :=1;
repeat
Line 4,471 ⟶ 4,664:
Begin
pDivs[l]:= pPot*pDivs[j];
inc(l);
end;
dec(k);
until k<=0;
len := l;
end;
p := pfRemain;
If p >1 then
begin
For j := 0 to len-1 do
Begin
pDivs[l]:= p*pDivs[j];
inc(l);
end;
len := l;
end;
end;
//Sort. Insertsort much faster than QuickSort in this special case
InsertSort(pDivs,0,len-1);
end;
procedure AllFacsOut(pD:tpPrimeFac;var Divs:tdivisors;proper:boolean=true);
var
k,j: Int32;
Begin
k :=
IF proper then
dec(k);
IF k > 0 then
For j := 0 to k-1 do
write(Divs[j],',');
end;
var
pPrimeDecomp :tpPrimeFac;
T0:Int64;
n : NativeUInt;
Begin
InitSmallPrimes;
T0 := GetTickCount64;
n := 0;
Init_Sieve(0);
repeat
pPrimeDecomp:= GetNextPrimeDecomp;
// GetDivisors(pPrimeDecomp,Divs);
inc(n);
until n > 10*1000*1000+1;
T0 := GetTickCount64-T0;
writeln('runtime ',T0/1000:0:3,' s');
GetDivisors(pPrimeDecomp,Divs);
AllFacsOut(pPrimeDecomp,Divs,true);
AllFacsOut(pPrimeDecomp,Divs,false);
writeln('simple version');
T0 := GetTickCount64;
n := 0;
repeatSpeedTest 0.296 secs for 1..1000000
mean count of divisors 13.970
**SpeedTest 5.707 secs for 1..10000000
Line 4,565 ⟶ 4,739:
including n version
123456789 = 3^2*3607*3803 got 12 divisors with sum : 178422816
1,3,9,3607,3803,10821,11409,32463,34227,13717421,41152263,123456789
Mypd:= smplPrimeDecomp(n);
// GetDivisors(@Mypd,Divs);
inc(n);
until n > 10*1000*1000+1;
T0 := GetTickCount64-T0;
writeln('runtime ',T0/1000:0:3,' s');
GetDivisors(@Mypd,Divs);
AllFacsOut(@Mypd,Divs,true);
AllFacsOut(@Mypd,Divs,false);
end.
</lang>
{{out}}
<pre>
runtime 1.216 s
1,11,909091
1,11,909091,10000001
simple version
runtime 5.854 s
1,11,909091
1,11,909091,10000001
//out-commented GetDivisors, but still calculates sum of divisors and count of divisors
// calculating explicit divisisors takes the same time ~ 0.9s for 1e7
runtime 0.311 s
1,11,909091
1,11,909091,10000001
simple version
runtime 4.922 s
1,11,909091
1,11,909091,10000001</pre>
=={{header|Perl}}==
|