Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers

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

DZone > Web Dev Zone > Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers in a Fibonacci Sequence

Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers in a Fibonacci Sequence

  

Hey friends!

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

We’ve solved the Fibonacci sequence herehere and here, which means that we have some clues of how to create a Fibonacci sequence :)

Today we want to be a little bit daring by solving the following problem:

I want to sum all even numbers in a Fibonacci sequence

Fibonacci Sequence in Ruby

As you already know, this is one of the solutions:

Ruby

1

def fibonacci_sum(count)

2

    number = 0

3

  sequence = []

4

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

5

    number = item if item <= 1

6

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

7

    sequence << number

8

  end

9

    sequence

10

end

We’re returning a Fibonacci sequence, however, that’s not what we’re looking for

Sum Even Numbers in a Fibonacci Sequence in Ruby

We’re going to:

  • add a local variable called sum
  • then update this variable only if the number is even
  • return the sum variable

Ruby

1

def fibonacci_sum(count)

2

  sum = 0

3

    number = 0

4

  sequence = []

5

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

6

    number = item if item <= 1

7

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

8

    sequence << number

9

10

    sum += number if number % 2 == 0

11

  end

12

  sum

13

end

ruby programming coding ruby on rails algorithm challenges 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 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 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

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.

Day 18 of 30 Ruby Coding Challenge - Finding the Missing Number Game

We're going to play a game: find the missing number in a given array. This first solution will be not that great but we'll get the job done!