Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.35 KB | None | 0 0
  1. class SolutionTwo < Problem
  2.  
  3.   def initialize()
  4.     @memoized = []
  5.   end
  6.  
  7.   def fibonacci(x)
  8.     @memoized[x] ||= x <= 1 ? x : fibonacci(x - 2) + fibonacci(x - 1)
  9.   end
  10.  
  11.   def sequence()
  12.     1.upto(Float::INFINITY).lazy.map {|x| fibonacci(x)}.take_while {|x| x < 4000000}.select {|x| x.even?}
  13.   end
  14.  
  15.   def solve()
  16.     sequence.sum
  17.   end
  18. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement