Finding the sum of even Fibonaccis

Finding the sum of even Fibonaccis

This problem comes from Project Euler. Project Euler is a website with a bunch of computational problems designed to be solved with software applications.


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.

class Euler

  def self.sum_of_even_fibonaccis(number)
    fibonaccis = []
    sum = 0

      a = 0
      b = 1

      while b < number do

        a, b = b, a + b

    fibonaccis.each do |n|
      if n % 2 == 0
        sum += n

    puts sum

This can be executed from the command line by running:

ruby -r "./find_sum_of_even_fibonaccis.rb" -e "Euler.sum_of_even_fibonaccis 4000000"