This article is part of a series where I'll be diving head first into the Project Euler puzzles. I want to document the challenge of solving such a puzzle and how I got to the answer. I want to prefix this by stating that I can't cheat for any of these challenges; with that I mean I can't look up any other implementations online. After the implementation, I will validate the answer by using this document or a similar sheet.
In this article I'll be solving: Project Euler #2.
This article features only an answer, because I’ve started writing from problem 14.
fn fibonacci_even(max: i32) -> i32 {
let mut far = vec![1, 2];
let mut total = far[1];
loop {
let n = far.remove(0);
let m = far[0] + n;
far.push(m);
if m % 2 == 0 {
total += m
}
if m >= max {
break;
}
}
total
}
#[test]
fn even_fibonnaci_numbers_test() {
assert_eq!(fibonacci_even(8), 10);
assert_eq!(fibonacci_even(55), 44);
assert_eq!(fibonacci_even(4000000), 4613732);
}
The full solution is available on GitHub.