How to resolve the algorithm Meissel–Mertens constant step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Meissel–Mertens constant step by step in the XPL0 programming language

Table of Contents

Problem Statement

Calculate Meissel–Mertens constant up to a precision your language can handle.

Analogous to Euler's constant, which is important in determining the sum of reciprocal natural numbers, Meissel-Mertens' constant is important in calculating the sum of reciprocal primes.

We consider the finite sum of reciprocal natural numbers: 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n this sum can be well approximated with: log(n) + E where E denotes Euler's constant: 0.57721... log(n) denotes the natural logarithm of n.

Now consider the finite sum of reciprocal primes: 1/2 + 1/3 + 1/5 + 1/7 + 1/11 ... 1/p this sum can be well approximated with: log( log(p) ) + M where M denotes Meissel-Mertens constant: 0.26149...

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Meissel–Mertens constant step by step in the XPL0 programming language

Source code in the xpl0 programming language

func IsPrime(N); \Return 'true' if N is prime
int  N, D;
[if N < 2 then return false;
if (N&1) = 0 then return N = 2;
if rem(N/3) = 0 then return N = 3;
D:= 5;
while D*D <= N do
    [if rem(N/D) = 0 then return false;
    D:= D+2;
    if rem(N/D) = 0 then return false;
    D:= D+4;
    ];
return true;
];

def  Euler = 0.57721566490153286;
real M;  int  P;
[M:= 0.;
for P:= 2 to 100_000_000 do
    if IsPrime(P) then
        M:= M + Ln(1. - 1./float(P)) + 1./float(P);
Format(1, 16);
Text(0, "MM = ");  RlOut(0, Euler + M);  CrLf(0);
]

  

You may also check:How to resolve the algorithm Comments step by step in the ProDOS programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the 11l programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the BQN programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the MiniZinc programming language