MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Summations with Primes

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40011] Re: [mg39932] Summations with Primes
  • From: Helge Andersson <helge at envic.chalmers.se>
  • Date: Sun, 16 Mar 2003 02:20:15 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Anthony,

here is one simple way which is quit fast.


p[i_]:=Prime[i];
imin=3;
imax=40000;
Plus@@Table[p[i],{i,imin,imax}]//Timing

Out[21]=
{0.571 Second,9160579434}

Then, if you like to have an extra function g that takes p just wrap it 
over p[i] such as ,
In[19]:=
g[x_]:=N[1/x];
Plus@@Table[g[p[i]],{i,imin,imax}]//Timing

Out[20]=
{1.062 Second,1.99946}

A more comprehensive way to write the code can be done using & and # which 
is almost as fast as the previous example.

In[54]:=
Plus@@(g@p@#&/@Range[imin,imax])//Timing

Out[54]=
{1.072 Second,1.99946}

/Helge
Chalmers
Sweden
At 02:29 2003-03-12 -0500, you wrote:

>Can you tell me if, when calculating a summation of f(i) where i goes from
>imin to imax, Mathematica can be forced to only use prime values for i?
>
>Thank you for your time
>
>Anthony Felder



  • Prev by Date: Functions with multiple groups of arguments? [David Park?]
  • Next by Date: How to handle powers in c-code generation?
  • Previous by thread: RE: Summations with Primes
  • Next by thread: Re: Summations with Primes