Abdulg

Solution to Project Euler #2 (Sum of even Fibonacci numbers)

Feb 21st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. //Compiled using g++ under Debian GNU/Linux
  2.  
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. long int PreviousTwo[2] = {1,2};
  8.  
  9. void PushIntoArray(long int PushThis);
  10.  
  11. int main()
  12. {
  13.         long int Current = 0;
  14.         long int Total = 2; //Possibly cheating
  15.  
  16.         while(Current < 4000000)
  17.         {
  18.                 Current = PreviousTwo[0] + PreviousTwo[1];
  19.                 PushIntoArray(Current);
  20.  
  21.                 if (Current % 2 == 0) Total += Current;
  22.         }
  23.         cout << "Sum of all even-numbered Fibonacci numbers under 4000000 is " << Total << endl;
  24.         return 0;
  25. }
  26.  
  27. void PushIntoArray(long int PushThis)
  28. {
  29.         PreviousTwo[0] = PreviousTwo[1];
  30.         PreviousTwo[1] = PushThis;
  31. }
  32.  
  33. /*
  34. Output:
  35. abdul@debian:~/ProjectEuler$ ./NumberTwo
  36. Sum of all even-numbered Fibonacci numbers under 4000000 is 4613732
  37. */
Advertisement
Add Comment
Please, Sign In to add comment