re: Project Euler #2 - Even Fibonacci numbers VIEW POST

FULL DISCUSSION
 

I have just given it a try in Ruby

def fibonacci_numbers(upto)
  sequence = [0, 1]

  loop do
    next_number = sequence[-2] + sequence [-1]
    sequence.push next_number if next_number < upto
    break unless next_number < upto
  end

  sequence
end

def sum_of_even_numbers(upto)
  sequence = fibonacci_numbers(upto)

  sequence.select{ |el| el % 2 == 0 }.reduce(:+)
end

puts sum_of_even_numbers(4000000)
code of conduct - report abuse