My skills with MATLAB and C are slightly rusty since using them at university last year. To re-familiarise myself with the syntax and with algorithms and programming in general, I've decided to go through the challenges listed on https://projecteuler.net/. I may find solutions in several languages but will mainly focus on MATLAB and C or C++, as these are the languages I find myself using most in my degree.
'Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1,2,3,5,8,13,21,34,55,89,…
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.'
I started in C, and created a simple program to generate the terms of the fibonacci sequence, then implemented an if statement to check if each value was even and then added it to a running total. I started for values less than 1000.
This is simple, and works for values less than 1000. Alternatively I could've created a function that calculates the next value and passed the previous values in as inputs. However the question is asking for the sum of even terms up to 4 million. Generating every term up to 4 million in this manner will be very computationally intensive.
Instead, you can notice that even valued terms appear every third term starting from term 2 ( this is because O + O = E, O + E = O etc). We can make a simple recurrence relation for the even terms in the fibonacci sequence.
With this simple program we find the answer to be 4,613,732.