Print first n Fibonacci Numbers using Direct Formula


In this article, we are going to solve the problem of printing first n Fibonacci Numbers using a direct formula.

In mathematics, the fibonacci numbers often denoted by Fn (which indicates nth fibonacci number), form a series in which each number is equal to the sum of the preceding two numbers. The nth fibonacci number can be indicates as below −

$$\mathrm{Fn\:=\:F_{n-1}\:+\:F_{n-2}}$$

The series begins with 0 and 1. The first few values in the fibonacci sequence, starting with 0 and 1 are −

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.

So, in this problem we will be given a number N and we need to print first N fibonacci numbers using a direct formula.

Example

INPUT: 4

OUTPUT: 0 1 1 2

INPUT: 8

OUTPUT: 0 1 1 2 3 5 8 13

For this problem we need to know the concept of Binet’s formula which gives the direct formula to get the nth fibonacci number which is discussed in the algorithm section in detail.

Algorithm

According to the formula,$\mathrm{Fn\:=\:F_{n-1}\:+\:F_{n-2}}$ we need (n-1)th term and (n-2)th to get the nth term by adding them. Since in this problem we are supposed to print the first n fibonacci number using a direct formula to get the nth fibonacci number.

To get the nth fibonacci number in the fibonacci sequence, one can apply the explicit formula known as Binet’s formula. It was created by mathematician Jacques Philippe Marie Binet.

Formula

If $\mathrm{Fn}$ denotes the nth fibonacci number in the fibonacci sequence, then it can be expressed as

$$\mathrm{F_n\:=\:\frac{1}{\sqrt5}((\frac{1+{\sqrt5}}{2})^n\:-\:(\frac{1-{\sqrt5}}{2})^n)}$$

NOTE − This formula gives the fibonacci sequence starting from 1 and 1. To get the fibonacci sequence starting from 0 and 1, use n-1 to get the nth fibonacci number.

We can derive this formula using concepts of quadratic equations. We will be using this formula to print every fibonacci number until nth fibonacci number to print first n fibonacci numbers.

Approach

  • We will use a for loop to print all N fibonacci numbers iterating from 0 to n since we are considering the fibonacci sequence starting from 0 and 1.

  • Initialise a variable as fibonacci and store the ith fibonacci number using the above formula for every iteration until i<N.

  • Keep printing fibonacci at every iteration which will give us the first N fibonacci numbers.

Example

Below is the implementation of the above approach in C++ −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void fibonacci(long long int N){ //function to print first N fibonacci numbers
   long long int fibonacci; //to store ith fibonacci number
   
   for(int i=0;i<N;i++) { //using for loop to print all N fibonacci numbers
      //using direct formula to print every fibonacci number until n
      fibonacci = 1/sqrt(5)*(pow((1+sqrt(5))/2,i) - (pow((1-sqrt(5))/2,i)));
      cout<<fibonacci<<" ";
   }
   cout<<endl;
}

//Driver Code
int main(){
   long long int N=10;
   fibonacci(N);
   N=6;
   fibonacci(N);
   return 0;
}

Output

0 1 1 2 3 5 8 13 21 34
0 1 1 2 3 5

Time Complexity: O(n), since for loop runs until i is less than n.

Space Complexity: O(1), since it uses no extra space.

Conclusion

In this article, we learned to print the first N fibonacci numbers using direct formula rather than using recursion. We have also learned about the Binet’s formula to directly get the nth fibonacci number in the fibonacci sequence.

I hope this article helps you to clear all your concepts regarding the topic.

Updated on: 14-Mar-2023

660 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements