From: Boris Borcic on
Ignacio Mondino wrote:
> On Tue, Jun 15, 2010 at 8:49 AM, superpollo<utente(a)esempio.net> 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)
>>
>> my solution:
>>
>>>>> s = 0
>>>>> for i in range(1, 2011):
>> ... s += i**2
>> ... if not (i+1)%5:
>> ... s -= 2*i**2
>> ... if not i%5:
>> ... s -= 2*i**2
>> ...
>>>>> print s
>> 536926141
>>>>>
>>
>> bye
>
> I think This one is pretty, clean, using the standard library.
> pretty pythonic.
>
> def sign_and_sqr(n):
> """ return a numbers square a change the sign accordingly
> if n % 5 == 0 or (n + 1) % 5 == 0:

imho this fails DRY too much to be wholly pythonic,
write rather

if n%5 in (0,4) :

> return (n ** 2) * -1
> else:
> return n ** 2
>
> result = sum([sign_and_sqr(x) for x in range(0,2011)])
> print result
>
> ok, the function has two exits, but im lazy at the moment :)
>
>


From: Andre Alexander Bell on
On 06/16/2010 12:47 PM, Lie Ryan wrote:
> Probably bending the rules a little bit:
>
>>>> sum(x**2 - 8*x - 20 for x in range(1, 2010, 5))
> 536926141

Bending them even further, the sum of the squares from 1 to N is given by

(1) N*(N+1)*(2*N+1)/6.

The given problem can be divided into five sums of every fifth square
starting from 1,2,3,4,5 respectively. This is given by

(2) S(a,k) = sum_{i=1}^k (5*i-4+a)^2

for a in range(5) and we are finally interested in

(3) S(k) = S(0,k) + S(1,k) + S(2,k) - S(3,k) - S(4,k)

Substituting (2) and (1) in (3) gives (in python code)

>>> S = lambda k: (50*k**3 - 165*k**2 - 47*k) / 6
>>> S(2010/5)
536926141

However, this only works for full cycles of (1,1,1,-1,-1) and you would
have to add/subtract the modulus parts yourself. (e.g. if you are
interested in your sum from 1..2011...

Cheers


Andre
From: Stefan Behnel on
Andre Alexander Bell, 18.06.2010 11:23:
> On 06/16/2010 12:47 PM, Lie Ryan wrote:
>> Probably bending the rules a little bit:
>>
>>>>> sum(x**2 - 8*x - 20 for x in range(1, 2010, 5))
>> 536926141
>
> Bending them even further, the sum of the squares from 1 to N is given by
>
> (1) N*(N+1)*(2*N+1)/6.
>
> The given problem can be divided into five sums of every fifth square
> starting from 1,2,3,4,5 respectively. This is given by
>
> (2) S(a,k) = sum_{i=1}^k (5*i-4+a)^2
>
> for a in range(5) and we are finally interested in
>
> (3) S(k) = S(0,k) + S(1,k) + S(2,k) - S(3,k) - S(4,k)
>
> Substituting (2) and (1) in (3) gives (in python code)
>
>>>> S = lambda k: (50*k**3 - 165*k**2 - 47*k) / 6
>>>> S(2010/5)
> 536926141
>
> However, this only works for full cycles of (1,1,1,-1,-1) and you would
> have to add/subtract the modulus parts yourself. (e.g. if you are
> interested in your sum from 1..2011...

The thing is, if you can't do the math in the time that your processor
needs to run the brute force loop, it's often not worth doing the math at all.

Stefan

From: Peter Otten on
Stefan Behnel wrote:

> Andre Alexander Bell, 18.06.2010 11:23:
>> On 06/16/2010 12:47 PM, Lie Ryan wrote:
>>> Probably bending the rules a little bit:
>>>
>>>>>> sum(x**2 - 8*x - 20 for x in range(1, 2010, 5))
>>> 536926141
>>
>> Bending them even further, the sum of the squares from 1 to N is given by
>>
>> (1) N*(N+1)*(2*N+1)/6.
>>
>> The given problem can be divided into five sums of every fifth square
>> starting from 1,2,3,4,5 respectively. This is given by
>>
>> (2) S(a,k) = sum_{i=1}^k (5*i-4+a)^2
>>
>> for a in range(5) and we are finally interested in
>>
>> (3) S(k) = S(0,k) + S(1,k) + S(2,k) - S(3,k) - S(4,k)
>>
>> Substituting (2) and (1) in (3) gives (in python code)
>>
>>>>> S = lambda k: (50*k**3 - 165*k**2 - 47*k) / 6
>>>>> S(2010/5)
>> 536926141
>>
>> However, this only works for full cycles of (1,1,1,-1,-1) and you would
>> have to add/subtract the modulus parts yourself. (e.g. if you are
>> interested in your sum from 1..2011...
>
> The thing is, if you can't do the math in the time that your processor
> needs to run the brute force loop, it's often not worth doing the math at
> all.

By that standard using Cython for the problem doesn't pay either;)

Peter
From: Stefan Behnel on
Peter Otten, 18.06.2010 12:14:
> Stefan Behnel wrote:
>
>> Andre Alexander Bell, 18.06.2010 11:23:
>>> On 06/16/2010 12:47 PM, Lie Ryan wrote:
>>>> Probably bending the rules a little bit:
>>>>
>>>>>>> sum(x**2 - 8*x - 20 for x in range(1, 2010, 5))
>>>> 536926141
>>>
>>> Bending them even further, the sum of the squares from 1 to N is given by
>>>
>>> (1) N*(N+1)*(2*N+1)/6.
>>>
>>> The given problem can be divided into five sums of every fifth square
>>> starting from 1,2,3,4,5 respectively. This is given by
>>>
>>> (2) S(a,k) = sum_{i=1}^k (5*i-4+a)^2
>>>
>>> for a in range(5) and we are finally interested in
>>>
>>> (3) S(k) = S(0,k) + S(1,k) + S(2,k) - S(3,k) - S(4,k)
>>>
>>> Substituting (2) and (1) in (3) gives (in python code)
>>>
>>>>>> S = lambda k: (50*k**3 - 165*k**2 - 47*k) / 6
>>>>>> S(2010/5)
>>> 536926141
>>>
>>> However, this only works for full cycles of (1,1,1,-1,-1) and you would
>>> have to add/subtract the modulus parts yourself. (e.g. if you are
>>> interested in your sum from 1..2011...
>>
>> The thing is, if you can't do the math in the time that your processor
>> needs to run the brute force loop, it's often not worth doing the math at
>> all.
>
> By that standard using Cython for the problem doesn't pay either;)

True. Running the C compiler certainly takes a lot longer than starting up
Python and running the loop.

Stefan