top of page

Problem 02

This is a C++ solution to Project Euler problem 02 found at: https://projecteuler.net/problem=2

My Algorithm

For this problem, we need to find the sum of the even-valued numbers in the Fibbonacci sequence that do not exceed four million. To do this, we can first define two variables which hold the first two terms of the Fibbonacci sequence. Then, we can create a loop that runs until four million is reached. This is because the maximum number we are concerned with is four million. Inside of this loop we need to check if the current Fibbonacci number is even using modulus and add it to a running sum if it is. Then, we need to generate the next term in the sequence by setting the first Fibonacci variable equal to the second and setting the second Fibonacci variable equal to the prior two terms summed together. This will move us forward in generating the sequence without having to store all of the previously generated numbers. Finally, we can return the sum to get our answer of 4613732.

Full Solution on GitHub

© 2023 by Matthew Woodring

    bottom of page