pythonize this!

superpollo utente at esempio.net
Tue Jun 15 08:23:11 EDT 2010


Ulrich Eckhardt ha scritto:
> superpollo wrote:
>> ...     s += i**2
>> ...     if not (i+1)%5:
>> ...         s -= 2*i**2
>> ...     if not i%5:
>> ...         s -= 2*i**2
> 
> if not (i % 5) in [1, 2]:
>     s += i**2
> else:
>     s -= i**2
> 
> Untested code.

does not work:

 >>> s = 0
 >>> for i in range(1, 2011):
...     if not (i % 5) in [1, 2]:
...         s += i**2
...     else:
...         s -= i**2
...
 >>> print s
546627205
 >>>

but this does:

 >>> s = 0
 >>> for i in range(1, 2011):
...     if i % 5 in [1, 2, 3]:
...         s += i**2
...     else:
...         s -= i**2
...
 >>> print s
536926141

bye



More information about the Python-list mailing list