Computing the twin prime constant with Mathematica
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
The twin prime constant is defined as
$$
C _2 = prod limits_p geqslant 3 , fracp(p-2)(p-1)^2.
$$
Translating this directly in Mathematica code gives:
N[Product[Prime[i] (Prime[i] - 2)/(Prime[i] - 1)^2, i, 2, Infinity]]
However, when I execute this code I get the following error messages:
What am I doing wrong?
code-review prime-numbers
add a comment |Â
up vote
1
down vote
favorite
The twin prime constant is defined as
$$
C _2 = prod limits_p geqslant 3 , fracp(p-2)(p-1)^2.
$$
Translating this directly in Mathematica code gives:
N[Product[Prime[i] (Prime[i] - 2)/(Prime[i] - 1)^2, i, 2, Infinity]]
However, when I execute this code I get the following error messages:
What am I doing wrong?
code-review prime-numbers
2
It's just that some of the algorithms used internally byNProduct
sometimes try to evaluate terms for inexact arguments, so a more clever approach is necessary.
– J. M. is computer-less♦
2 hours ago
1
I didn't get this message. However I suggest not usingInfinity
insideProduct
, as there will possibly be no results. Try a big number instead, like 100000
– t-smart
1 hour ago
1
This evaluates fine if you simply use a really big number, like 10^6
– Carl Lange
1 hour ago
3
See oeis.org/A005597 for better methods, how to compute this constant.
– Vaclav Kotesovec
1 hour ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
The twin prime constant is defined as
$$
C _2 = prod limits_p geqslant 3 , fracp(p-2)(p-1)^2.
$$
Translating this directly in Mathematica code gives:
N[Product[Prime[i] (Prime[i] - 2)/(Prime[i] - 1)^2, i, 2, Infinity]]
However, when I execute this code I get the following error messages:
What am I doing wrong?
code-review prime-numbers
The twin prime constant is defined as
$$
C _2 = prod limits_p geqslant 3 , fracp(p-2)(p-1)^2.
$$
Translating this directly in Mathematica code gives:
N[Product[Prime[i] (Prime[i] - 2)/(Prime[i] - 1)^2, i, 2, Infinity]]
However, when I execute this code I get the following error messages:
What am I doing wrong?
code-review prime-numbers
code-review prime-numbers
asked 2 hours ago
PierreTheFermented
21717
21717
2
It's just that some of the algorithms used internally byNProduct
sometimes try to evaluate terms for inexact arguments, so a more clever approach is necessary.
– J. M. is computer-less♦
2 hours ago
1
I didn't get this message. However I suggest not usingInfinity
insideProduct
, as there will possibly be no results. Try a big number instead, like 100000
– t-smart
1 hour ago
1
This evaluates fine if you simply use a really big number, like 10^6
– Carl Lange
1 hour ago
3
See oeis.org/A005597 for better methods, how to compute this constant.
– Vaclav Kotesovec
1 hour ago
add a comment |Â
2
It's just that some of the algorithms used internally byNProduct
sometimes try to evaluate terms for inexact arguments, so a more clever approach is necessary.
– J. M. is computer-less♦
2 hours ago
1
I didn't get this message. However I suggest not usingInfinity
insideProduct
, as there will possibly be no results. Try a big number instead, like 100000
– t-smart
1 hour ago
1
This evaluates fine if you simply use a really big number, like 10^6
– Carl Lange
1 hour ago
3
See oeis.org/A005597 for better methods, how to compute this constant.
– Vaclav Kotesovec
1 hour ago
2
2
It's just that some of the algorithms used internally by
NProduct
sometimes try to evaluate terms for inexact arguments, so a more clever approach is necessary.– J. M. is computer-less♦
2 hours ago
It's just that some of the algorithms used internally by
NProduct
sometimes try to evaluate terms for inexact arguments, so a more clever approach is necessary.– J. M. is computer-less♦
2 hours ago
1
1
I didn't get this message. However I suggest not using
Infinity
inside Product
, as there will possibly be no results. Try a big number instead, like 100000– t-smart
1 hour ago
I didn't get this message. However I suggest not using
Infinity
inside Product
, as there will possibly be no results. Try a big number instead, like 100000– t-smart
1 hour ago
1
1
This evaluates fine if you simply use a really big number, like 10^6
– Carl Lange
1 hour ago
This evaluates fine if you simply use a really big number, like 10^6
– Carl Lange
1 hour ago
3
3
See oeis.org/A005597 for better methods, how to compute this constant.
– Vaclav Kotesovec
1 hour ago
See oeis.org/A005597 for better methods, how to compute this constant.
– Vaclav Kotesovec
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
The problem with your code is that N[Product[...]]
calls NProduct[...]
which turns integers into reals before Prime
can evaluate, causing the error: See Details and Options section of documentation of NProduct
.
For your calculation, you actually do not need these. It looks like the result converges around $0.660162$:
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k]], k, 5]
0.665138, 0.66033, 0.66017, 0.660162, 0.660162
Since the product is monotonic and you are using N
at the end, it is not necessary to carry out whole multiplication all the way upto infinity: Depending on the interested accuracy, you can go to higher orders as well. For example,
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k], 8], k, 5]
yields
0.66513840, 0.66033029, 0.66017020, 0.66016232, 0.66016185
which reveals that using only first $10^5$ primes, which is all primes upto $1299709$, is sufficient to get a result with error of the order $10^-6$.
For the record: Mathematica fails to do the calculation analytically:
Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, Infinity]
$prod _i=2^infty fracleft(p_i-2right) p_ileft(p_i-1right)^2$
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
add a comment |Â
up vote
3
down vote
As not all primes are known, Mathematica will have a problem to compute the constant exactly. But the sequence appears to converge quickly to some number $0.660162...$. (Soner posted that already.)
So let me give a way to compute the sequence of partial products in a more efficient way.
n = 10000;
seq = FoldList[#1 #2 (#2 - 2)/(#2 - 1)^2 &, 1, Prime[Range[2, n]]];
N[seq[[-1]]]
ListLinePlot[N[seq], ConstantArray[N[seq[[-1]]], Length[seq]],
PlotRange -> 1/2, 1,
PlotStyle -> Thick, Dashed
]
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The problem with your code is that N[Product[...]]
calls NProduct[...]
which turns integers into reals before Prime
can evaluate, causing the error: See Details and Options section of documentation of NProduct
.
For your calculation, you actually do not need these. It looks like the result converges around $0.660162$:
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k]], k, 5]
0.665138, 0.66033, 0.66017, 0.660162, 0.660162
Since the product is monotonic and you are using N
at the end, it is not necessary to carry out whole multiplication all the way upto infinity: Depending on the interested accuracy, you can go to higher orders as well. For example,
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k], 8], k, 5]
yields
0.66513840, 0.66033029, 0.66017020, 0.66016232, 0.66016185
which reveals that using only first $10^5$ primes, which is all primes upto $1299709$, is sufficient to get a result with error of the order $10^-6$.
For the record: Mathematica fails to do the calculation analytically:
Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, Infinity]
$prod _i=2^infty fracleft(p_i-2right) p_ileft(p_i-1right)^2$
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
add a comment |Â
up vote
3
down vote
accepted
The problem with your code is that N[Product[...]]
calls NProduct[...]
which turns integers into reals before Prime
can evaluate, causing the error: See Details and Options section of documentation of NProduct
.
For your calculation, you actually do not need these. It looks like the result converges around $0.660162$:
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k]], k, 5]
0.665138, 0.66033, 0.66017, 0.660162, 0.660162
Since the product is monotonic and you are using N
at the end, it is not necessary to carry out whole multiplication all the way upto infinity: Depending on the interested accuracy, you can go to higher orders as well. For example,
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k], 8], k, 5]
yields
0.66513840, 0.66033029, 0.66017020, 0.66016232, 0.66016185
which reveals that using only first $10^5$ primes, which is all primes upto $1299709$, is sufficient to get a result with error of the order $10^-6$.
For the record: Mathematica fails to do the calculation analytically:
Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, Infinity]
$prod _i=2^infty fracleft(p_i-2right) p_ileft(p_i-1right)^2$
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The problem with your code is that N[Product[...]]
calls NProduct[...]
which turns integers into reals before Prime
can evaluate, causing the error: See Details and Options section of documentation of NProduct
.
For your calculation, you actually do not need these. It looks like the result converges around $0.660162$:
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k]], k, 5]
0.665138, 0.66033, 0.66017, 0.660162, 0.660162
Since the product is monotonic and you are using N
at the end, it is not necessary to carry out whole multiplication all the way upto infinity: Depending on the interested accuracy, you can go to higher orders as well. For example,
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k], 8], k, 5]
yields
0.66513840, 0.66033029, 0.66017020, 0.66016232, 0.66016185
which reveals that using only first $10^5$ primes, which is all primes upto $1299709$, is sufficient to get a result with error of the order $10^-6$.
For the record: Mathematica fails to do the calculation analytically:
Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, Infinity]
$prod _i=2^infty fracleft(p_i-2right) p_ileft(p_i-1right)^2$
The problem with your code is that N[Product[...]]
calls NProduct[...]
which turns integers into reals before Prime
can evaluate, causing the error: See Details and Options section of documentation of NProduct
.
For your calculation, you actually do not need these. It looks like the result converges around $0.660162$:
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k]], k, 5]
0.665138, 0.66033, 0.66017, 0.660162, 0.660162
Since the product is monotonic and you are using N
at the end, it is not necessary to carry out whole multiplication all the way upto infinity: Depending on the interested accuracy, you can go to higher orders as well. For example,
Table[N[Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, 10^k], 8], k, 5]
yields
0.66513840, 0.66033029, 0.66017020, 0.66016232, 0.66016185
which reveals that using only first $10^5$ primes, which is all primes upto $1299709$, is sufficient to get a result with error of the order $10^-6$.
For the record: Mathematica fails to do the calculation analytically:
Product[(Prime[i] (Prime[i] - 2))/(Prime[i] - 1)^2, i, 2, Infinity]
$prod _i=2^infty fracleft(p_i-2right) p_ileft(p_i-1right)^2$
answered 1 hour ago
Soner
62839
62839
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
add a comment |Â
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
Thank you. So if I want an error of maximum $10^-x$, I only need to use the first $10^x-1$ primes?
– PierreTheFermented
25 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
@PierreTheFermented No, not necessarily. I was just trying to say that you do not need all primes and depending on the accuracy needed, different number of primes will be sufficient. I am not sure if there is an analytical way to predict that number though. In any case, using more than first 10^6 primes will probably be fairly slow.
– Soner
20 mins ago
add a comment |Â
up vote
3
down vote
As not all primes are known, Mathematica will have a problem to compute the constant exactly. But the sequence appears to converge quickly to some number $0.660162...$. (Soner posted that already.)
So let me give a way to compute the sequence of partial products in a more efficient way.
n = 10000;
seq = FoldList[#1 #2 (#2 - 2)/(#2 - 1)^2 &, 1, Prime[Range[2, n]]];
N[seq[[-1]]]
ListLinePlot[N[seq], ConstantArray[N[seq[[-1]]], Length[seq]],
PlotRange -> 1/2, 1,
PlotStyle -> Thick, Dashed
]
add a comment |Â
up vote
3
down vote
As not all primes are known, Mathematica will have a problem to compute the constant exactly. But the sequence appears to converge quickly to some number $0.660162...$. (Soner posted that already.)
So let me give a way to compute the sequence of partial products in a more efficient way.
n = 10000;
seq = FoldList[#1 #2 (#2 - 2)/(#2 - 1)^2 &, 1, Prime[Range[2, n]]];
N[seq[[-1]]]
ListLinePlot[N[seq], ConstantArray[N[seq[[-1]]], Length[seq]],
PlotRange -> 1/2, 1,
PlotStyle -> Thick, Dashed
]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
As not all primes are known, Mathematica will have a problem to compute the constant exactly. But the sequence appears to converge quickly to some number $0.660162...$. (Soner posted that already.)
So let me give a way to compute the sequence of partial products in a more efficient way.
n = 10000;
seq = FoldList[#1 #2 (#2 - 2)/(#2 - 1)^2 &, 1, Prime[Range[2, n]]];
N[seq[[-1]]]
ListLinePlot[N[seq], ConstantArray[N[seq[[-1]]], Length[seq]],
PlotRange -> 1/2, 1,
PlotStyle -> Thick, Dashed
]
As not all primes are known, Mathematica will have a problem to compute the constant exactly. But the sequence appears to converge quickly to some number $0.660162...$. (Soner posted that already.)
So let me give a way to compute the sequence of partial products in a more efficient way.
n = 10000;
seq = FoldList[#1 #2 (#2 - 2)/(#2 - 1)^2 &, 1, Prime[Range[2, n]]];
N[seq[[-1]]]
ListLinePlot[N[seq], ConstantArray[N[seq[[-1]]], Length[seq]],
PlotRange -> 1/2, 1,
PlotStyle -> Thick, Dashed
]
answered 1 hour ago


Henrik Schumacher
42.5k261125
42.5k261125
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184324%2fcomputing-the-twin-prime-constant-with-mathematica%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
It's just that some of the algorithms used internally by
NProduct
sometimes try to evaluate terms for inexact arguments, so a more clever approach is necessary.– J. M. is computer-less♦
2 hours ago
1
I didn't get this message. However I suggest not using
Infinity
insideProduct
, as there will possibly be no results. Try a big number instead, like 100000– t-smart
1 hour ago
1
This evaluates fine if you simply use a really big number, like 10^6
– Carl Lange
1 hour ago
3
See oeis.org/A005597 for better methods, how to compute this constant.
– Vaclav Kotesovec
1 hour ago