euler/crystal/euler002.cr

27 lines
378 B
Crystal
Raw Normal View History

2018-02-25 03:32:39 +00:00
module Euler
module Problem002
extend self
2018-02-21 21:00:08 +00:00
2018-02-25 03:32:39 +00:00
def fibonacci_nums_up_to(n)
result = [1] of Int32
2018-02-21 21:00:08 +00:00
2018-02-25 03:32:39 +00:00
a = 1
b = 2
accum = 0
2018-02-21 21:00:08 +00:00
2018-02-25 03:32:39 +00:00
while accum < n
accum = a + b
result << b
a = b
b = accum
end
result
end
2018-02-21 21:00:08 +00:00
2018-02-25 03:32:39 +00:00
def solution
fibonacci_nums_up_to(4000000).select{ |n| n.even? }.sum
end
end
end