Project Euler - Problem 002

August 1st, 2007 by Daniel Høyer Iversen

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, …

Find the sum of all the even-valued
terms in the sequence which do not exceed one million.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
 
#include <iostream>
using namespace std;
 
int main ()
{
int res(1), first(1), second(1), temp(0);
 
while(res < 1000000)
{
 
if( second %2 !=0)
{
res += second;
}
temp = second;
second += first;
first =temp;
 
}
 
cout << "resultatet er ";
cout << res;
return 0;
}
Do you want to use this code?

0 Responses to “ Project Euler - Problem 002”

  1. No Comments

Leave a Response