Day 2 Problem 2. Today has been a semi productive day and I am excited to get back to other reading as soon as Im done with this. The problem seems simple enough, and at the face of it, I can tell I can do a brute force and a recursion. Hmm. Although I am tempted to move straight to making a recursion code, I think Ill make the brute force one just for the heck of it. euler2.cpp

#include<iostream> using namespace std; int fiblast(int a[]); int main() { int s=2,k=0,a[3]={1,2,0}; while(k<4000000){ if(k%2==0) s+=k; k=fiblast(a); } cout<<s<<endl; return 0; } int fiblast(int a[]){ a[2]=a[0]+a[1]; a[0]=a[1]; a[1]=a[2]; return a[1]; }

Done. Now to think if a recursive solution can exist. Hmm. First, I cannot help but think how deplorably inefficient my earlier program is. Why the hell am I passing bulky arrays to a function which is going to be called many many times? I feel I can do a better job with three variables in the main function itself. Ok. Try 2 at brute force, basically the same algorithm but a better implementation.

#include<iostream> using namespace std; int main() { int a1=1,a2=2,a3=0; while(a2<3009){ if (a2 % 2==0)a3+=a2; a2=a1+a2; a1=a2-a1; if(a2%2==0)cout<<"a2="<<a2<<endl; }return 0; }

Perfect. Well… in the sense of improvement, atleast. Now. Time to find a better solution. Hmm. After making some of the sequence, an obvious pattern is emerging. Starting from ever number is even. This is so because the sum of odd and even is odd, and the sum of two odds is even. First let me just write the program, I will analyze the recursion itself later to see if there is a closed form, though I should expect one as it is a linear recursion

with and

#include<iostream> using namespace std; int fib(int z); int main(){ int s=0,k=0,l=1; for(int i=2;k<4000000;i+=3) {s+=k;k=fib(i);} cout<<s<<endl; return 0;} int fib(int i){ if(i==1) return 1; if(i==2) return 2; return fib(i-1)+fib(i-2); }

Well. That’s done.Though I am not sure yet if this is a better solution than the earlier one. I remember a recursive function creates a copy of itself and stores it in memory each time its called. Hmm. Ill make the comparision after I’m done with the closed form as it has begun to bug me and this has taken more time than I expected.

Note to self: Start Euler problems early morning before starting work.

The most efficient code for this problem would be to make the computer do just one computation by figuring out the closed form yourself.

im looking for a closed form for

Where is the k’th Fibbonaci number. , being the base cases.This sum should wait. Take the general sum S_n = T_0+ &T_1+ T_2 +\cdots + T_n

S_{n-1} =\qquad &T_0 + T_1+\cdots +T_{n-1}

On adding the above two equations, we get the recursion for the sums:

S_n+S_{n-1} &= T_0 + (T_2+T_3+\cdots + T_{n+1}) = S_{n+1} – T_1

S_n+S_{n-1} &= S_{n+1} – 1

So this is the recursion relation that needs to reduced to a close form. This is, as the Fibonaci terms, a second order recurrence. How should I have seen that earlier? Hmm. Ahh, of course, uptil now there is no reference to any particular sequence, so I can deduce that the sum of terms defined by a second order recurrence will have second order recursive defintions themselves.

My first attack would be to use generating functions:

In some domain x where the power series is convergent.

S(x) &= S_0+S_1 x+\sum_{n=2}^\infty S_{n+1} x^n – \sum_{n=2}^\infty S_{n-1} x^n -\sum_{n=2}^\infty x^n

&= S_0+S_1 x+\frac{1}{x}\sum_{n=2}^\infty S_{n+1} x^{n+1} – x\sum_{n=2}^\infty S_{n-1} x^{n-1} -\sum_{n=2}^\infty x^n

&= S_0+S_1 x+\frac{1}{x}(S(x) – S_0 – S_1 x -S_2 x^2) – x(S(x) – S_0 ) -\frac{x^2}{1-x}

Using the values , and the above reduces to

The amount of cancelling that took place to reduce to the simpler form from the formidable looking (9) suggests there could have been a simpler way. From the generating function of the fibbonaci sequence: G(x) &=\sum_{n=0}^\infty T_n x^n

&=\sum_{n=0}^\infty (S_n-S_{n-1}) x^n

&=S(x)-xS(x)

So, I could have got the generating function from direcly computing . Well, I’ve calculated so I verify that

Now time to finally get the closed form from this generating function.

S(x) &=-\frac{1}{1-x}+\frac{x+2}{1-x-x^2}

&=-\frac{1}{1-x}+\frac{(x+2)}{\alpha-\beta}\left(\frac{1}{x-\alpha}-\frac{1}{x-\beta}\right) Now I think convergence issue has to be addressed, because the expansion of would depend on the value of . If has a magnitude less than the max value of then the power series would be in inverse powers of x. Ah, that is just silly of me! is just a spectator in my scheme. I already know that has a power series expansion in powers of x not in inverse powers. So domain is the excluded interval

But I just realize that all this is useless overworking in the face of the obvious solution staring at me now. The roots of the generating function for the fibbonaci sequence are and and the from this, the closed form for the fibbonacis I get is

I just need the sequence,

So this finally gives me

Now I need to find the to truncate this summation at the limit, in this case 4 million. To take the general case where the limit is specified to be the solution to

Finally, I’m done with the analysis. I plug in Eqn (23) into Eqn (22) and then translate the following formula to code.