Print the Fibonacci Sequence using 2 Variables


What comes to your mind when reading the title? Here we need to calculate the Fibonacci sequence using only 2 variables.

First, What is Fibonacci Sequence?

The Fibonacci series is a set of numbers where each number is the sum of the two numbers before it. Any number after 0 and 1 is the sum of the two numbers before it.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ...

Many fascinating mathematical features and applications of the Fibonacci series may be found in a variety of disciplines, including finance, biology, and computer technology.

What does the Question in the Title Ask Us?

The question implied by the title typically asks you to create a program or function that generates the sequence without storing all of the intermediate values in an array or list. Instead, the program should keep track of the most recent and previous values in the series using just two variables.

This method is frequently used as a programming and problem−solving exercise since it requires careful variable management and iterative calculation of the following value in the sequence.

Approach

Here is the step−by−step approach to solving the problem

  • Initialise the three variables n, t1, and t2 first.

  • Take user input for the number of terms to be present in the Fibonacci series.

  • In the beginning, t1 should be initialised to 0 and t2 to 1.

  • Iterate through the sequence using a for loop. Since the first two terms have already been initialised, the loop should begin at 1 and continue until n terms have been created.

  • Add t1 and t2 together inside the loop to determine the next term in the series, and then store the result in the third variable `nextTerm`.

  • As t1 is the current term in the sequence, print its value to the console.

  • Update the values of t1 and t2 by setting t1 to t2 and t2 to `nextTerm`, so that they will be the previous two terms in the next iteration of the loop.

Code Implementation

Now, lets code the approach using C++ language.

Example

#include <iostream>
using namespace std;

int main() {
    int n=8 ; // take user input n, as length of the Fibonacci series
    int t1 = 0, t2 = 1, nextTerm;
 // initialize t1 as 0 and t2 as 1 as these are the first two terms in the Fibonacci series
     
   cout << "Fibonacci Series: ";

    for (int i = 1; i <= n; ++i) {
        cout << t1 << ", ";
        nextTerm = t1 + t2; // nextTerm is the sum of first two terms
        t1 = t2; // after storing the sum in `nextterm` , value of t2 is stored in t1
        t2 = nextTerm; // and value of nextTerm is stored in t2
    }
    return 0;
}

Output

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,

Time Complexity: O(n)

Space Complexity: O(1)

Conclusion

In this post, we've attempted to describe how to print the Fibonacci sequence using 2 variables. Hope you had a better view of things and sync up well with the flow.

Happy Learning!

Updated on: 23-Aug-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements