Re: local variables - Module, For loop

*To*: mathgroup at smc.vnet.net*Subject*: [mg113129] Re: local variables - Module, For loop*From*: Sebastian Schmitt <sschmitt at physi.uni-heidelberg.de>*Date*: Wed, 13 Oct 2010 23:28:24 -0400 (EDT)*References*: <i9279n$3i3$1@smc.vnet.net> <201010131640.MAA28345@smc.vnet.net>

Hi Albert! Albert Retey wrote: > Am 12.10.2010 19:52, schrieb Sebastian Schmitt: >> Bill Rowe wrote: >>> In[5]:= Timing[Sum[k, {k, n}]] >>> >>> Out[5]= {0.393106,500000500000} >>> >>> In[6]:= Timing[Sum[k, {k, m}] /. m -> n] >>> >>> Out[6]= {0.08371,500000500000} >>> >>> Each code snippet does exactly the same thing and produces >>> exactly the same sum. But as you can see, using For is the >>> slowest method. >> >> Dear Bill! >> >> Thank you very much for your detailed answer! >> >> Interesting that the last version is so much faster. Is it only due to >> numeric vs. algebraic? > > the first is adding a lot of numbers, the second is evaluating 1/2 > m(m+1). It's hard to believe, but there was a 9 year old boy named Carl > Friedrich who made the experience that the second is faster some 200 > years ago -- without access to Mathematica!!! Attaboy! ;) I thought Mathematica is smart enough to see that it could also use the algebraic version even if I supply n in the call to Sum. But it is probably on purpose this way. Cheers, Sebastian

**References**:**Re: local variables - Module, For loop***From:*Albert Retey <awnl@gmx-topmail.de>