Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg73907] Re: fastest way to add up a billion numbers
  • From: "dimitris" <dimmechan at yahoo.com>
  • Date: Sat, 3 Mar 2007 00:55:02 -0500 (EST)
  • References: <es92b2$3oj$1@smc.vnet.net>

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



  • Prev by Date: nested list creation with index skipping
  • Next by Date: Re: Bug with Limit?
  • Previous by thread: Re: fastest way to add up a billion numbers
  • Next by thread: Re: Re: fastest way to add up a billion numbers