Skip to content

Instantly share code, notes, and snippets.

Created May 18, 2013 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5605311 to your computer and use it in GitHub Desktop.
Save anonymous/5605311 to your computer and use it in GitHub Desktop.
Project Euler Problem 2
#! /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