26 lines
597 B
C++
26 lines
597 B
C++
#include "euler_p2.h"
|
|
|
|
/*
|
|
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.
|
|
*/
|
|
|
|
int euler2(int limit) {
|
|
|
|
int current = 1;
|
|
int previous = 1;
|
|
int temp = 0;
|
|
int result = 0;
|
|
|
|
while (temp < limit) {
|
|
temp = current + previous;
|
|
previous = current;
|
|
current = temp;
|
|
if (temp % 2 == 0)
|
|
{
|
|
result += temp;
|
|
}
|
|
}
|
|
return result;
|
|
} |