euler/euler002.rb

31 lines
512 B
Ruby
Raw Normal View History

2014-04-25 23:59:14 +00:00
def fibonacci(n)
fibTable = []
if (n == 0) or (n == 1)
return 1
else
if fibTable.at(n-1) == nil
fibTable.insert(n-1, fibonacci(n-1))
fib1 = fibTable.at(n-1)
else
fib1 = fibTable.at(n-1)
end
if fibTable.at(n-2) == nil
fibTable.insert(n-2, fibonacci(n-2))
fib2 = fibTable.at(n-2)
else
fib2 = fibTable.at(n-2)
end
return fib1 + fib2
end
end
sum = 0
for i in 1..32
fib = fibonacci(i)
if (fib%2) == 0
sum += fib
end
end
puts sum