Day 17 Ruby Coding Challenge - Sum Even Numbers in Fibonacci Sequence - DZone Web Dev

Day 17 Ruby Coding Challenge - Sum Even Numbers in Fibonacci Sequence - DZone Web Dev

Day 17 of 30 - Ruby Coding Challenges in 30 Days. We're going to sum all the even numbers in a given Fibonacci sequence with a better code design.

Day 17 of 30. We're going to sum all the even numbers in a given Fibonacci sequence using a better code design in Ruby

Hey friends!

This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series

Today, we want to solve the previous problem in a more Ruby Way

I want to sum all even numbers in a Fibonacci sequence

Last Algorithm Version

This was the last algorithm version of the problem:

Ruby

def fibonacci_sum(count)

 sum = 0

    number = 0

 sequence = []

  (0..count).each do |item|

   number = item if item <= 1

    number = sequence[-1] + sequence[-2] if item > 1

   sequence << number

    sum += number if number % 2 == 0

 end

  sum

end


The code is not that great for a couple of reasons:

*   Too many local variables to manipulate.
*   Two main responsibilities: create the Fibonacci sequence **AND** validate even numbers.

The two main reasons above leave us with code that's difficult to read, and therefore, difficult to maintain. Fewer friends in our team, right? 

I’m going to try to get rid of these problems. Let’s get into it!

## Better Algorithm Version - Ruby Way

I’m going to break the refactoring into a few small steps

### **Step 1 - Splitting Responsibilities**

Let’s create a new method do generate only the Fibonacci sequence

Ruby

def fibonacci(count)

  sequence = []

(0..count).each do |number|

    sequence << number if number <= 1

   sequence << sequence[-1] + sequence[-2] if number > 1

  end

 sequence

end
```

Then we’re going to create the method to sum even numbers based on the generated sequence

Ruby

```
def fibonacci(count)

 sequence = []

  (0..count).each do |number|

   sequence << number if number <= 1

    sequence << sequence[-1] + sequence[-2] if number > 1

 end

  sequence

end

def sum(array)

 # magic here

end

puts sum(fibonacci(10))


### **Step 2 - Sum of Even Numbers**

Now, we just need to sum all the even numbers, given an array of numbers.

As we did [previously here](https://youtu.be/Y3W64fXmfkw), I’m going to use a Ruby symbol, which allows us to reduce a list of items into a single number by applying an operation:

Ruby

def sum(array)

  array.select { |number| number % 2 == 0 }.reduce(:+)

end


The complete code would be:

Ruby

def fibonacci(count)

  sequence = []

(0..count).each do |number|

    sequence << number if number <= 1

   sequence << sequence[-1] + sequence[-2] if number > 1

  end

 sequence

end

def sum(array)

  array.select { |number| number % 2 == 0 }.reduce(:+)

end

`````` puts sum(fibonacci(10)) ```

ruby programming coding ruby on rails algorithm fibonacci tutorial for beginners algorithm analysis coding basics

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Day 14 of 30 Ruby Coding Challenge - Fibonacci Sequence the Ruby Way

Day 14 of 30. We're going to solve the famous Fibonacci sequence by taking advantage of features that Ruby offers.

Day 15 of 30 Ruby Coding Challenge - Fibonacci Sequence Recursively

We're going to solve the famous Fibonacci sequence recursively in Ruby. This is not a better strategy than the previous one, this will be another option. Today, we’ll solve the previous Fibonacci problem using the dreaded recursion approach. A little bit of theory was explored in this video, and today, I’ll straight to the point

Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers

Day 16 of 30 - Ruby Coding Challenges in 30 Days. We're going to sum all the even numbers in a given Fibonacci sequence.This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

Ruby Beginner Tutorial | Basic Ruby Commands

Ruby Beginner Tutorial | Basic Ruby Commands