How to calculate $\prod\limits_{p}\frac{p^2+1}{p^2-1}$?



When I input $$ \prod _{p=2}^{\infty } \text{If}\left[\text{PrimeQ}[p],\frac{p^2+1}{p^2-1},1\right] $$ in Mathematica 12.0, it gives out the value $1$.

The result is ridiculous, for that according to numerical evaluation we have

In fact, after some analysis we can find out the analytical result $\displaystyle \prod_p \frac{1+p^{-s}}{1-p^{-s}}=\frac{\zeta(s)^2}{\zeta(2s)}$, so that $ \displaystyle \prod_p \frac{1+p^{-2}}{1-p^{-2}}=\frac{\zeta(2)^2}{\zeta(4)}=\frac{\left (\frac{\pi^2}{6}\right )^2}{\frac{\pi^4}{90}}=\frac{5}{2}$

I am wondering that if it is my fault (e.g. wrong input method) or a bug in mathematica 12.0.


Posted 2019-06-14T04:28:22.270

Reputation: 225

it seems to have to do with the upper limit being $\infty$. Since doing something like Product[If[PrimeQ[p], (p^2 + 1)/(p^2 - 1), 1], {p, 2, 100000}] // N gives 2.5 which is the limit. ps. it would be better to post your Mathematica code also in plain text to make it easier to copy and user. Eliminates mistakes. – Nasser – 2019-06-14T04:49:44.480

2Don't know why what you wrote doesn't work but the following does work: Product[(Prime[n]^2 + 1)/(Prime[n]^2 - 1), {n, 1, \[Infinity]}]. – JimB – 2019-06-14T04:56:47.917

@JimB is it possible Mathematica knew about these special cases and just did a lookup for the result? Any other ones not in a lookup table, should not work if the upper limit is infinity, according to help? – Nasser – 2019-06-14T05:04:32.077

@Nasser Good inference. It does appear that the lower limit is recognized in whatever lookup is used as using {n, 3, \[Infinity]} also gives an appropriate answer. – JimB – 2019-06-14T05:11:40.483

1Per Michael E2's comment/answer I'm not sure this is a bug, so I removed the tag. We try to be very careful about having only bugs confirmed by WRI or the community when using it. – b3m2a1 – 2019-06-14T06:13:44.017

2Please don't use the [tag:bugs] tag when posting new questions. See the tag description for why. – Szabolcs – 2019-06-14T06:33:02.853



A Trace reveals the problem:

Product[If[PrimeQ[p], (p^2 + 1)/(p^2 - 1), 1], {p, 2, 
   Infinity}] // Trace
{Product[If[PrimeQ[p], (p^2 + 1)/(p^2 - 1), 1], {p, 2, Infinity}],
   If[False, (p^2 + 1)/(p^2 - 1), 1],
  Product[1, {p, 2, Infinity}],

The If[] statement is evaluated before the Product. In turn PrimeQ[p] is evaluated before p has any value, and it evaluates to False because the symbol p is not a prime integer. Hence the product being evaluated is the product of an infinite number of ones.

Update. The desired product can be computed with the following (just noticed that @JimB mentioned this in a comment around the time I was working on this answer):

Product[(Prime[p]^2 + 1)/(Prime[p]^2 - 1), {p, 1, Infinity}]
(*  5/2  *)

Update 2.

From the docs for Product:

If a product cannot be carried out explicitly by multiplying a finite number of terms, Product will attempt to find a symbolic result. In this case, f is first evaluated symbolically.

As @Sjoerd mentions in a comment, this problem with PrimeQ is explicitly pointed out in the documentation for Sum

Michael E2

Posted 2019-06-14T04:28:22.270

Reputation: 190 928

Oh, then it seems that this is definitely a bug in mathematica. – FFjet – 2019-06-14T05:15:20.690

4@FFjet I don't think it's a bug. Most functions ending in Q evaluate to True or False no matter whether the input is numeric or not. Sometimes it's irritating, but it is by design and therefore not a bug, imo. Product has to inspect its arguments to know what to do. When it evaluates the first one, it evaluates to 1 (because PrimeQ[p] is False). You can try If[Element[p, Primes], (p^2 + 1)/(p^2 - 1), 1], which does not evaluate until p has a numeric value. However, Product does not handle this form, but NProduct will produce a somewhat poor approximation. – Michael E2 – 2019-06-14T05:39:23.623

3@MichaelE2 Agreed. The documentation for Sum explicitly lists this problem in the "Possible Issues" section. – Sjoerd Smit – 2019-06-14T08:16:19.810

ok, i see, thx :D – FFjet – 2019-06-15T06:24:20.303