I went for what I thought was an interesting solution with Ruby this time. It uses a lazy enumerable over an infinite range and calculates the sum and the fibonacci numbers until the end condition is met.
range = 1..Float::INFINITY
sum = 0
last_two_fibs = [0,1]
next_fib = last_two_fibs + last_two_fibs
sum += next_fib if next_fib % 2 == 0
last_two_fibs = [last_two_fibs, next_fib]
next_fib < max
# => 4613732
This uses constant space, so does not compute an array of fibonacci numbers, just holds the latest two and the current sum. It runs in O(n) time and for 4,000,000 took less than 0.2s on my Macbook Pro.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.