Meissel–Mertens constant: Difference between revisions
Added Easylang
m (Added language identifier.) |
(Added Easylang) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 286:
<pre>MM = 0.261497</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<double> list_prime_reciprocals(const int32_t& limit) {
const int32_t half_limit = ( limit % 2 == 0 ) ? limit / 2 : 1 + limit / 2;
std::vector<bool> composite(half_limit);
for ( int32_t i = 1, p = 3; i < half_limit; p += 2, ++i ) {
if ( ! composite[i] ) {
for ( int32_t a = i + p; a < half_limit; a = a + p ) {
composite[a] = true;
}
}
}
std::vector<double> result(composite.size());
result[0] = 0.5;
for ( int32_t i = 1, p = 3; i < half_limit; p += 2, ++i ) {
if ( ! composite[i] ) {
result.emplace_back(1.0 / p);
}
}
return result;
}
int main() {
std::vector<double> prime_reciprocals = list_prime_reciprocals(100'000'000);
const double euler = 0.577'215'664'901'532'861;
double sum = 0.0;
for ( double reciprocal : prime_reciprocals ) {
sum += reciprocal + log(1.0 - reciprocal);
}
const double meissel_mertens = euler + sum;
std::cout << "The Meissel-Mertens constant = " << std::setprecision(8) << meissel_mertens << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
The Meissel-Mertens constant = 0.26149721
</pre>
=={{header|Delphi}}==
Line 402 ⟶ 447:
{{out}}
<pre>MM = 0.2614972131057144</pre>
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
fastfunc isprim num .
if num mod 2 = 0
if num = 2
return 1
.
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
func log x .
return log10 x / log10 2.7182818284590452354
.
euler = 0.5772156649
for x = 2 to 1e6
if isprim x = 1
m += log (1 - (1 / x)) + (1 / x)
.
.
numfmt 11 0
print "mm = " & euler + m
</syntaxhighlight>
{{out}}
<pre>
mm = 0.26149724673
</pre>
=={{header|FreeBASIC}}==
Line 495 ⟶ 577:
import java.util.List;
public final class MeisselMertensConstant {
Line 505 ⟶ 584:
double sum = 0.0;
for ( double reciprocal : primeReciprocals ) {
sum +=
}
final double
System.out.println(String.format("%s%.9f", "The Meissel-Mertens constant = ",
}
Line 516 ⟶ 595:
sieve.set(2, aLimit + 1);
for ( int j = i * i; j <= aLimit; j += i ) {
sieve.clear(j);
Line 535 ⟶ 613:
{{ out }}
<pre>
The Meissel-Mertens constant = 0.
</pre>
Line 551 ⟶ 629:
@show meissel_mertens(100_000_000) # meissel_mertens(100000000) = 0.2614972128591237
</syntaxhighlight>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
meissel_mertens:%gamma+lsum(log(1-(1/i))+(1/i),i,primes(2,10000000)),numer;
</syntaxhighlight>
{{out}}
<pre>
0.2614972157776471
</pre>
=={{header|Nim}}==
Line 664 ⟶ 751:
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36;
use ntheory
my $s;
say my $result = $s + .57721566490153286;</syntaxhighlight>
{{out}}
Line 834 ⟶ 921:
<pre>
1: 0.262733140866
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var sum = 0
1e7.each_prime {|p|
with (1f/p) {|t|
sum += (log(1 - t) + t)
}
}
say sum+Num.EulerGamma</syntaxhighlight>
{{out}}
<pre>
0.26149721577767111119422410228297206467931376306
</pre>
Line 841 ⟶ 941:
{{libheader|Wren-fmt}}
It will be seen that this is converging to the correct answer though we'd need to add a lot more primes to obtain a valid 11th digit.
<syntaxhighlight lang="
import "./fmt" for Fmt
Line 878 ⟶ 978:
{{libheader|Wren-gmp}}
This agrees with the Phix entry that the 1,000th digit after the decimal point is '8' after rounding (according to OEIS it's actually '7' but the next one is '7' also).
<syntaxhighlight lang="
import "./math" for Int
import "./fmt" for Fmt
|