# 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.

using ProjectEulerSolutions

# Brute force solution, cycle through Fibonacci numbers up to 4 mil and sum the
# even ones.
function p002solution_running_sum(n::Integer=3)::Integer
    fibs = fibonacci_bound(BigInt(n))
    running_sum = 0
    @simd for val in fibs
        if val % 2 == 0
            running_sum += val
        end
    end
    return running_sum
end

# With a little thought, we see that it is only the 3*k+2 Fibonacci numbers that
# are even, so just sum them.
function p002solution_sum(n::Integer=3)::Integer
    fibs = fibonacci_bound(BigInt(n))
    return sum(fibs[2:3:length(fibs)])
end

p002 = Problems.Problem(Dict("Running sum" => p002solution_running_sum, "Sum" => p002solution_sum))

Problems.benchmark(p002, 4_000_000 - 1)