Skip to content

Instantly share code, notes, and snippets.

@eric1234
Created September 19, 2009 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric1234/189461 to your computer and use it in GitHub Desktop.
Save eric1234/189461 to your computer and use it in GitHub Desktop.
Problem 2 on Project Euler
class Numeric
def even?
self % 2 == 0
end
end
module Enumerable
def sum &blk
blk = proc {|x| x} unless block_given?
inject(0) {|sum, element| sum + (blk[element] || 0)}
end
end
def fib(limit, *args)
return [args.last] if args.sum > limit
fib(limit, args.last, args.sum) << args.first
end
puts fib(4000000, 1).sum {|n| n if n.even?}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment