DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Soft Skills Are as Important as Hard Skills for Developers
  • Practical Coding Principles for Sustainable Development

Trending

  • Strategies for Securing E-Commerce Applications
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • Designing AI Multi-Agent Systems in Java
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business

Day 13 of 30 Ruby Coding Challenge - Fibonacci Sequence in Ruby

Day 13 of 30. We're going to solve the famous Fibonacci sequence in Ruby. The next videos are going to be all about getting the algorithm better

By 
Alexandre Gama user avatar
Alexandre Gama
·
Jul. 06, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.0K Views

Join the DZone community and get the full member experience.

Join For Free

Hey friends!

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

Fancy Fibonacci Algorithm Definition

  • To get the next number in a sequence, you have to sum the previous two numbers.

One important point: The Fibonacci sequence already starts with 0 and 1 as the first 2 numbers

Here is a sequence to help you out a bit more:

Plain Text
 




x


 
1
0 1 1 2 3 5 8 13 21 34 55 88 ...



Perfect. Now we want to solve the following puzzle:

We want to calculate the first N numbers in a Fibonacci sequence

First Real Example:

I want to calculate the first 8 numbers in a Fibonacci sequence:

Plain Text
 




xxxxxxxxxx
1


 
1
0 1 1 2 3 5 8 13



Second Real Example:

I want to calculate the first 10 numbers in a Fibonacci sequence:

Plain Text
 




xxxxxxxxxx
1


 
1
0 1 1 2 3 5 8 13 21 34



I’m pretty sure you got it : )

Fibonacci Algorithm in Ruby

Step 1

  • let’s create the fibonacci() method
  • then we’ll start the sequence with 0 and 1
Ruby
 




xxxxxxxxxx
1


 
1
def fibonacci(count)
2
  n1 = 0
3
  n2 = 1
4
  sequence = [n1, n2]
5
end
6

          
7
puts fibonacci(8)



Step 2

  • because the list starts with 2 numbers, we can calculate the next one using a while loop
Ruby
 




xxxxxxxxxx
1
12


 
1
def fibonacci(count)
2
  n1 = 0
3
  n2 = 1
4
  sequence = [n1, n2]
5
  while count > 2 # just a while loop expression that decrements the argument count
6
    count = count - 1
7
  end
8
  return sequence
9
end
10

          
11
puts fibonacci(8)
12
# 0 1 1 2 3 5 8 13



Step 3

  • the next number is the sum of the previous 2 numbers
Ruby
 




xxxxxxxxxx
1
19


 
1
def fibonacci(count)
2
  n1 = 0
3
  n2 = 1
4
  sequence = [n1, n2]
5
  while count > 2
6
        # sum of the previous 2 numbers
7
    n3 = n1 + n2
8
    sequence.push(n3)
9
10
      # assigning the new numbers to calculate the next number in the sequence
11
    n1 = n2
12
    n2 = n3
13
    count = count - 1
14
  end
15
  return sequence
16
end
17
18
puts fibonacci(8)
19
# 0 1 1 2 3 5 8 13



Pretty simple, isn’t it?

Although the code is simple, it’s far from a good code design because:

  • it lacks readability
  • it updates an argument received in the method
  • it has too many local variables

To be honest, sometimes (and even most of the time), a good code design is a matter of context and personal taste, and maybe you might think that this code is already good enough, and I don’t blame you. However, I’ll solve the same problem using a different approach that probably you might like better


Hope to see you in the next Ruby Coding Challenge : )

Don’t forget to come by and say hi Alex
Courses Twitter Youtube Instagram Linkedin GitHub

Coding (social sciences) Plain text

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

Opinions expressed by DZone contributors are their own.

Related

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Soft Skills Are as Important as Hard Skills for Developers
  • Practical Coding Principles for Sustainable Development

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!