Re: Re: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
- To: mathgroup at smc.vnet.net
- Subject: [mg88176] Re: [mg88113] Re: [mg88090] Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
- From: DrMajorBob <drmajorbob at att.net>
- Date: Sun, 27 Apr 2008 04:58:49 -0400 (EDT)
- References: <200804240957.FAA28500@smc.vnet.net> <4714471.1209200633216.JavaMail.root@m08>
- Reply-to: drmajorbob at longhorns.com
Your curious result was
Clear[n, result]
result = Sum[i (i + 1)/2, {i, 1, 10^7}]
166666716666670000000
In general, the sum is
Clear[k, result, n]
result = Sum[i (i + 1)/2, {i, 1, n}]
1/6 n (1 + n) (2 + n)
If n is the kth power of 10, the factor n causes "result" to have k
trailing zeroes, and the factor (n+1) causes the repetition you noticed,
but it also requires (n+2)/6 to be an integer, which occurs when
Mod[10^k+2,6]==0.
But, modulo 6, we see that 10 is congruent to 4, and 4 is idempotent:
Mod[10, 6]
4
Mod[4^2, 6]
4
Union@Table[Mod[10^k, 6], {k, 100}]
{4}
As a consequence, the pattern you noticed will always appear when n is a
power of 10.
Table[Mod[10^k, 6], {k, 100}]
{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, \
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, \
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, \
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}
Oops! I almost forgot the final requirement, that (n+2)/6 < n. But that's
pretty obvious.
Bobby
On Fri, 25 Apr 2008 04:28:10 -0500, Syd Geraghty <sydgeraghty at mac.com>
wrote:
> 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
> free Mathematica License back then?
>
> 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*)
>
>
>
--
DrMajorBob at longhorns.com
- References:
- Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
- From: "W_Craig Carter" <ccarter@mit.edu>
- Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)