0
$\begingroup$

I am trying to find out partial products going up in prime steps for Euler's product formula

$$\prod_{p} \frac{1}{1-p^{-s}}$$

Trying things like

s=10;
Product[1/(1-p^(-s)), {i, p, 100p, Prime[p]}]

I am clearly a little lost!

$\endgroup$
  • 1
    $\begingroup$ You could include the condition in the definition of your function, like : f[p_ /; PrimeQ[p], s_] := 1/(1 - p)^(-s) ; f[x_, s_] := 1. Then you can use Product as usual. $\endgroup$ – b.gates.you.know.what Jan 8 '14 at 11:59
  • $\begingroup$ I am having a little trouble following - how would I then use it with product? $\endgroup$ – martin Jan 8 '14 at 12:06
  • $\begingroup$ You have better suggestions, however mine was to just do Product[f[p,10], {p, 1, 100}]. $\endgroup$ – b.gates.you.know.what Jan 8 '14 at 12:09
  • $\begingroup$ Great - ok, thanks for letting me know :) $\endgroup$ – martin Jan 8 '14 at 12:10
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/25882/… $\endgroup$ – Sjoerd C. de Vries Jan 8 '14 at 12:50
2
$\begingroup$
P[s_,n_] := FoldList[#1/(1 - Prime[#2]^-s) &, 1, Range[n]]

where $s$ is the exponent in the formula, and $n$ is the number of partial products you wish to compute. So for instance, you could calculate the first 20 partial products for $s = 2$ by entering P[2,20]. If you want a numeric list instead of a symbolic result, then you can either use N[P[2,20]] or you can create another function, say,

P2[s_,n_] := FoldList[#1/(1 - Prime[#2]^-s) &, 1., Range[n]]

which will automatically use numeric values due to the decimal point after the 1.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Great - N[Last[p[2, 20]]] gives the result I am after! - Many thanks :) $\endgroup$ – martin Jan 8 '14 at 12:10
  • 1
    $\begingroup$ My code was based on the assumption that you needed the entire list of partial products. If you only needed the last value, then you would be much better off doing this: P[s_, n_] := Product[1/(1 - Prime[k]^-s), {k, 1, n}] $\endgroup$ – heropup Jan 8 '14 at 12:12
  • $\begingroup$ Great - thanks again :) $\endgroup$ – martin Jan 8 '14 at 12:13
2
$\begingroup$

Is this the syntax you are after?

s = 10;
m = 10;
Product[1/(1 - p^(-s)), {p, Prime /@ Range[m]}]

Response to comment

If you are interested in finding all primes smaller than some number n, one should use PrimePi. This allows you know in advance how many primes you will find, so that you do not have to conjure up some data structure that allows you keep adding elements. In other cases, where you cannot calculate "the size of the list" beforehand, AppendTo is still not nice and a linkedlist is a better alternative. Anyway here are some timings

n = 2*^5;
Prime /@ Range[n];
(res1 = (p = 1; lst = {}; 
     While[Prime[p] <= n, AppendTo[lst, Prime[p]]; p++]; lst)) // 
  Timing // First
(res2 = TakeWhile[Prime /@ Range[n], # <= n &]) // Timing // First
(res3 = Prime /@ Range[PrimePi[n]]) // Timing // First
res1 === res2 === res3

0.783796
0.405947
0.008712

The reason "res1" does bad because lst has to be copied n times. res2 does bad because way too many primes are "calculated". Actually Prime caches values, which is why I do Prime /@ Range[n] first, to make a fairish comparison.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Seems to be generating slightly different result than expected - am I using it incorrectly? $\endgroup$ – martin Jan 8 '14 at 12:12
  • 1
    $\begingroup$ @martin ah I see I copied your brackets around (1-p), those are not supposed to be there $\endgroup$ – Jacob Akkerboom Jan 8 '14 at 12:29
  • $\begingroup$ @Jacob Akkerboom Thank you for the detailed explanation. As much as I got you now, the only difference is in timing. No other problem arises, and on the scale of n=2^5 and below the timing makes no real difference. The difference only manifests itself during really long computation. Am I right? I would like to make it sure, since from your comment I had an impression, that there is something else (like some trap when using Append). $\endgroup$ – Alexei Boulbitch Jan 10 '14 at 8:30
  • $\begingroup$ @AlexeiBoulbitch you are mostly correct. Do note though that it also uses twice as much memory, as in the end the entire result minus one element has to be copied. But yeah for small manipulations like vars = Hold[a, b]; AppendTo[vars, Unevaluated@c] it is pretty harmless. Just a heads up :). $\endgroup$ – Jacob Akkerboom Jan 10 '14 at 11:13
1
$\begingroup$

Please note that the Tex formula you gave above differs from the one in the Mathematica code above. I guess that the one written as a Tex expression is correct. Though I like the heropup solution here, I will give another one, less elegant, but seeming me to be more transparent.

This function:

    listOfPrimes[n_Integer] := Block[{lst = {}, p = 1},
  While[Prime[p] < n, AppendTo[lst, Prime[p]]; p++];
  lst]

returns a list of primes which are smaller than n. For example:

    listOfPrimes[100]

(* {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, \
67, 71, 73, 79, 83, 89, 97} *)

Then this function:

product[s_Integer, n_Integer] :=
  Product[1/(1 - p^(-s)), {p, listOfPrimes[n]}];

returns the desired product. Indeed, let us check it for s=2, and n=10:

    product[2, 10]

(*  1225/768 *)

Let us calculate it another way around: The list of primes below 10 are:

    list = listOfPrimes[10]

(*  {2, 3, 5, 7}   *)

Let as Map your multiplicand on it:

    Map[1/(1 - #^-2) &, list]

(* {4/3, 9/8, 25/24, 49/48}  *)

Now let us calculate the product of these terms:

    % /. List -> Times

(* 1225/768 *)

We get the same result as above.

The product you wanted to get is then

    product[10, 100]

   (* 5967759019413103966712383822640397807425621247392071517391894091770913\
    7631541172889595623000607090746821932378768543960498126265333779392434\
    4179150805476651915980558014529664252194197981450243364217210779183600\
    1422507361633475003901129446863362595584099708682593971471674963197862\
    16397714230452347/\
    5961829532044241429590586174163153020831738244618927601996598058563559\
    5126956873083435666030479840027636509215232399948421584659255348957093\
    1712114227006564474060461795961292887260822274782304242071692628118352\
    7983211158413303809350926079289480162856392561736077488839386393558583\
    45000960000000000 *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Be wary of using Append(To) in a loop or even at all! From the comments I gather that the OP does not want primes smaller than some number, as this does not happen in heropup's answer. But if you want to select primes smaller than some number, I would suggest something like TakeWhile[Prime /@ Range[m], #<m&]? $\endgroup$ – Jacob Akkerboom Jan 8 '14 at 12:46
  • $\begingroup$ @Jacob Akkerboom Thank you for advice. Could you kindly comment, why is it like this and in what situations it may give rise to a problem? I never experienced any problem with Append/AppendTo and analogous so far, though I use them here and there. $\endgroup$ – Alexei Boulbitch Jan 9 '14 at 8:46
  • $\begingroup$ I updated my answer to respond to your comment. $\endgroup$ – Jacob Akkerboom Jan 9 '14 at 9:50

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