Re: Sum
- To: mathgroup at smc.vnet.net
- Subject: [mg30155] Re: Sum
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 1 Aug 2001 02:19:10 -0400 (EDT)
- References: <9k2doa$2fn$1@smc.vnet.net> <9k5r2m$hbl$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Another, faster, way (I'm rather fond od the dot product for this sort of thing): S2[n_] := (#.Log /@ FoldList[Plus, First[#], Rest[#]]) &[Reverse[Array[Prime, n]]] Compare S[n_] := (*Orestis Vantzos*) Module[{P = Array[Prime, n], s}, s[0] = 0; s[1] = First@P; s[i_] := (s[i] = s[i - 1] + P[[i]]); Log@(-Table[s[i], {i, 0, n - 1}] + s[n]).P ] (r1=S[5000]);//Timing {5.88 Second,Null} (r2=S2[5000]);//Timing {0.6 Second,Null} Check r1===r2 True S3[n_Integer?Positive] := (*Bob Hanlon*) Sum[P[k]*Log[Sum[P[m], {m, k, n}]], {k, 1, n}]; S3[500];//Timing (*500 not 5000*) {6.15 Second,Null} -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Orestis Vantzos" <atelesforos at hotmail.com> wrote in message news:9k5r2m$hbl$1 at smc.vnet.net... > > S[n_] := Module[{P = Array[Prime, n], s}, > s[0] = 0; > s[1] = First@P; > s[i_] := (s[i] = s[i - 1] + P[[i]]); > Log@(-Table[s[i], {i, 0, n - 1}] + s[n]).P] > > ..does the trick ;-) > > In[10]:= > Timing[S[5000];] > > Out[10]= > {0.99 Second, Null} > > Orestis > PS. This is an application of "dynamic programming" in Mathematica. > > > > "marc jeanno" <ts at tsts.com> wrote in message > news:9k2doa$2fn$1 at smc.vnet.net... > > Dear Sirs, > > > > let n be an integer >=1. Let us consider the following sums: > > > > s(1)= P[1]Log[P[1]] > > s(2)= P[2]Log[P[2]]+P[1]log[P[2]+P[1]] > > s(3)= P[3]Log[P[3]]+P[2]Log[P[3]+P[2]]+P[1]Log[P[3]+P[2]+P[1]] > > . > > . > > . > > s(n)= P[n]Log[P[n]]+...+P[1]Log[P[n]+...+P[1]] . > > > > > > P[k] is the expression Prime[k]. > > > > I used a method to evaluate the expression in function of n (n must be > > assigned a priori). But my PC was frozen during the > > operation(zzzzzz...zzzzz...) for large n values. > > How could I evaluate s(n) in a very very fast way? > > Do I have to try with 2 Fold lines? > > I'd like to know Your opinions. > > Thanks. > > > > > > > > > > > > > > > > >