In this section, we are going to solve a mathematical challenge that asks us to find even Fibonacci numbers. The full question is: by considering the terms in the Fibonacci sequence whose values do not exceed 4 million, find the sum of the even-valued terms.
Since this is a fairly straightforward problem (assuming that you are familiar with the Fibonacci sequence), we're going to build a more exhaustive solution to further our understanding of Ruby. We're not going to write a one-line code answer for this problem; rather, we are going to build a class:
class Fibbing
def initialize(max)
@num_1 = 0
@i = 0
@sum = 0
@num_2 = 1
@max = max
end
def even_fibonacci
while @i<= @max
@i = @num_1 + @num_2
@sum += @i if@i % 2 == 0
@num_1 = @num_2
@num_2 = @i
end
@sum
end
end
In this code...