euler/ruby/euler002.rb

17 lines
270 B
Ruby
Raw Normal View History

2014-12-05 00:50:52 +00:00
def even_fibonacci_up_to(n)
Enumerator.new do |y|
a = 0
b = 1
loop do
temp = b
b = a + b
a = temp
y << b
2014-04-25 23:59:14 +00:00
end
2014-12-05 00:50:52 +00:00
end.take_while { |i| i < n }.select { |i| i.even? }
2014-04-25 23:59:14 +00:00
end
2014-12-05 00:50:52 +00:00
def solution
even_fibonacci_up_to(4000000).inject(:+)
2014-04-25 23:59:14 +00:00
end