10
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Nasser Jun 14 at 4:49
  • 2
    $\begingroup$ Don'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]}]. $\endgroup$ – JimB Jun 14 at 4:56
  • $\begingroup$ @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? $\endgroup$ – Nasser Jun 14 at 5:04
  • 1
    $\begingroup$ Per 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. $\endgroup$ – b3m2a1 Jun 14 at 6:13
  • 1
    $\begingroup$ Please don't use the bugs tag when posting new questions. See the tag description for why. $\endgroup$ – Szabolcs Jun 14 at 6:33
18
$\begingroup$

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}],
  {{PrimeQ[p],
    False},
   If[False, (p^2 + 1)/(p^2 - 1), 1],
   1},
  Product[1, {p, 2, Infinity}],
  1}
*)

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

$\endgroup$
  • $\begingroup$ Oh, then it seems that this is definitely a bug in mathematica. $\endgroup$ – FFjet Jun 14 at 5:15
  • 4
    $\begingroup$ @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. $\endgroup$ – Michael E2 Jun 14 at 5:39
  • 3
    $\begingroup$ @MichaelE2 Agreed. The documentation for Sum explicitly lists this problem in the "Possible Issues" section. $\endgroup$ – Sjoerd Smit Jun 14 at 8:16
  • $\begingroup$ ok, i see, thx :D $\endgroup$ – FFjet Jun 15 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.