Goldbach's comet: Difference between revisions

no edit summary
(Nim solution.)
No edit summary
Line 626:
 
Instead, for G(1e6), we find all primes less than a million, subtract each from 1 million and count how many of the differences are prime and cut that in half. We cut that sum in half because this approach counts each pair twice (once with the smallest value first, again with the smallest value second -- since 1e6 is not the square of a prime we do not have a prime which appears twice in one of these sums).
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function GetGoldbachCount(N: integer): integer;
{Count number of prime number combinations add up to N }
var I: integer;
begin
Result:=0;
{Look at all number pairs that add up to N}
{And see if they are prime}
for I:=1 to N div 2 do
if IsPrime(I) and IsPrime(N-I) then Inc(Result);
end;
 
procedure ShowGoldbachComet(Memo: TMemo);
{Show first 100 Goldback numbers}
var I,N,Cnt,C: integer;
var S: string;
begin
Cnt:=0; N:=2; S:='';
while true do
begin
C:=GetGoldbachCount(N);
if C>0 then
begin
Inc(Cnt);
S:=S+Format('%3d',[C]);
if (Cnt mod 10)=0 then S:=S+CRLF;
if Cnt>=100 then break;
end;
Inc(N,2);
end;
Memo.Lines.Add(S);
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
1 1 1 2 1 2 2 2 2 3
3 3 2 3 2 4 4 2 3 4
3 4 5 4 3 5 3 4 6 3
5 6 2 5 6 5 5 7 4 5
8 5 4 9 4 5 7 3 6 8
5 6 8 6 7 10 6 6 12 4
5 10 3 7 9 6 5 8 7 8
11 6 5 12 4 8 11 5 8 10
5 6 13 9 6 11 7 7 14 6
8 13 5 8 11 7 9 13 8 9
 
 
Elapsed Time: 1.512 ms.
 
</pre>
 
 
=={{header|jq}}==
465

edits