MathGroup Archive 2007

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

Search the Archive

Re: fastest way to add up a billion numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73950] Re: fastest way to add up a billion numbers
  • From: Bill Rowe <readnewsciv at sbcglobal.net>
  • Date: Sat, 3 Mar 2007 01:18:20 -0500 (EST)

On 3/2/07 at 6:41 AM, rajanikanth at gmail.com (Raj) wrote:

>Could somebody tell me what would be the fastest way to add up a
>billion numbers(from 1 to 10^9 i.e the first billion numbers) in
>Mathematica?

>Ofcourse the answer is n(n+1)/2, but is there any other way in
>Mathematica other than the following one:

>Total@@Range[10^9]

There are a great many ways. I suspect none is faster or more
efficient than

In[2]:=
Timing[Sum[n, {n, 1, m}] /. m -> 10^9]

Out[2]=
{0.11082 Second,500000000500000000}

Note for smaller lists of numbers it will be faster to actually
sum a list, i.e.,

In[1]:=
data=Range[10000];
Timing[Total@data]
Timing[Sum[n,{n,1,m}]/.m\[Rule]10000]

Out[2]=
{0.000297 Second,50005000}

Out[3]=
{0.11049 Second,50005000}
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: The Mathematica equivalent of Cons (as in Lisp)?
  • Next by Date: Re: The Mathematica equivalent of Cons (as in Lisp)?
  • Previous by thread: Re: fastest way to add up a billion numbers
  • Next by thread: Re: fastest way to add up a billion numbers