Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/MeisselMertens_constant

View file

@ -0,0 +1,37 @@
;Task:
Calculate MeisselMertens constant up to a precision your language can handle.
;Motivation:
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.
;Example:
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...
;See:
:*   Details in the Wikipedia article:  [https://en.wikipedia.org/wiki/Meissel%E2%80%93Mertens_constant MeisselMertens constant]
<br /><br />

View file

@ -0,0 +1,22 @@
F primes_up_to_limit(Int limit)
[Int] r
I limit >= 2
r.append(2)
V isprime = [1B] * ((limit - 1) I/ 2)
V sieveend = Int(sqrt(limit))
L(i) 0 .< isprime.len
I isprime[i]
Int p = i * 2 + 3
r.append(p)
I i <= sieveend
L(j) ((p * p - 3) >> 1 .< isprime.len).step(p)
isprime[j] = 0B
R r
V euler = 0.57721566490153286
V m = 0.0
L(x) primes_up_to_limit(10'000'000)
m += log(1 - (1 / x)) + (1 / x)
print(MM = #.16.format(euler + m))

View file

@ -0,0 +1,45 @@
BEGIN # compute an approximation to the Meissel-Mertens constant #
# construct a sieve of odd primes #
[ 0 : 10 000 000 ]BOOL primes;
BEGIN
FOR i TO UPB primes DO primes[ i ] := TRUE OD;
INT ip := 1;
FOR i WHILE i + ( ip +:= 2 ) <= UPB primes DO
IF primes[ i ] THEN
FOR s FROM i + ip BY ip TO UPB primes DO primes[ s ] := FALSE OD
FI
OD
END;
# sum the reciprocals of the primes #
INT p count := 1;
INT last p := 0;
LONG REAL sum := long ln( 0.5 ) + 0.5;
INT p := 1;
INT p10 := 10;
# Euler's constant from the wikipedia, truncated for LONG REAL #
LONG REAL eulers constant = 0.5772156649015328606 # 0651209008240243104215933593992 #;
FOR i TO UPB primes DO
p +:= 2;
IF primes[ i ] THEN
LONG REAL rp = 1 / LENG p;
sum +:= long ln( 1 - rp ) + rp;
p count +:= 1;
last p := p;
IF p count = p10 THEN
print( ( "after ", whole( p count, -8 ), " primes, the approximation is: "
, fixed( sum + eulers constant, -14, 12 )
, ", last prime considered: ", whole( last p, 0 )
, newline
)
);
p10 := IF p10 < 1 000 000 THEN p10 * 10 ELSE p10 + 1 000 000 FI
FI
FI
OD;
print( ( "after ", whole( p count, -8 ), " primes, the approximation is: "
, fixed( sum + eulers constant, -14, 12 )
, ", last prime considered: ", whole( last p, 0 )
, newline
)
)
END

View file

@ -0,0 +1,11 @@
meisselMertens: function [depth][
Euler: 0.57721566490153286
m: (1//2) + ln 1-1//2
loop range.step:2 3 depth 'x ->
if prime? x ->
m: m + (1//x) + ln 1-1//x
return m + Euler
]
print meisselMertens 10000000

View file

@ -0,0 +1,19 @@
Euler = 0.5772156649
m = 0
for x = 2 to 1e6 # more prime numbers do not add more precision
if isPrime(x) then m += log(1-(1/x)) + (1/x)
next x
print "MM = "; Euler + m
print Euler
end
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function

View file

@ -0,0 +1,18 @@
import 'dart:math';
bool isPrime(var n) {
if (n <= 1) return false;
if (n == 2) return true;
for (var i = 2; i <= sqrt(n); i++) if (n % i == 0) return false;
return true;
}
void main() {
const double euler = 0.57721566490153286;
double m = 0.0;
for (var x = 2; x <= 1e8; x++)
if (isPrime(x)) m += log(1 - (1 / x)) + (1 / x);
print('MM = ${euler + m}');
}

View file

@ -0,0 +1,69 @@
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 7 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;

View file

@ -0,0 +1,10 @@
'#include "isprime.bas"
Const As Double Euler = 0.57721566490153286
Dim As Double m = 0
For x As Ulongint = 2 To 1e8
If isPrime(x) Then m += Log(1-(1/x)) + (1/x)
Next x
Print "MM ="; Euler + m
Sleep

View file

@ -0,0 +1,33 @@
package main
import (
"fmt"
"math"
"rcu"
)
func contains(a []int, f int) bool {
for _, e := range a {
if e == f {
return true
}
}
return false
}
func main() {
const euler = 0.57721566490153286
primes := rcu.Primes(1 << 31)
pc := len(primes)
sum := 0.0
fmt.Println("Primes added M")
fmt.Println("------------ --------------")
for i, p := range primes {
rp := 1.0 / float64(p)
sum += math.Log(1.0-rp) + rp
c := i + 1
if (c%1e7) == 0 || c == pc {
fmt.Printf("%11s %0.12f\n", rcu.Commatize(c), sum+euler)
}
}
}

View file

@ -0,0 +1,2 @@
Euler=: 0.57721566490153286
MM=: {{ Euler + +/ (+ ^.@-.)@% p: i. y }}

View file

@ -0,0 +1,2 @@
0j13 ": MM 1e8
0.2614972128591

View file

@ -0,0 +1 @@
mm=: (% 10x(^#)10#.inv]) 26149721284764278375542683860869585905156664826119920619206421392x

View file

@ -0,0 +1,2 @@
0j65":mm
0.26149721284764278375542683860869585905156664826119920619206421392

View file

@ -0,0 +1,41 @@
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
/**
* Calculates the Meissel-Mertens constant correct to 9 s.f. in approximately 15 seconds.
*/
public final class MeisselMertensConstant {
public static void main(String[] aArgs) {
List<Double> primeReciprocals = listPrimeReciprocals(1_000_000_000);
final double euler = 0.577_215_664_901_532_861;
double sum = 0.0;
for ( double reciprocal : primeReciprocals ) {
sum += reciprocal + Math.log(1.0 - reciprocal);
}
final double constant = euler + sum;
System.out.println("The Meissel-Mertens constant = " + constant);
}
private static List<Double> listPrimeReciprocals(int aLimit) {
BitSet sieve = new BitSet(aLimit + 1);
sieve.set(2, aLimit + 1);
final int squareRoot = (int) Math.sqrt(aLimit);
for ( int i = 2; i <= squareRoot; i = sieve.nextSetBit(i + 1) ) {
for ( int j = i * i; j <= aLimit; j += i ) {
sieve.clear(j);
}
}
List<Double> result = new ArrayList<Double>(sieve.cardinality());
for ( int i = 2; i >= 0; i = sieve.nextSetBit(i + 1) ) {
result.add(1.0 / i);
}
return result;
}
}

View file

@ -0,0 +1,9 @@
using Base.MathConstants # sets constant γ = 0.5772156649015...
using Primes
""" Approximate the Meissel-Mertons constant. """
function meissel_mertens(iterations = 100_000_000)
return mapreduce(p ->(d = 1/p; log(1 - d) + d), +, primes(prime(iterations))) + γ
end
@show meissel_mertens(100_000_000) # meissel_mertens(100000000) = 0.2614972128591237

View file

@ -0,0 +1,4 @@
PrimeNumbers = Select[Range[100000000], PrimeQ[#] &]; (*all primes in the first 100 000 000 numbers, this takes a toll on my computer's CPU and RAM*)
MM = N[Total[Log[1 - 1/PrimeNumbers] + 1/PrimeNumbers] + EulerGamma, 10] (*Calculating it up to a precision of 10, this is correct up to 8 digits*)
AnalyticMMto305 = N[EulerGamma + Sum[MoebiusMu[n]/n Log[Zeta[n]], {n, 2, 1000}], 1000] (*Precise up to 305 digits*)
AnalyticMM = N[EulerGamma + Sum[MoebiusMu[n]/n Log[Zeta[n]], {n, 2, 10000}], 1001] (*Precise up to at least 1000 digits*)

View file

@ -0,0 +1,43 @@
import std/[math, strformat, strutils]
proc initPrimes(N: static int): seq[int] =
## Initialize the list of primes.
const M = 2 * N - 1
var composite = newSeq[bool](N)
composite[0] = true # 1 is not prime.
# Conversions from index to value and value to index.
template index(n: int): int = (n - 1) shr 1
template value(idx: int): int = idx shl 1 + 1
# Fill the sieve.
var n = 3
while n * n <= M:
if not composite[n.index]:
for k in countup(n * n, M, 2 * n):
composite[k.index] = true
inc n, 2
# Build list of primes.
result = @[2]
for idx in 0..composite.high:
if not composite[idx]:
result.add idx.value
const N = 2^30
let primes = initPrimes(N)
echo "Primes added M"
echo "──────────── ──────────────"
const γ = 0.57721566490153286 # EulerMascheroni constant.
let primeCount = primes.len
var sum = 0.0
var count = 0
for p in primes:
let rp = 1 / p
sum += ln(1 - rp) + rp
inc count
if count mod 10_000_000 == 0 or count == primeCount:
echo &"{insertSep($count):>11} {sum+γ:.12}"

View file

@ -0,0 +1,8 @@
{
MM(t)=
my(s=0);
forprime(p = 2, t,
s += log(1.-1./p)+1./p
);
Euler+s
};

View file

@ -0,0 +1,16 @@
{
Meissel_Mertens(d)=
default(realprecision, d);
my(prec = default(realprecision), z = 0, y = 0, q);
forprime(p = 2 , 7,
z += log(1.-1./p)+1./p
);
for(k = 2, prec,
q = 1;
forprime(p = 2, 7,
q *= 1.-p^-k
);
y += moebius(k)*log(zeta(k)*q)/k
);
Euler+z+y
};

View file

@ -0,0 +1,6 @@
use v5.36;
use ntheory 'is_prime';
my $s;
is_prime $_ and $s += log(1 - 1/$_)+1/$_ for 2 .. 10**9;
say my $result = $s + .57721566490153286;

View file

@ -0,0 +1,33 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (but perhaps a bit too slow)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">mmc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.2614972128476427837554268386086958590516</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">smmc</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0.2614972128476427837554268386086958590516"</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.57721566490153286</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dpa</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p10</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">adp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">st</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"%.0f"</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Primes added M\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"------------ --------------\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">adp</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">rp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">rp</span>
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mmc</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">dpa</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ft</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">trunc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p10</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">p10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (as below)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ft</span><span style="color: #0000FF;">=</span><span style="color: #000000;">st</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">--
-- We have to artifically calculate a "truncated t", aka tt,
-- to prevent say 0.2..299[&gt;5..] being automatically rounded
-- by printf() to 0.2..300, otherwise it just "looks wrong".
--</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">tt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trunc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1e12</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">1e12</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,11d %0.12f (accurate to %d d.p.)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">adp</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">adp</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%.%df"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">adp</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">st</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">smmc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">adp</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">dpa</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">10</span>
<span style="color: #000000;">p10</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(actual value 0.26149721284764278375542683860869)\n"</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,54 @@
-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- no mpfr_zeta[_ui]() in mpfr.js, as yet, or mpfr_log() or mpfr_const_euler(), for that matter</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">moebius</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">))?-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Meissel_Mertens</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (d decimal places)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- z += log(1-1/p)+1/p</span>
<span style="color: #7060A8;">mpfr_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_div_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_si_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">d</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">moebius</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpfr_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- q *= 1-power(p,-k)</span>
<span style="color: #7060A8;">mpfr_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_pow_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_si_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- y += moebius(k)*log(zeta(k)*q)/k</span>
<span style="color: #7060A8;">mpfr_zeta_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_div_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rp</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- res := EULER+z+y</span>
<span style="color: #000000;">mpfr_const_euler</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Meissel_Mertens</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1001</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mpfr_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1001</span><span style="color: #0000FF;">))</span>
<!--

View file

@ -0,0 +1,30 @@
Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v % 2 = 0 : ProcedureReturn #False
ElseIf v < 9 : ProcedureReturn #True
ElseIf v % 3 = 0 : ProcedureReturn #False
Else
Protected r = Round(Sqr(v), #PB_Round_Down)
Protected f = 5
While f <= r
If v % f = 0 Or v % (f + 2) = 0
ProcedureReturn #False
EndIf
f + 6
Wend
EndIf
ProcedureReturn #True
EndProcedure
OpenConsole()
Euler.d = 0.5772156649 ;0153286
For x.i = 2 To 1e8
If isPrime(x)
m.d = m + Log(1-(1/x)) + (1/x)
EndIf
Next x
PrintN("MM = " + StrD(Euler + m))
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()

View file

@ -0,0 +1,19 @@
#!/usr/bin/python
from math import log
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
if __name__ == '__main__':
Euler = 0.57721566490153286
m = 0
for x in range(2, 10_000_000):
if isPrime(x):
m += log(1-(1/x)) + (1/x)
print("MM =", Euler + m)

View file

@ -0,0 +1,5 @@
# 20221011 Raku programming solution
my $s;
.is-prime and $s += log(1-1/$_)+1/$_ for 2 .. 10**8;
say $s + .57721566490153286

View file

@ -0,0 +1,17 @@
function isPrime(n)
if n < 2 then isPrime = 0 : goto [exit]
if n = 2 then isPrime = 1 : goto [exit]
if n mod 2 = 0 then isPrime = 0 : goto [exit]
isPrime = 1
for i = 3 to int(n^.5) step 2
if n mod i = 0 then isPrime = 0 : goto [exit]
next i
[exit]
end function
e = 0.5772156
for x = 2 to 100000 ' more prime numbers do not add more precision
if isPrime(x) then m = m + log(1-(1/x)) + (1/x)
next x
print "MM = "; e + m

View file

@ -0,0 +1,25 @@
FUNCTION isPrime (n)
IF n = 2 THEN
LET isPrime = 1
ELSEIF n <= 1 OR REMAINDER(n, 2) = 0 THEN
LET isPrime = 0
ELSE
LET isPrime = 1
FOR i = 3 TO INT(SQR(n)) STEP 2
IF REMAINDER(n, i) = 0 THEN
LET isPrime = 0
EXIT FUNCTION
END IF
NEXT i
END IF
END FUNCTION
LET e = .5772156649
FOR x = 2 to 1e6 !more prime numbers do not add more precision
IF isPrime(x) = 1 THEN
LET m = m + LOG(1-(1/x)) + (1/x)
END IF
NEXT x
PRINT "MM ="; e + m
END

View file

@ -0,0 +1,16 @@
import "./math" for Int
import "./fmt" for Fmt
var euler = 0.57721566490153286
var primes = Int.primeSieve(2.pow(31))
var pc = primes.count
var sum = 0
var c = 0
System.print("Primes added M")
System.print("------------ --------------")
for (p in primes) {
var rp = 1/p
sum = (1-rp).log + rp + sum
c = c + 1
if ((c % 1e7) == 0 || c == pc) Fmt.print("$,11d $0.12f", c, sum + euler)
}

View file

@ -0,0 +1,51 @@
import "./gmp" for Mpf
import "./math" for Int
import "./fmt" for Fmt
var isSquareFree = Fn.new { |n|
var i = 2
while (i * i <= n) {
if (n%(i*i) == 0) return false
i = (i > 2) ? i + 2 : i + 1
}
return true
}
var mu = Fn.new { |n|
if (n < 1) Fiber.abort("Argument must be a positive integer")
if (n == 1) return 1
var sqFree = isSquareFree.call(n)
var factors = Int.primeFactors(n)
if (sqFree && factors.count % 2 == 0) return 1
if (sqFree) return -1
return 0
}
var meisselMertens = Fn.new { |d|
Mpf.defaultPrec = d
var z = Mpf.zero
var y = Mpf.zero
var r = Mpf.new()
var q = Mpf.new()
var t = Mpf.new()
var m = Mpf.new()
for (p in [2, 3, 5, 7]) {
r.setUi(p).inv
t.uiSub(1, r).log.add(r)
z.add(t, z)
}
for (k in 2..d) {
q.setUi(1)
for (p in [2, 3, 5, 7]) {
r.setUi(p).inv
t.uiSub(1, r.pow(k))
q.mul(t)
}
m.setSi(mu.call(k))
t.zetaUi(k).mul(q).log.mul(m).div(k)
y.add(t, y)
}
return Mpf.euler.add(z).add(y)
}
Fmt.print("$20a", meisselMertens.call(3300).toString(1001))

View file

@ -0,0 +1,24 @@
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);
]

View file

@ -0,0 +1,18 @@
e = 0.5772156
for x = 2 to 1e6 // more prime numbers do not add more precision
if isPrime(x) m = m + log(1-(1/x)) + (1/x)
next x
print "MM = ", e + m
end
sub isPrime(v)
if v < 2 return False
if mod(v, 2) = 0 return v = 2
if mod(v, 3) = 0 return v = 3
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub