Algorithms in Ruby: Fibonacci sequence

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!endtests
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




web developer making apps for people with appetite

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Documentation is the Key point of Development

Web3 with Python

A class for patterns ?

What is ACID? Baby don’t hurt me. No more.

How To Use The Laps Chrome Extension (v1.0.0 beta)

Setting up a Grafana Monitor Dashboard for Klever Testnet

Get your k0s development cluster

Concurrency in Clean Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aaron feingold

aaron feingold

web developer making apps for people with appetite

More from Medium

Ruby on Rails Version 7.0

The Binding Class (and binding.pry)

LeetCode Bi-Weekly Contest 71 (Q2)

Rails 6 —add Active class on Navbar | Rejka Permana