Successive prime differences: Difference between revisions
Content added Content deleted
m (C++ - renamed class) |
MaiconSoft (talk | contribs) No edit summary |
||
Line 427: | Line 427: | ||
Last group = [997141, 997147, 997151, 997153] |
Last group = [997141, 997147, 997151, 997153] |
||
Number found = 306</pre> |
Number found = 306</pre> |
||
=={{header|Delphi}}== |
|||
{{libheader| System.SysUtils}} |
|||
{{libheader| System.Generics.Collections}} |
|||
<lang Delphi> |
|||
program Successive_prime_differences; |
|||
{$APPTYPE CONSOLE} |
|||
{$R *.res} |
|||
uses |
|||
System.SysUtils, |
|||
System.Generics.Collections; |
|||
function IsPrime(a: UInt64): Boolean; |
|||
var |
|||
d: UInt64; |
|||
begin |
|||
if (a < 2) then |
|||
exit(False); |
|||
if (a mod 2) = 0 then |
|||
exit(a = 2); |
|||
if (a mod 3) = 0 then |
|||
exit(a = 3); |
|||
d := 5; |
|||
while (d * d <= a) do |
|||
begin |
|||
if (a mod d = 0) then |
|||
Exit(false); |
|||
inc(d, 2); |
|||
if (a mod d = 0) then |
|||
Exit(false); |
|||
inc(d, 4); |
|||
end; |
|||
Result := True; |
|||
end; |
|||
function Sieve(limit: UInt64): TArray<Boolean>; |
|||
var |
|||
p, p2, 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 |
|||
p := 3; |
|||
while true do |
|||
begin |
|||
p2 := p * p; |
|||
if p2 >= limit then |
|||
break; |
|||
i := p2; |
|||
while i < limit do |
|||
begin |
|||
Result[i] := false; |
|||
inc(i, 2 * p); |
|||
end; |
|||
while true do |
|||
begin |
|||
inc(p, 2); |
|||
if Result[p] then |
|||
Break; |
|||
end; |
|||
end; |
|||
// set False all even above 2 |
|||
p := 4; |
|||
while p < limit do |
|||
begin |
|||
Result[p] := False; |
|||
inc(p, 2); |
|||
end; |
|||
end; |
|||
function Commatize(const n: UInt64): string; |
|||
var |
|||
str: string; |
|||
digits: Integer; |
|||
i: Integer; |
|||
begin |
|||
Result := ''; |
|||
str := n.ToString; |
|||
digits := str.Length; |
|||
for i := 1 to digits do |
|||
begin |
|||
if ((i > 1) and (((i - 1) mod 3) = (digits mod 3))) then |
|||
Result := Result + ','; |
|||
Result := Result + str[i]; |
|||
end; |
|||
end; |
|||
const |
|||
GroupLabel: array[1..6] of string = ('(2)', '(1)', '(2, 2)', '(2, 4)', |
|||
'(4, 2)', '(6, 4, 2)'); |
|||
var |
|||
limit, start: UInt64; |
|||
c: TArray<Boolean>; |
|||
i, j: UInt64; |
|||
Group: array[1..6] of Tlist<string>; |
|||
begin |
|||
for i := 1 to 6 do |
|||
Group[i] := Tlist<string>.Create; |
|||
limit := Trunc(1e6 - 1); |
|||
c := Sieve(limit); |
|||
start := 3; |
|||
j := start; |
|||
while j < limit do |
|||
begin |
|||
if c[j] and c[j - 2] then |
|||
Group[1].Add(format('(%d,%d)', [j - 2, j])); |
|||
if c[j] and c[j - 1] then |
|||
Group[2].Add(format('(%d,%d)', [j - 1, j])); |
|||
if (c[j - 2] and c[j]) and (c[j] and c[j + 2]) then |
|||
Group[3].Add(format('(%d,%d,%d)', [j - 2, j, j + 2])); |
|||
if (c[j - 2] and c[j]) and (c[j] and c[j + 4]) then |
|||
Group[4].Add(format('(%d,%d,%d)', [j - 2, j, j + 4])); |
|||
if j > 4 then |
|||
if (c[j - 4] and c[j]) and (c[j] and c[j + 2]) then |
|||
Group[5].Add(format('(%d,%d,%d)', [j - 4, j, j + 2])); |
|||
if j > 12 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 |
|||
Group[6].Add(format('(%d,%d,%d,%d)', [j - 12, j-6,j-2,j])); |
|||
inc(j); |
|||
end; |
|||
for i := 1 to 6 do |
|||
begin |
|||
Writeln(Group[i].Count); |
|||
Write(GroupLabel[i], ': first group = ', Group[i].First); |
|||
Writeln(', last group = ', Group[i].Last, ', count = ', Group[i].Count); |
|||
Group[i].free; |
|||
end; |
|||
readln; |
|||
end. |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
(2): first group = (3,5), last group = (999959,999961), count = 8169 |
|||
(1): first group = (2,3), last group = (2,3), count = 1 |
|||
(2, 2): first group = (3,5,7), last group = (3,5,7), count = 1 |
|||
(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 |
|||
(6, 4, 2): first group = (7,13,17,19), last group = (997141,997147,997151,997153), count = 337 |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |