Find the Sum of a Fibonacci Sequence in Ruby
In this guide, you will learn how to define a Fibonacci class that will add up all of the values of a given number of values in a sequence and produce the total sum of that sequence.
Guide Tasks
  • Read Tutorial
  • Complete the Exercise

Summary

Build a method that can find the total sum of a Fibonacci sequence.

Exercise Description

Define a method that will first, properly implement the fibonacci sequence and generate an array of n values. Second, it should give the sum of the fibonacci sequence.

Example Data

Sequence:
Fibonacci.new(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
Fibonacci.new(23)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025]

Sum:
Fibonacci.new(10)
->376
Fibonacci.new(23)
->196417

Real World Usage

This exercise is very helpful to understand how to implement correctly. It makes adding a large amount of numbers easy and quick and limits the risk of making a mistake.

Test Cases

Code File