       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;
s = First@P;
s[i_] := (s[i] = s[i - 1] + P[[i]]);
Log@(-Table[s[i], {i, 0, n - 1}] + s[n]).P
]

(r1=S);//Timing

{5.88 Second,Null}

(r2=S2);//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;//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;
>     s = 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:=
> Timing[S;]
>
> Out=
> {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)= PLog[P]
> > s(2)= PLog[P]+Plog[P+P]
> > s(3)= PLog[P]+PLog[P+P]+PLog[P+P+P]
> > .
> > .
> > .
> > s(n)= P[n]Log[P[n]]+...+PLog[P[n]+...+P]  .
> >
> >
> > 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.
> >
> >
> >
> >
> >
> >
> >
>
>
>

```

• Prev by Date: Re: Solving linear equations with symbolic RHS
• Next by Date: RE: SquareFreeQ vs. MoebiusMu
• Previous by thread: Re: Sum
• Next by thread: Re: Sum