Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
229
Task/Giuga-numbers/Free-Pascal/giuga-numbers-1.pas
Normal file
229
Task/Giuga-numbers/Free-Pascal/giuga-numbers-1.pas
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
program Giuga;
|
||||
|
||||
{$IFDEF FPC}
|
||||
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
uses
|
||||
sysutils
|
||||
{$IFDEF WINDOWS},Windows{$ENDIF}
|
||||
;
|
||||
//######################################################################
|
||||
//prime decomposition only squarefree and multiple of 6
|
||||
|
||||
type
|
||||
tprimeFac = packed record
|
||||
pfpotPrimIdx : array[0..9] of Uint64;
|
||||
pfMaxIdx : Uint32;
|
||||
end;
|
||||
tpPrimeFac = ^tprimeFac;
|
||||
tPrimes = array[0..65535] of Uint32;
|
||||
|
||||
var
|
||||
{$ALIGN 8}
|
||||
SmallPrimes: tPrimes;
|
||||
{$ALIGN 32}
|
||||
|
||||
procedure InitSmallPrimes;
|
||||
//get primes. #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
|
||||
p +=1
|
||||
until pr[p]= 0;
|
||||
j := (p+1)*p*2;
|
||||
if j>MAXLIMIT then
|
||||
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: NativeInt;
|
||||
Begin
|
||||
str(n,s);
|
||||
result := s+' :';
|
||||
with pd^ do
|
||||
begin
|
||||
chk := 1;
|
||||
For n := 0 to pfMaxIdx-1 do
|
||||
Begin
|
||||
if n>0 then
|
||||
result += '*';
|
||||
p := pfpotPrimIdx[n];
|
||||
chk *= p;
|
||||
str(p,s);
|
||||
result += s;
|
||||
end;
|
||||
str(chk,s);
|
||||
result += '_chk_'+s+'<';
|
||||
end;
|
||||
end;
|
||||
|
||||
function IsSquarefreeDecomp6(var res:tPrimeFac;n:Uint64):boolean;inline;
|
||||
//factorize only not prime/semiprime and squarefree n= n div 6
|
||||
var
|
||||
pr,i,q,idx :NativeUInt;
|
||||
Begin
|
||||
with res do
|
||||
Begin
|
||||
Idx := 2;
|
||||
|
||||
q := n DIV 5;
|
||||
if n = 5*q then
|
||||
Begin
|
||||
pfpotPrimIdx[2] := 5;
|
||||
n := q;
|
||||
q := q div 5;
|
||||
if q*5=n then
|
||||
EXIT(false);
|
||||
inc(Idx);
|
||||
end;
|
||||
|
||||
q := n DIV 7;
|
||||
if n = 7*q then
|
||||
Begin
|
||||
pfpotPrimIdx[Idx] := 7;
|
||||
n := q;
|
||||
q := q div 7;
|
||||
if q*7=n then
|
||||
EXIT(false);
|
||||
inc(Idx);
|
||||
end;
|
||||
|
||||
q := n DIV 11;
|
||||
if n = 11*q then
|
||||
Begin
|
||||
pfpotPrimIdx[Idx] := 11;
|
||||
n := q;
|
||||
q := q div 11;
|
||||
if q*11=n then
|
||||
EXIT(false);
|
||||
inc(Idx);
|
||||
end;
|
||||
|
||||
if Idx < 3 then
|
||||
Exit(false);
|
||||
|
||||
i := 5;
|
||||
while i < High(SmallPrimes) do
|
||||
begin
|
||||
pr := SmallPrimes[i];
|
||||
q := n DIV pr;
|
||||
//if n < pr*pr
|
||||
if pr > q then
|
||||
BREAK;
|
||||
if n = pr*q then
|
||||
Begin
|
||||
pfpotPrimIdx[Idx] := pr;
|
||||
n := q;
|
||||
q := n div pr;
|
||||
if pr*q = n then
|
||||
EXIT(false);
|
||||
inc(Idx);
|
||||
end;
|
||||
inc(i);
|
||||
end;
|
||||
if n <> 1 then
|
||||
begin
|
||||
pfpotPrimIdx[Idx] := n;
|
||||
inc(Idx);
|
||||
end;
|
||||
pfMaxIdx := idx;
|
||||
end;
|
||||
exit(true);
|
||||
end;
|
||||
|
||||
function ChkGiuga(n:Uint64;pPrimeDecomp :tpPrimeFac):boolean;inline;
|
||||
var
|
||||
p : Uint64;
|
||||
idx: NativeInt;
|
||||
begin
|
||||
with pPrimeDecomp^ do
|
||||
Begin
|
||||
idx := pfMaxIdx-1;
|
||||
repeat
|
||||
p := pfpotPrimIdx[idx];
|
||||
result := (((n DIV p)-1)MOD p) = 0;
|
||||
if not(result) then
|
||||
EXIT;
|
||||
dec(idx);
|
||||
until idx<0;
|
||||
end;
|
||||
end;
|
||||
|
||||
const
|
||||
LMT = 24423128562;//2214408306;//
|
||||
var
|
||||
PrimeDecomp :tPrimeFac;
|
||||
T0:Int64;
|
||||
n,n6 : UInt64;
|
||||
cnt:Uint32;
|
||||
Begin
|
||||
InitSmallPrimes;
|
||||
|
||||
T0 := GetTickCount64;
|
||||
with PrimeDecomp do
|
||||
begin
|
||||
pfpotPrimIdx[0]:= 2;
|
||||
pfpotPrimIdx[1]:= 3;
|
||||
end;
|
||||
n := 0;
|
||||
n6 := 0;
|
||||
cnt := 0;
|
||||
repeat
|
||||
//only multibles of 6
|
||||
inc(n,6);
|
||||
inc(n6);
|
||||
//no square factor of 2
|
||||
if n AND 3 = 0 then
|
||||
continue;
|
||||
//no square factor of 3
|
||||
if n MOD 9 = 0 then
|
||||
continue;
|
||||
|
||||
if IsSquarefreeDecomp6(PrimeDecomp,n6)then
|
||||
if ChkGiuga(n,@PrimeDecomp) then
|
||||
begin
|
||||
inc(cnt);
|
||||
writeln(cnt:3,'..',OutPots(@PrimeDecomp,n),' ',(GettickCount64-T0)/1000:6:3,' s');
|
||||
end;
|
||||
until n >= LMT;
|
||||
T0 := GetTickCount64-T0;
|
||||
writeln('Found ',cnt);
|
||||
writeln('Tested til ',n,' runtime ',T0/1000:0:3,' s');
|
||||
writeln;
|
||||
writeln(OutPots(@PrimeDecomp,n));
|
||||
end.
|
||||
198
Task/Giuga-numbers/Free-Pascal/giuga-numbers-2.pas
Normal file
198
Task/Giuga-numbers/Free-Pascal/giuga-numbers-2.pas
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
program Giuga;
|
||||
{
|
||||
30 = 2 * 3 * 5.
|
||||
858 = 2 * 3 * 11 * 13.
|
||||
1722 = 2 * 3 * 7 * 41.
|
||||
66198 = 2 * 3 * 11 * 17 * 59.
|
||||
2214408306 = 2 * 3 * 11 * 23 * 31 * 47057.
|
||||
24423128562 = 2 * 3 * 7 * 43 * 3041 * 4447.
|
||||
432749205173838 = 2 * 3 * 7 * 59 * 163 * 1381 * 775807.
|
||||
14737133470010574 = 2 * 3 * 7 * 71 * 103 * 67213 * 713863.
|
||||
550843391309130318 = 2 * 3 * 7 * 71 * 103 * 61559 * 29133437.
|
||||
244197000982499715087866346 = 2 * 3 * 11 * 23 * 31 * 47137 * 28282147 * 3892535183.
|
||||
554079914617070801288578559178 = 2 * 3 * 11 * 23 * 31 * 47059 * 2259696349 * 110725121051.
|
||||
1910667181420507984555759916338506 = 2 * 3 * 7 * 43 * 1831 * 138683 * 2861051 * 1456230512169437. }
|
||||
{$IFDEF FPC}
|
||||
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
uses
|
||||
sysutils
|
||||
{$IFDEF WINDOWS},Windows{$ENDIF}
|
||||
;
|
||||
const
|
||||
LMT =14737133470010574;// 432749205173838;//24423128562;//2214408306;
|
||||
type
|
||||
tFac = packed record
|
||||
fMul :Uint64;
|
||||
fPrime,
|
||||
fPrimIdx,
|
||||
fprimMaxIdx,dummy :Uint32;
|
||||
dummy2: Uint64;
|
||||
end;
|
||||
tFacs = array[0..15] of tFac;
|
||||
tPrimes = array[0..62157] of Uint32;//775807 see factor of 432749205173838
|
||||
// tPrimes = array[0..4875{14379}] of Uint32;//sqrt 24423128562
|
||||
// tPrimes = array[0..1807414] of Uint32;//29133437
|
||||
// tPrimes = array[0..50847533] of Uint32;// 1e9
|
||||
// tPrimes = array[0..5761454] of Uint32;//1e8
|
||||
var
|
||||
SmallPrimes: tPrimes;
|
||||
T0 : Int64;
|
||||
cnt:Uint32;
|
||||
|
||||
procedure InitSmallPrimes;
|
||||
//get primes. #0..65535.Sieving only odd numbers
|
||||
const
|
||||
//MAXLIMIT = (trunc(sqrt(LMT)+1)-1) shr 1+4;
|
||||
MAXLIMIT = 775807 DIV 2+1;//(trunc(sqrt(LMT)+1)-1) shr 1+4;
|
||||
var
|
||||
pr : array of byte;
|
||||
pPr :pByte;
|
||||
p,j,d,flipflop :NativeUInt;
|
||||
Begin
|
||||
SmallPrimes[0] := 2;
|
||||
setlength(pr,MAXLIMIT);
|
||||
pPr := @pr[0];
|
||||
p := 0;
|
||||
repeat
|
||||
repeat
|
||||
p +=1
|
||||
until pPr[p]= 0;
|
||||
j := (p+1)*p*2;
|
||||
if j>MAXLIMIT then
|
||||
BREAK;
|
||||
d := 2*p+1;
|
||||
repeat
|
||||
pPr[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 pPr[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));
|
||||
setlength(pr,0);
|
||||
end;
|
||||
|
||||
procedure OutFac(var F:tFacs;maxIdx:Uint32);
|
||||
var
|
||||
i : integer;
|
||||
begin
|
||||
write(cnt:3,' ');
|
||||
For i := 0 to maxIdx do
|
||||
write(F[i].fPrime,'*');
|
||||
write(#8,' = ',F[maxIdx].fMul);
|
||||
writeln(' ',(GetTickCount64-T0)/1000:10:3,' s');
|
||||
//readln;
|
||||
end;
|
||||
|
||||
function ChkGiuga(var F:tFacs;MaxIdx:Uint32):boolean;inline;
|
||||
var
|
||||
n : Uint64;
|
||||
idx: NativeInt;
|
||||
p : Uint32;
|
||||
begin
|
||||
n := F[MaxIdx].fMul;
|
||||
idx := MaxIdx;
|
||||
repeat
|
||||
p := F[idx].fPrime;
|
||||
result := (((n DIV p)-1)MOD p) = 0;
|
||||
if not(result) then
|
||||
EXIT;
|
||||
dec(idx);
|
||||
until idx<0;
|
||||
inc(cnt);
|
||||
end;
|
||||
|
||||
procedure InsertNextPrimeFac(var F:tFacs;idx:Uint32);
|
||||
var
|
||||
Mul : Uint64;
|
||||
i,p : uint32;
|
||||
begin
|
||||
with F[idx-1] do
|
||||
begin
|
||||
Mul := fMul;
|
||||
i := fPrimIdx;
|
||||
end;
|
||||
|
||||
while i<High(SmallPrimes) do
|
||||
begin
|
||||
inc(i);
|
||||
with F[idx] do
|
||||
begin
|
||||
if i >fprimMaxIdx then
|
||||
BREAK;
|
||||
p := SmallPrimes[i];
|
||||
if p*Mul>LMT then
|
||||
BREAK;
|
||||
fMul := p*Mul;
|
||||
fPrime := p;
|
||||
fPrimIdx := i;
|
||||
IF (Mul-1) MOD p = 0 then
|
||||
IF ChkGiuga(F,idx) then
|
||||
OutFac(F,idx);
|
||||
end;
|
||||
// max 6 factors //for lmt 24e9 need 7 factors
|
||||
if idx <5 then
|
||||
InsertNextPrimeFac(F,idx+1);
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
{$ALIGN 32}
|
||||
Facs : tFacs;
|
||||
i : integer;
|
||||
Begin
|
||||
InitSmallPrimes;
|
||||
|
||||
T0 := GetTickCount64;
|
||||
with Facs[0] do
|
||||
begin
|
||||
fMul := 2;fPrime := 2;fPrimIdx:= 0;
|
||||
end;
|
||||
with Facs[1] do
|
||||
begin
|
||||
fMul := 2*3;fPrime := 3;fPrimIdx:= 1;
|
||||
end;
|
||||
i := 2;
|
||||
//search the indices of mx found factor
|
||||
while SmallPrimes[i] < 11 do inc(i); Facs[2].fprimMaxIdx := i;
|
||||
while SmallPrimes[i] < 71 do inc(i); Facs[3].fprimMaxIdx := i;
|
||||
while SmallPrimes[i] < 3041 do inc(i); Facs[4].fprimMaxIdx := i;
|
||||
while SmallPrimes[i] < 67213 do inc(i); Facs[5].fprimMaxIdx := i;
|
||||
while SmallPrimes[i] < 775807 do inc(i); Facs[6].fprimMaxIdx := i;
|
||||
{
|
||||
writeln('Found ',cnt,' in ',(GetTickCount64-T0)/1000:10:3,' s');
|
||||
//start with 2*3*7
|
||||
with Facs[2] do
|
||||
begin
|
||||
fMul := 2*3*7;fPrime := 7;fPrimIdx:= 3;
|
||||
end;
|
||||
InsertNextPrimeFac(Facs,3);
|
||||
//start with 2*3*11
|
||||
writeln('Found ',cnt,' in ',(GetTickCount64-T0)/1000:10:3,' s');
|
||||
with Facs[2] do
|
||||
begin
|
||||
fMul := 2*3*11;fPrime := 11;fPrimIdx:= 4;
|
||||
end;
|
||||
InsertNextPrimeFac(Facs,3);
|
||||
}
|
||||
InsertNextPrimeFac(Facs,2);
|
||||
writeln('Found ',cnt,' in ',(GetTickCount64-T0)/1000:10:3,' s');
|
||||
writeln;
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue