Meissel–Mertens constant: Difference between revisions
Content deleted Content added
Drkameleon (talk | contribs) added Arturo |
No edit summary |
||
Line 285: | Line 285: | ||
{{out}} |
{{out}} |
||
<pre>MM = 0.261497</pre> |
<pre>MM = 0.261497</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
This program doesn't test the limits of numbers in Delphi. The limit is the processing time. The time to process 10^7 is 3 minutes. The time to process 10^8 is nearly two hours. As a result, process beyond 10^8 is pretty much impossible and a different strategy would be required |
|||
<syntaxhighlight lang="Delphi"> |
|||
function IsPrime(N: int64): boolean; |
|||
{Fast, optimised prime test} |
|||
var I,Stop: int64; |
|||
begin |
|||
if (N = 2) or (N=3) then Result:=true |
|||
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false |
|||
else |
|||
begin |
|||
I:=5; |
|||
Stop:=Trunc(sqrt(N+0.0)); |
|||
Result:=False; |
|||
while I<=Stop do |
|||
begin |
|||
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit; |
|||
Inc(I,6); |
|||
end; |
|||
Result:=True; |
|||
end; |
|||
end; |
|||
function GetNextPrime(var Start: integer): integer; |
|||
{Get the next prime number after Start} |
|||
{Start is passed by "reference," so the |
|||
{original variable is incremented} |
|||
begin |
|||
repeat Inc(Start) |
|||
until IsPrime(Start); |
|||
Result:=Start; |
|||
end; |
|||
function MeisselMertens(Depth: integer; Prog: TProgress): extended; |
|||
{Calculate MM value up a certain Depth} |
|||
var I,P: integer; |
|||
const Euler = 0.57721566490153286; |
|||
begin |
|||
Result:=0; |
|||
P:=1; |
|||
for I:=1 to Depth do |
|||
begin |
|||
P:=GetNextPrime(P); |
|||
Result:=Result+Ln(1-(1/P)) + (1/P); |
|||
if Assigned(Prog) and ((I mod 10000)=0) then |
|||
HandleProgress(MulDiv(100,I,Depth)); |
|||
end; |
|||
Result:=Result+Euler; |
|||
end; |
|||
procedure ShowMeisselMertens(Memo: TMemo; Prog: TProgress); |
|||
var I,IT,Digits: integer; |
|||
var M,Last: extended; |
|||
begin |
|||
Memo.Lines.Add('Primes Digits M'); |
|||
Memo.Lines.Add('-----------------------------------------'); |
|||
Last:=0; |
|||
IT:=1; |
|||
{Calculate MM to specified Power of 10} |
|||
for I:=1 to 8 do |
|||
begin |
|||
IT:=IT*10; |
|||
M:=MeisselMertens(IT,Prog); |
|||
{Calculated Digits of accuracy} |
|||
Digits:=Trunc(abs(Log(abs(M-Last)))); |
|||
Memo.Lines.Add(Format('10^%2d %7d %25.18f',[I,Digits,M])); |
|||
Last:=M |
|||
end; |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Primes Digits M |
|||
----------------------------------------- |
|||
10^ 1 0 0.265160104017311294 |
|||
10^ 2 2 0.261624626172936147 |
|||
10^ 3 3 0.261503563616850571 |
|||
10^ 4 5 0.261497594498432488 |
|||
10^ 5 6 0.261497238454297260 |
|||
10^ 6 7 0.261497214692305277 |
|||
10^ 7 8 0.261497212987251183 |
|||
10^ 8 9 0.261497212858582276 |
|||
</pre> |
|||
=={{header|Dart}}== |
=={{header|Dart}}== |