pythonize this!

Stefan Behnel stefan_ml at behnel.de
Wed Jun 16 07:24:49 EDT 2010


Jussi Piitulainen, 16.06.2010 13:10:
> Lie Ryan writes:
>
>> On 06/15/10 21:49, superpollo wrote:
>>> goal (from e.c.m.): evaluate
>>> 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each
>>> three consecutive + must be followed by two - (^ meaning ** in
>>> this context)
> [...]
>> Probably bending the rules a little bit:
>>
>>>>> sum(x**2 - 8*x - 20 for x in range(1, 2010, 5))
>> 536926141
>>
>> another variation:
>>
>>>>> sum((x - 10) * (x + 2) for x in range(1, 2010, 5))
>> 536926141
>
> Gleefully bending the rules all the way:
>
>>>> 536926141
> 536926141

Timeit:

100000000 loops, best of 3: 0.0148 usec per loop

Certainly the fastest solution so far.

Stefan




More information about the Python-list mailing list