Advertisement
hb20007

Euler2

Dec 1st, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. //By considering the terms in the Fibonacci sequence whose values do not exceed four million,
  2. //find the sum of the even - valued terms.
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     long unsigned int x = 1, y = 2, sum = 2, ctr = 0;  
  9.     for (long unsigned int fibonacci = 3; fibonacci < 4000000; fibonacci = x + y)
  10.     {
  11.         if (fibonacci % 2 == 0)
  12.         sum += fibonacci;
  13.         if (ctr % 2 == 0)  
  14.             x = fibonacci;  
  15.         else
  16.             y = fibonacci;
  17.         ctr++;
  18.     }
  19.     cout << sum <<endl;
  20.     system("pause");
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement