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!
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.
N[Last[p[2, 20]]] gives the result I am after! - Many thanks :) $\endgroup$ P[s_, n_] := Product[1/(1 - Prime[k]^-s), {k, 1, n}] $\endgroup$ 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.
(1-p), those are not supposed to be there $\endgroup$ vars = Hold[a, b]; AppendTo[vars, Unevaluated@c] it is pretty harmless. Just a heads up :). $\endgroup$ 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 *) 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$
f[p_ /; PrimeQ[p], s_] := 1/(1 - p)^(-s);f[x_, s_] := 1. Then you can useProductas usual. $\endgroup$Product[f[p,10], {p, 1, 100}]. $\endgroup$