Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Compiled using g++ under Debian GNU/Linux
- #include<iostream>
- using namespace std;
- long int PreviousTwo[2] = {1,2};
- void PushIntoArray(long int PushThis);
- int main()
- {
- long int Current = 0;
- long int Total = 2; //Possibly cheating
- while(Current < 4000000)
- {
- Current = PreviousTwo[0] + PreviousTwo[1];
- PushIntoArray(Current);
- if (Current % 2 == 0) Total += Current;
- }
- cout << "Sum of all even-numbered Fibonacci numbers under 4000000 is " << Total << endl;
- return 0;
- }
- void PushIntoArray(long int PushThis)
- {
- PreviousTwo[0] = PreviousTwo[1];
- PreviousTwo[1] = PushThis;
- }
- /*
- Output:
- abdul@debian:~/ProjectEuler$ ./NumberTwo
- Sum of all even-numbered Fibonacci numbers under 4000000 is 4613732
- */
Advertisement
Add Comment
Please, Sign In to add comment