• Challenge 2 - Even Fibonacci Numbers

    Jamie Ashton3 days ago 0 comments

    '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.

    And here's the same implementation in Matlab

  • Challenge 1

    Jamie Ashton5 days ago 1 comment

    Challenge 1: 

    'If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23.

    Find the sum of all the multiples of 3 or 5 below 1000.'

    Simple solution in MATLAB:

    Simple solution in C:

    Both programs return the answer as 233,168.

    Conclusion: A very simple first challenge, to ease me into things.