Files
test/ProjectEuler/p002_EvenFibNum.cpp

47 lines
764 B
C++

/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
*/
#include <iostream>
/*
Even Fibonacci numbers
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.
Solution:
4613732
*/
int main ( void )
{
int x = 1;
int y = 2;
int z = x+y;
int s = 0;
while ( z < 4000000 )
{
s+= z;
x = y;
y = z;
z = x+y;
}
if ( y%2 )
{
s -= y;
if ( x%2 )
s -= x;
}
s /= 2;
s += 2;
std::cout << s << std::endl;
return 0;
}