Created
December 8, 2011 23:14
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(:+) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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