This is my implementation of Project Euler #2 in C#. It takes 0 milliseconds to run, and I believe it is correct - I have seen nothing to indicate otherwise!
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
static void GetFibos(ref List<int> nums, int maxVal)
{
nums.Add(1);
int lastNum = 2;
while (lastNum <= maxVal)
{
nums.Add(lastNum);
lastNum = nums[nums.Count - 2] + nums[nums.Count - 1];
}
}
static void Main(string[] args)
{
Stopwatch s = new Stopwatch();
s.Start();
List<int> fiboNums = new List<int>();
GetFibos(ref fiboNums, 4000000);
int sumEvenFibos = 0;
foreach (int i in fiboNums) { if (i % 2 == 0) sumEvenFibos += i; }
s.Stop();
Console.WriteLine(sumEvenFibos);
Console.WriteLine(s.ElapsedMilliseconds);
}
Is there anything I should be doing different?