The second Euler problem concerns the Fibonacci sequence, which for anyone doing MS221 is the basis of module A1.

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.

Following the logic from the previous problem, then I can check each number of the Fibonacci sequence to see if it’s even and add it to the total, where . We need to repeat this while and add to our total if it’s even.

Sub Main() Dim beganAt As Date = Now Dim n, n1, n2, total As Integer n1 = 0 n2 = 1 Do While n2 < 4000000 n = n1 + n2 n1 = n2 n2 = n If n < 4000000 Then If n Mod 2 = 0 Then total = total + n End If End If Loop Dim endAt As Global.System.TimeSpan = Now.Subtract(beganAt) Dim took As Integer = endAt.Milliseconds Console.WriteLine(total.ToString + " in " + took.ToString + "ms.") Console.ReadKey() End Sub

Gives an answer of 4613732, which is correct. I know there is a more efficient approach but I’m tired and pissed off. Today was not a good day.