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

DZone 's Guide to

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

Day 16 of 30. We're going to sum all the even numbers in a given Fibonacci sequence

· Web Dev Zone ·
Free Resource

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 here, here 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:


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

Math comes in handy here. An even number is a number dividable by 2, which in Ruby is represented by


Quite simple, isn’t it? ��

In the next video, we’re going to explore a decent and beautiful Ruby Way to solve this same problem, see you there :)

Thanks for the visit and see you in the next coding challenge!

Don’t forget to come by and say hi!

Courses Twitter Youtube Instagram Linkedin GitHub

algorithm, algorithm analysis, challenges, coding, coding basics, programming, ruby, ruby on rails, tutorial for beginners

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}