Kevin Silberberg
  • Home
  • Photos
  • Study

Even Fibonacci Numbers

Problem 2

Author

Kevin Silberberg

Published

April 14, 2025

Problem definition

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:

\[\begin{align} 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \cdots \end{align}\]

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Solution

The fibonacci sequence can be thought of as a discrete dynamical system or a recurrance relation defined as follows,

\[\begin{align} x_{n+1} = x_{n} + x_{n-1} \end{align}\]

where \(x_{0} = 0\) and \(x_{1} = 1\) and \(n = 1, 2, 3, \cdots, N\).

When \(N = 35\) the value \(x_{N}\) exceeds four million, thus we will solve for the fibonacci numbers up to the value \(N = 34\) and then use a julia filter function along with the iseven conditional function to sum only the even values.

N = 34
x = Vector{Int}(undef, N)
x[1] = 0
x[2] = 1
for n = 2:N-1
    x[n+1] = x[n] + x[n-1]
end
println(sum(filter(iseven, x)))
4613732

© Copyright 2025 Kevin Silberberg. Except where otherwise noted, all text and images licensed CC-BY-NC 4.0.