From: Mark Lawrence on 18 Jun 2010 09:32 On 18/06/2010 10:23, Andre Alexander Bell wrote: > 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 The good news is that this is easily the fastest piece of code that I've seen yet. The bad news is that first prize in the speed competition is a night out with me. :) Cheers. Mark Lawrence.
From: Steven D'Aprano on 18 Jun 2010 11:00 On Fri, 18 Jun 2010 14:32:30 +0100, Mark Lawrence wrote: > The good news is that this is easily the fastest piece of code that I've > seen yet. The bad news is that first prize in the speed competition is > a night out with me. I suppose second prize is two nights out with you? -- Steven
From: Mark Lawrence on 18 Jun 2010 11:32 On 18/06/2010 16:00, Steven D'Aprano wrote: > On Fri, 18 Jun 2010 14:32:30 +0100, Mark Lawrence wrote: > >> The good news is that this is easily the fastest piece of code that I've >> seen yet. The bad news is that first prize in the speed competition is >> a night out with me. > > I suppose second prize is two nights out with you? > > Stephen, you are absolutely correct. I never realised from your posting history just how astute you are. :) Kindest regards. Mark Lawrence.
From: Andre Alexander Bell on 18 Jun 2010 11:26 On 06/18/2010 03:32 PM, Mark Lawrence wrote: > The good news is that this is easily the fastest piece of code that I've > seen yet. The bad news is that first prize in the speed competition is > a night out with me. :) Well, that actually means that Stefan Behnel will run my solution through cython. As Steven D'Aprano already pointed out this then will make me receive the second price of two nights out? Cheers Andre
From: Mark Lawrence on 18 Jun 2010 11:53
On 18/06/2010 16:26, Andre Alexander Bell wrote: > On 06/18/2010 03:32 PM, Mark Lawrence wrote: >> The good news is that this is easily the fastest piece of code that I've >> seen yet. The bad news is that first prize in the speed competition is >> a night out with me. :) > > Well, that actually means that Stefan Behnel will run my solution > through cython. As Steven D'Aprano already pointed out this then will > make me receive the second price of two nights out? > > Cheers > > > Andre Andre, looks like a really bad day for you then, *TWO* nights out with me *AND* (looking at your email address) Germany loosing in the world cup. :( Kindest regards. Mark Lawrence. |