MathGroup Archive 2007

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

Search the Archive

Re: Re: fastest way to add up a billion numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73973] Re: [mg73907] Re: fastest way to add up a billion numbers
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sat, 3 Mar 2007 23:53:18 -0500 (EST)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <es92b2$3oj$1@smc.vnet.net> <200703030555.AAA02643@smc.vnet.net>
  • Reply-to: murray at math.umass.edu

I wonder what the timing for a sum of the form Sum[n,{n,nstart,nend}] 
really indicates.  I suspect that Mathematica is not actually summing 
but rather just uses Gauss' well-known closed-form formula for such a 
sum of consecutive positive integers.

dimitris wrote:
> There is recent post of mine asking something relevant here.
> 
> You can find also a relevant thread here:
> 
> http://forums.wolfram.com/mathgroup/archive/2005/May/msg00600.html
> 
> One billion numbers?
> Here is what I get to my PC
> 
> In[3]:=
> Range[10^9]
>>From In[3]:=
> No more memory available.
> Mathematica kernel has shut down.
> Try quitting other applications and then retry.
> 
> In[1]:=
> Table[i, {i, 1, 10^9}];
> 
>>From In[1]:=
> No more memory available.
> Mathematica kernel has shut down.
> Try quitting other applications and then retry.
> 
> Anyway...
> 
> lst = Range[10^6];
> 
> Timing[Plus @@ lst]
> {0.34299999999999997*Second, 500000500000}
> 
> Timing[Total[lst]]
> {0.2659999999999999*Second, 500000500000}
> 
> Timing[Fold[Plus, 0, lst]]
> {0.703*Second, 500000500000}
> 
> Timing[lst /. {x_, y___} -> x + y]
> {0.45300000000000007*Second, 500000500000}
> 
> Timing[Tr[lst]]
> {0.3899999999999997*Second, 500000500000}
> 
> Timing[Sum[lst[[i]], {i, 1, 10^6}]]
> {1.4379999999999997*Second, 500000500000}
> 
> 
> But for your case I should simply suggest
> 
> Timing[Sum[i,{i,1,10^9}]]
> {0.047 Second,500000000500000000}
> 
> 
> Regards
> Dimitris
> 
> 
> 
> 
> =CF/=C7 Raj =DD=E3=F1=E1=F8=E5:
>> hi!
>>
>> 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]
>>
>> Thanks,
>>
>> Raj
> 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: Is this the best way to Solve the Katsura 6 problem
  • Next by Date: Re: ReadList and columns
  • Previous by thread: Re: fastest way to add up a billion numbers
  • Next by thread: Re: Re: Re: fastest way to add up a billion numbers