Rust Implementation of Problem 2
View source code here on GitHub!
- pub fn problems::p0002::p0002() -> utils::Answer
1/*
2Project Euler Problem 2
3
4This is a port of the optimized version found in python. For a proof of why this
5works, see that implementation
6
7Problem:
8
9Each new term in the Fibonacci sequence is generated by adding the previous two
10terms. By starting with 1 and 2, the first 10 terms will be:
11
121, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
13
14By considering the terms in the Fibonacci sequence whose values do not exceed
15four million, find the sum of the even-valued terms.
16*/
17use crate::include::fibonacci::fib_by_3;
18use crate::include::utils::Answer;
19
20pub fn p0002() -> Answer {
21 return Answer::Int(fib_by_3::<u64>().take_while(|x| *x < 4000000).sum::<u64>().into());
22}