Skip to content

Instantly share code, notes, and snippets.

@abdollar
Created December 8, 2011 23:14
Show Gist options
  • Save abdollar/1449180 to your computer and use it in GitHub Desktop.
Save abdollar/1449180 to your computer and use it in GitHub Desktop.
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
vals = [0, 1]
34.times do
val = vals[-1] + vals[-2]
vals.push(val) if val < 4000000
end
vals.select{|item| item % 2 == 0 }.inject(:+)
@abdollar
Copy link
Author

require 'matrix'
sum = 0
for n in (1 .. 33)
fib = (Matrix[[1,1],[1,0]] ** (n-1))[0,0]
sum += fib if (fib < 4000000) && (fib % 2 == 0)
end
sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment