
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
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.
- Related Articles
- How to print the first ten Fibonacci numbers using C#?
- How to print the Fibonacci Sequence using Python?
- Python program to print decimal octal hex and binary of first n numbers
- Print first n distinct permutations of string using itertools in Python
- Print all increasing sequences of length k from first n natural numbers in C++
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ Program to Find Fibonacci Numbers using Iteration
- Print first m multiples of n without using any loop in Python
- C++ Program to Find Fibonacci Numbers using Dynamic Programming
- C++ Program to Find Fibonacci Numbers using Matrix Exponentiation
- Find fibonacci series upto n using lambda in Python
- C Program to print numbers from 1 to N without using semicolon
- Print first m multiples of n in C#
- Find the GCD of N Fibonacci Numbers with given Indices in C++
- Average of first n odd naturals numbers?
