Project Euler: 02 Even Fibonacci Numbers
The first few terms of the fibonacci sequence are
$$
\begin{align*}
1,2,3,5,8,13,21,34,55,89,...
\end{align*}
$$
The goal of this problem is finding the sum of the even-valued terms where the maximum term that we need to consider is at most 4 million.
Solution
One thing to note immediately is that we’re going to exceed 4 million just with the 35th term! So the simplest solution that just implements the fibonacci recurrence (below) will work.
#define N 35 // the 35th term exceeds 4 million
// all fibonacci's terms
unsigned long long all[N] = {0};
void fibonacci_all() {
all[0] = 0; all[1] = 1; all[2] = 1;
for (int i = 3; i < 35; i++) {
all[i] = all[i-1] + all[i-2];
}
}
int main(int argc, const char * argv[]) {
fibonacci_all();
unsigned long long sum = 0;
for (int i = 0; i < N; i++) {
if (all[i] % 2 == 0) { // add only if it's even
sum += all[i];
}
}
printf("sum = %llu\n", sum); // works!
return 0;
}
The entire code is here.