# Algorithms in Ruby: Fibonacci sequence

• By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
`new_term = term1 + term2`
`declare procedure to take in a limiting number and return the sum of all the even numbers in the fib sequence up to that limit  starting from zero  our first number is also zero  our second number is one  so long as the starting point is less than the limit, let's run some math and evaluate those returns against some conditions  do math: first plus second, and assign this sum as the new starting point  is this sum even? if so, let's store it somewhere, otherwise its not something we're going to be using (we could store it, but its useless)  now let's make some reassignments for the next run of the sequence: our new_first is our former_second, and our starting point will be the sum we've already determined.  keep running this until the sum of first and second are greater than limit, at which point break and do no go forwards into reassignment or (most importantly) storing evens.lastly, take all those evens, add em up and return!endtests10201001000`
`def even_fibonacci_sum(limit)  # variable declarations  array_of_evens = []  i = 0  first = 0  second = 1  # lets use a while loop  while i < limit   # add first and second, then assign it to i    i = first + second   # if this i is greater than our limit, break the loop    break if i > limit   # what to do now? evaluate if i is even, and store in array    array_of_evens << i if i.even?   # reassign our variables for the next loop   # new first is former second    first = second  # new second is reassigned i    second = i  end # take the array and do the maths sum = | x, y, x | x + y + z to infinity and keep going potentially  array_of_evens.reduce(:+)end`

--

--