Commit c18027f2 by Andrew Dahl

Problem 2 Solved

parent eca7db7c
Showing with 34 additions and 0 deletions
Question:
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.
Answer: 4613732
#include <iostream>
using namespace std;
int main() {
int sum = 2;
int term1 = 1;
int term2 = 2;
int term_tmp = 0;
while(term2 <= 4000000)
{
term_tmp = term2;
term2 += term1;
term1 = term_tmp;
if(term2 % 2 == 0)
sum += term2;
}
cout << sum;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment