/*https://projecteuler.net/problem=2
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.
Analysis:
2 8 34 144
1 2 (1+2) (1+2*2) (1*2 + 2*3) (1*3+2*5) (1*5+2*8) (1*8+2*13) (13, 21) (21 34) (34,55)
x x x x
So,
each even number is represented as P=1*a + 2*b
next even number is P_next=1*p + 2*(2a+3b)
It's trivial to write program with above fomular
*/
#include <iostream>
int main()
{
unsigned int a=0, b=1, i = 2, sum = 0;
unsigned int nextA, nextB;
while (i <= 4000000)
{
sum += i;
nextB = 2*a + 3*b;
nextA = i;
i = 1*nextA + 2*nextB;
a = nextA;
b = nextB;
}
std::cout << "sum: " << sum << std::endl;
return sum;
}