Successive prime differences: Difference between revisions

Content added Content deleted
No edit summary
Line 472: Line 472:
end;
end;


function Sieve(limit: UInt64): TArray<Boolean>;
function Primes(const limit: UInt64): TArray<UInt64>;
var
var
p, p2, i: UInt64;
i: UInt64;
begin
inc(limit);
SetLength(Result, limit);
FillChar(Result[2], sizeof(Boolean) * limit - 2, 1); // all true except 1,2
FillChar(Result[0], sizeof(Boolean) * 2, 0); // 1,2 false


procedure Add(const value: UInt64);
p := 3;
while true do
begin
begin
SetLength(result, Length(result) + 1);
p2 := p * p;
Result[Length(result) - 1] := value;
if p2 >= limit then
break;
end;


begin
i := p2;
while i < limit do
if limit < 2 then
begin
exit;
Result[i] := false;
inc(i, 2 * p);
end;


// 2 is the only even prime
while true do
begin
Add(2);
inc(p, 2);
if Result[p] then
Break;
end;
end;


i := 3;
// set False all even above 2
p := 4;
while i <= limit do
while p < limit do
begin
begin
if IsPrime(i) then
Result[p] := False;
inc(p, 2);
Add(i);
inc(i, 2);
end;
end;
end;
end;
Line 528: Line 514:
Result := Result + str[i];
Result := Result + str[i];
end;
end;
end;

function CheckScan(index: Integer; p: TArray<UInt64>; pattern: array of Integer): Boolean;
var
i, last: Integer;
begin
last := Length(pattern) - 1;
for i := 0 to last do
if p[index - last + i - 1] + pattern[i] <> p[index - last + i] then
exit(False);
Result := True;
end;
end;


Line 536: Line 533:
var
var
limit, start: UInt64;
limit, start: UInt64;
c: TArray<Boolean>;
c: TArray<UInt64>;
i, j: UInt64;
i, j: UInt64;
Group: array[1..6] of Tlist<string>;
Group: array[1..6] of Tlist<string>;
Line 545: Line 542:


limit := Trunc(1e6 - 1);
limit := Trunc(1e6 - 1);
c := Sieve(limit);
c := Primes(limit);


start := 3;
for j := 1 to High(c) do

j := start;
while j < limit do
begin
begin
if c[j] and c[j - 2] then
if CheckScan(j, c, [2]) then
Group[1].Add(format('(%d,%d)', [j - 2, j]));
Group[1].Add(format('(%d,%d)', [c[j - 1], c[j]]));


if c[j] and c[j - 1] then
if CheckScan(j, c, [1]) then
Group[2].Add(format('(%d,%d)', [j - 1, j]));
Group[2].Add(format('(%d,%d)', [c[j - 1], c[j]]));


if (c[j - 2] and c[j]) and (c[j] and c[j + 2]) then
if j > 1 then
begin
Group[3].Add(format('(%d,%d,%d)', [j - 2, j, j + 2]));
if CheckScan(j, c, [2, 2]) then
Group[3].Add(format('(%d,%d,%d)', [c[j - 2], c[j - 1], c[j]]));


if (c[j - 2] and c[j]) and (c[j] and c[j + 4]) then
if CheckScan(j, c, [2, 4]) then
Group[4].Add(format('(%d,%d,%d)', [j - 2, j, j + 4]));
Group[4].Add(format('(%d,%d,%d)', [c[j - 2], c[j - 1], c[j]]));


if j > 4 then
if CheckScan(j, c, [4, 2]) then
if (c[j - 4] and c[j]) and (c[j] and c[j + 2]) then
Group[5].Add(format('(%d,%d,%d)', [c[j - 2], c[j - 1], c[j]]));
end;
Group[5].Add(format('(%d,%d,%d)', [j - 4, j, j + 2]));


if j > 12 then
if j > 2 then
if (c[j - 12] and c[j-6]) and (c[j-6] and c[j -2]) and (c[j-2] and c[j]) then
if CheckScan(j, c, [6, 4, 2]) then
Group[6].Add(format('(%d,%d,%d,%d)', [j - 12, j-6,j-2,j]));
Group[6].Add(format('(%d,%d,%d,%d)', [c[j - 3], c[j - 2], c[j - 1], c[j]]));


inc(j);
end;
end;


for i := 1 to 6 do
for i := 1 to 6 do
begin
begin
Writeln(Group[i].Count);
Write(GroupLabel[i], ': first group = ', Group[i].First);
Write(GroupLabel[i], ': first group = ', Group[i].First);
Writeln(', last group = ', Group[i].Last, ', count = ', Group[i].Count);
Writeln(', last group = ', Group[i].last, ', count = ', Group[i].Count);
Group[i].free;
Group[i].free;
end;
end;


readln;
readln;

end.
end.


Line 596: Line 589:
(2, 4): first group = (5,7,11), last group = (999431,999433,999437), count = 1393
(2, 4): first group = (5,7,11), last group = (999431,999433,999437), count = 1393
(4, 2): first group = (7,11,13), last group = (997807,997811,997813), count = 1444
(4, 2): first group = (7,11,13), last group = (997807,997811,997813), count = 1444
(6, 4, 2): first group = (7,13,17,19), last group = (997141,997147,997151,997153), count = 337
(6, 4, 2): first group = (31,37,41,43), last group = (997141,997147,997151,997153), count = 306
</pre>
</pre>