This is a question about Project Euler number 2, a relatively easy one. The question is:
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.
My code is pretty simple; I'm interested in other ways of doing it (lamdba expressions for instance).
namespace ProjectEuler
{
public class Fibonacci
{
// find even number fibonacci terms < 4 mil.
public void FibonacciSequence(int limit)
{
var sw = Stopwatch.StartNew();
int a = 0;
int b = 1;
int total = 0;
int evenTerms = 0;
while (total < limit)
{
total = a + b;
a = b;
b = total;
if (total % 2 == 0)
{
evenTerms += total;
}
}
sw.Stop();
Console.WriteLine(evenTerms);
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
}