Created
May 18, 2013 18:07
-
-
Save anonymous/5605311 to your computer and use it in GitHub Desktop.
Project Euler Problem 2
This file contains 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
#! /usr/bin/env ruby | |
# goal is to print the sum of all even fibonacci numbers under 4 million | |
def fibonacci | |
a, b = 1, 2 | |
while true | |
yield a | |
a, b = b, a + b | |
end | |
end | |
def fibonacci_up_to_n(n) | |
fibonacci.take_while {|i| i < n} | |
end | |
even_fibonacci_up_to_4mil = fibonacci_up_to_n(4000000).select {|num| num.even?} | |
puts even_fibonacci_up_to_4mil.reduce(:+) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment