Quote

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 four million.

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 four million.

Here's the C# code I came up with to solve this problem:

static int valueOne = 1; static int valueTwo = 1; static int result = 0; static List<int> resultList = new List<int>(); static int sum = 0; static void Main(string[] args) { int count = Environment.TickCount; Fibonacci(30); foreach ( int i in resultList ) { if ( i % 2 == 0 ) { sum += i; } } Console.WriteLine(sum.ToString("n0")); Console.WriteLine(Environment.TickCount - count + "ms."); Console.ReadLine(); } /// <summary> /// Creates a Fibonacci Sequence. /// </summary> /// <param name="terms"> /// Is the amount of numbers to be added /// to the sequence. /// </param> private static void Fibonacci(int terms) { result = valueOne + valueTwo; resultList.Add(result); terms--; if ( terms != -1 ) { valueOne = valueTwo; valueTwo = result; Fibonacci(terms); } return; }

I'm curious as to what solutions you guys come up with. And tell me what you guys think of my solution and if there's any improvements that I can make to my code. Hopefully my post will spark up some good conversation.