Skip to content

Instantly share code, notes, and snippets.

@jinahadam
Created March 27, 2016 05:59
Show Gist options
  • Save jinahadam/9e2eef22b3e3f70923f8 to your computer and use it in GitHub Desktop.
Save jinahadam/9e2eef22b3e3f70923f8 to your computer and use it in GitHub Desktop.
def fib(n)
if n <= 2
return (1..n).to_a
else
k = n - 2
arr = [1,2]
k.downto(2).each do |j|
i = arr[-2]+arr[-1]
arr = arr.push(i) if i < 4000000
end
end
return arr
end
fib(1000).select { |k| k.even? }.inject(:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment