Skip to content

Instantly share code, notes, and snippets.

@igrep
Created January 31, 2011 14:32
Show Gist options
  • Save igrep/804097 to your computer and use it in GitHub Desktop.
Save igrep/804097 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*-coding:utf-8-*-
$VERBOSE = true
=begin
Project Euler: 2
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
=end
fib = Hash.new{|hsh, n| hsh[n] = hsh[n-2] + hsh[n-1] }
fib[0] = 0
fib[1] = 1
puts (2..4_0000).map{|i| fib[i]}.select{|i| i.even? }.inject(:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment