Euler02, C++
1

1/**************************************** 2 Even Fibonacci numbers 3 Problem 2 4 Each new term in the Fibonacci sequence is generated by adding the previous 5two terms. By starting with 1 and 2, the first 10 terms will be: 6 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 7 8 By considering the terms in the Fibonacci sequence whose values do not 9exceed four million, find the sum of the even-valued terms.10****************************************/1112#include <iostream>13#include <cmath>1415usingnamespacestd;1617/* Use Binet's formula to generate Fibonacci sequence.18 Only add to sum if number is even. */1920intmain()21{22/* Golden ratio */23constdoublephi=(1.0+sqrt(5.0))/2.0;24constintmax_term=4000000;2526intsum=0;27inta=1;2829for(inti=0;a<max_term;i++)30{31/* (int) cast is like the floor function */32a=(int)((pow(phi,i)/sqrt(5.0))+1.0/2.0);3334if(a%2==0)35sum+=a;36}3738cout<<sum<<endl;39return0;40}