[Tutor] Loop comparison
Alan Gauld
alan.gauld at btinternet.com
Sat Apr 17 01:40:39 CEST 2010
"Steven D'Aprano" <steve at pearwood.info> wrote
>> (We can of course do some fancy math to speed this particular
>> sum up since the result for any power of ten has a common pattern,
>> but I wouldn't expect the compiler optimiser to be that clever)
>
> No fancy maths needed,
> The sum of 1,2,3,4,...,N is given by a simple formula: 1/2*N*(N+1).
Depends how you define fancy... I count that as pretty fancy maths
for a compiler optimiser to recognise! :-)
> anecdote about the great mathematician Carl Gauss
I like it! I hadn't come across that one before.
Alan G.
More information about the Tutor
mailing list