pythonize this!

Shashwat Anand anand.shashwat at gmail.com
Tue Jun 15 09:22:10 EDT 2010


>>> sum(i*i*(-1)**((i % 5) / 4 + (i + 4) % 5 / 4)  for i in range(1,2011))
536926141


On Tue, Jun 15, 2010 at 6:25 PM, superpollo <utente at esempio.net> wrote:

> superpollo ha scritto:
>
>  Peter Otten ha scritto:
>>
>>> 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)
>>>>
>>>
>>>  from itertools import cycle, izip
>>>>>> sum(sign*i*i for sign, i in izip(cycle([1]*3+[-1]*2), range(1, 2011)))
>>>>>>
>>>>> 536926141
>>>
>>
>> don't understand it bit i like this a lot!
>>
>                      ^^^
>
> *but*
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100615/ec0d3b27/attachment-0001.html>


More information about the Python-list mailing list