       Re: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)

• To: mathgroup at smc.vnet.net
• Subject: [mg88113] Re: [mg88090] Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
• From: Syd Geraghty <sydgeraghty at mac.com>
• Date: Fri, 25 Apr 2008 05:28:10 -0400 (EDT)
• References: <200804240957.FAA28500@smc.vnet.net>

```Craig,

Just a (tongue in cheek) reminder that Mathematica is a wonderful all
purpose environment in which to do Mathematics.

221 years ago the 10 year old Gauss would have solved the problem by
coding:-

n = 10000000;

Timing[total = n (n + 1)/2]

{0.000035, 50000005000000}

Which is a whole lot faster!

I realize that this is not  very helpful in deciding usage of Table vs
Do.

But the precocious Gauss also might have tried

Timing[total = Table[i (i + 1)/2, {i, 10000000}]]

{20.46425400000001, {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78,
91, <<9999974>>,
49999885000066, 49999895000055, 49999905000045, 49999915000036,
49999925000028, 49999935000021, 49999945000015, 49999955000010,
49999965000006, 49999975000003, 49999985000001, 49999995000000,
50000005000000}}

or better yet

Timing[Total[Table[i (i + 1)/2, {i, 10000000}]]]

{31.7433, 166666716666670000000}

Just to show off ...  :)

Cheers ... Syd

PS Where would we be now if Stephen Wolfram had provided Gauss with a

BTW What a surprising result (1666667)(1666667)(0000000)

I wonder if anyone has computed this result before and explained its
repetition of the first 7 digits?

Syd Geraghty B.Sc, M.Sc.

sydgeraghty at mac.com

My System

Mathematica 6.0.2.1 for Mac OS X x86 (64 - bit) (March 13, 2008)
MacOS X V 10.5.2
MacBook Pro 2.33 Ghz Intel Core 2 Duo  2GB RAM

On Apr 24, 2008, at 2:57 AM, W_Craig Carter wrote:

> This is problem with a known simple result, but it will serve: let's
> find the sum of the first 10000000 integers.
>
> (*let's use a do loop*)
> Timing[
> icount = 0;
> Do[icount = icount + i, {i, 1, 10000000, 1}];
> icount
> ]
>
> (*this returns {10.2826, 50000005000000} on my machine.*)
> (*10.28 being a measure of how long the computation took to run*)
>
> (*lets try a table*)
> Timing[
> Total[Table[i, {i, 1, 10000000, 1}]]
> ]
>
> (*This returns {3.25516, 50000005000000} on my machine*)

```

• Prev by Date: Re: Dynamic: feature?
• Next by Date: Re: Wolfram User Interface Research?
• Previous by thread: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
• Next by thread: Re: Re: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)